【问题标题】:Displaying Snackbar inside a SearchDelegate在 SearchDelegate 中显示 Snackbar
【发布时间】:2023-03-07 07:38:01
【问题描述】:

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

【问题讨论】:

  • 你有想过这个吗?

标签: flutter


【解决方案1】:

想通了

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 中对我不起作用(因为在小部件树中 SearchDelegate 之前没有 Scaffold)。你能分享更多代码吗,这样我就可以得到更多关于这个调用的上下文?
  • 更新的答案,基本上在我的列表视图中,我返回一个带有按钮的项目列表,点击后将打开一个应用程序应该从服务器 _launchExtraContent 下载的文档。但是,如果应用程序无法下载该文件,那么当抛出异常时,它就会被捕获,并且一个快餐栏会显示错误消息。
猜你喜欢
  • 2017-03-27
  • 2020-05-16
  • 2019-12-15
  • 2020-09-24
  • 2017-11-23
  • 1970-01-01
  • 1970-01-01
  • 2017-04-10
  • 1970-01-01
相关资源
最近更新 更多