Flutter 在SearchDelegate中显示Snackbar

Flutter 在SearchDelegate中显示Snackbar,flutter,Flutter,我使用的是SearchDelegate,希望在用户尝试使用空查询执行搜索时显示一个Snackbar。我尝试从buildSuggestions和buildResults方法返回Scaffold小部件,然后在buildResults方法中使用Builder/GlobalKey,如果搜索查询的长度为零,则向用户显示消息。但是,这会导致在引发异常的render方法期间更新脚手架的状态。有人遇到过类似的挑战吗?似乎是一个常见的用例,您希望在搜索代理中显示一个Snackbar,但我似乎无法找到一个简单的方法

我使用的是SearchDelegate,希望在用户尝试使用空查询执行搜索时显示一个Snackbar。我尝试从
buildSuggestions
buildResults
方法返回Scaffold小部件,然后在
buildResults
方法中使用Builder/GlobalKey,如果搜索查询的长度为零,则向用户显示消息。但是,这会导致在引发异常的render方法期间更新脚手架的状态。有人遇到过类似的挑战吗?似乎是一个常见的用例,您希望在搜索代理中显示一个Snackbar,但我似乎无法找到一个简单的方法来实现它。

找到了它

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,
    ));
  }
}
类数据搜索扩展了SearchDelegate{
List drugList=新列表();
数据搜索(未来列表药物名称){
this.drugListFuture=listDrugName;
}
@凌驾
列出buildActions(BuildContext上下文){
//应用程序栏的操作
返回[
图标按钮(
图标:图标(图标。清除),
已按下:(){
query=“”;
})
];
}
@凌驾
小部件buildLeading(BuildContext上下文){
//应用程序栏左侧的前导图标
返回图标按钮(
图标:动画指令(
图标:animatedics.menu_箭头,进度:transitionAnimation),
已按下:(){
关闭(上下文,空);
});
}
@凌驾
小部件构建结果(构建上下文){
//显示所选内容的结果
返回null;
}
@凌驾
小部件构建建议(构建上下文){
返回新的FutureBuilder(
future:db.getDrugEntries(),
生成器:(BuildContext上下文,异步快照){
如果(!snapshot.hasData | snapshot.data.length<1){
返回新中心(
子级:新加载指示器(Constants.msgLoading));
}否则{
drugList=snapshot.data;
//当用户搜索某物时显示
final suggestionList=query.isEmpty
?药单
:药单
。其中((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();
返回ListView.builder(
itemBuilder:(上下文,索引){
字符串drugName=suggestionList[index].genericName;
字符串drugId=suggestionList[index].drugId;
int queryIndex=drugName.indexOf(查询);
如果(查询索引==-1){
queryIndex=0;
}
int queryIndexEnd=queryIndex+query.length;
返回容器(按钮/…onTap:_launchExtraContent(上下文,drugId);
},
itemCount:suggestionList.length,
);
}
});
}
_
_launchExtraContent(BuildContext上下文,StringtheFileName)异步{
试一试{
//......
}捕获(e){
_showSnackBar(上下文,“错误:无法检索文件,请提交错误报告”);
}
}
void\u showSnackBar(BuildContext上下文,字符串文本){
Scaffold.of(上下文).showSnackBar(新的SnackBar(
内容:新文本(
文本,
textAlign:textAlign.center,
),
背景颜色:Colors.red,
));
}
}

你有没有发现这一点?已经有一段时间了,但我相信这种方法在SearchDelegate中对我不起作用(因为在小部件树中SearchDelegate之前没有支架)。你能分享更多的代码吗,这样我就可以获得更多的调用上下文吗?更新的答案,基本上在我的列表视图中,我返回一个带有按钮的项目列表,点击这些按钮将打开应用程序应该从服务器下载的文档。
\u launchExtraContent
。但是如果应用程序无法下载该文件,则当抛出异常时,将捕获该异常,并显示一个snackbar错误消息。