在SearchDelegate中显示Snackbar

时间:2019-06-10 21:01:18

标签: flutter

我正在使用SearchDelegate,并希望在用户尝试使用空查询执行搜索时显示Snackbar。我尝试过从buildSuggestionsbuildResults方法中返回脚手架小部件,然后在buildResults方法中使用Builder / GlobalKey如果搜索查询中有一个向用户显示一条消息,长度为零。但是,这会导致在渲染方法期间更新Scaffold的状态,从而引发异常。有人应对过类似的挑战吗?似乎是一个常见的用例,您想在搜索委托中显示一个Snackbar,但我似乎无法理解一种简单的方法。

1 个答案:

答案 0 :(得分:0)

想通了

class DataSearch extends SearchDelegate<String> {
  List<Drug> drugList = new List<Drug>();
  DataSearch(Future<List<Drug>> listDrugName) {
    this.drugListFuture = listDrugName;
  }

  @override
  List<Widget> buildActions(BuildContext context) {
    // actions for app bar
    return [
      IconButton(
          icon: Icon(Icons.clear),
          onPressed: () {
            query = "";
          })
    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    // leading icon on the left of app bar
    return IconButton(
        icon: AnimatedIcon(
            icon: AnimatedIcons.menu_arrow, progress: transitionAnimation),
        onPressed: () {
          close(context, null);
        });
  }

  @override
  Widget buildResults(BuildContext context) {
    // show result from selection
    return null;
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    return new FutureBuilder(
        future: db.getDrugEntries(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (!snapshot.hasData || snapshot.data.length < 1) {
            return new Center(
                child: new LoadingIndicator(Constants.msgLoading));
          } else {
            drugList = snapshot.data;
            // show when user searches for something
            final suggestionList = query.isEmpty
                ? drugList
                : drugList
                    .where((r) =>
                        (r.drugId.toLowerCase())
                            .contains(query.toLowerCase()) ||
                        (r.fullDrugName.toLowerCase())
                            .contains(query.toLowerCase()) ||
                        (r.otherName.toLowerCase())
                            .contains(query.toLowerCase()) ||
                        (r.tradeName.toLowerCase())
                            .contains(query.toLowerCase()))
                    .toList();
            return ListView.builder(
              itemBuilder: (context, index) {
                String drugName = suggestionList[index].genericName;
                String drugId = suggestionList[index].drugId;
                int queryIndex = drugName.indexOf(query);
                if (queryIndex == -1) {
                  queryIndex = 0;
                }
                int queryIndexEnd = queryIndex + query.length;


                return Container(button//...onTap:_launchExtraContent(context,drugId);
              },
              itemCount: suggestionList.length,
            );
          }
        });
  }



  _

  _launchExtraContent(BuildContext context, StringtheFileName) async {
    try {
      //......
    } catch (e) {
      _showSnackBar(context,'ERROR: Unable to retrieve file please submit a bug report');
    }
  }

  void _showSnackBar(BuildContext context, String text) {
    Scaffold.of(context).showSnackBar(new SnackBar(
      content: new Text(
        text,
        textAlign: TextAlign.center,
      ),
      backgroundColor: Colors.red,
    ));
  }
}