使用搜索委托(Flutter)时如何在buildResult方法中调用this.close?

时间:2020-05-25 20:25:49

标签: flutter dart

我有一个非常简单的SearchApp,其中有一个带有搜索图标和文本小部件的AppBar。轻按搜索图标后,将调用showSearch并调用CustomSearchDelegate。我如何做到这一点,以便在buildResults方法中,无需返回窗口小部件即可立即调用this.close,并将查询传递给SearchApp无状态窗口小部件以在“文本”窗口小部件上显示。

代码如下:

class SearchApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: <Widget>[
          IconButton(
            tooltip: 'Search',
            icon: const Icon(Icons.search),
            //Don't block the main thread
            onPressed: () {
              showSearch(context: context, delegate: CustomSearchDelegate());
            },
          ),
        ],
      ),
      body: Text(query), // The query data should be displayed here
    );
  }
}

class CustomSearchDelegate extends SearchDelegate {
  @override
  List<Widget> buildActions(BuildContext context) {
    return <Widget>[Icon(Icons.close)];
  }

  @override
  Widget buildLeading(BuildContext context) {
    return Icon(Icons.arrow_back_ios);
  }

  @override
  Widget buildResults(BuildContext context) {
    // GO back to SearchApp page immediately with the query result
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    return Column();
  }
}

1 个答案:

答案 0 :(得分:0)

我意识到我可以编辑搜索类的源代码,所以我复制并粘贴了源代码,在Textfield下,我只是删除了showResult方法并用close代替了它,因此不会显示结果页面。至于将查询传递到原始页面,我要做的就是将值存储在异步变量中。