【问题标题】:Flutter return data when ModalBottomSheet is dismissed关闭 ModalBottomSheet 时颤振返回数据
【发布时间】:2021-09-23 19:58:07
【问题描述】:

当 FAB 在屏幕上按下时,我正在显示 ModelBottomSheet。当 ModelBottomSheet 被弹出时,我使用从 ModelBottomSheet 返回的数据运行一个方法来更新屏幕。

onPressed: () {
              print('FAB Pressed');
              showModalBottomSheet<SearchData>(
                isScrollControlled: true,
                context: context,
                builder: (context) => SingleChildScrollView(
                  child: SearchScreen(
                    searchData: _searchData,
                    locationEnabled: !userPosition.error,
                  ),
                ),
              ).then((value) => _updateList(value));
            },

在 ModelBottomSheet 上,我有一个按钮可以弹出 ModelBottomSheet 并返回数据 (_searchData)。

ElevatedButton(
                onPressed: () {
                  print('search button pressed');
                  if (_textSearch.text == null || _textSearch.text.isEmpty) {
                    _searchData.searchTerm = '';
                  } else {
                    _searchData.searchTerm = 'value=${_textSearch.text}';
                  }
                  if (_idSearch.text == null || _idSearch.text.isEmpty) {
                    _searchData.guideId = '';
                  } else {
                    _searchData.guideId = '&location=${_idSearch.text}';
                    _searchData.showOfficial = false;
                  }
                  Navigator.pop(context, _searchData);
                },

当 ModelBottomSheet 被关闭(即用户点击屏幕的上半部分)时,如何获得与按下 ModelBottomSheet 上的按钮相同的结果?

【问题讨论】:

  • 我相信你只需要在这种情况下检查结果是否为空
  • 嗨,你能再解释一下吗?我不明白这将如何达到预期的结果?数据需要从 ModelBottomSheet 传递。我不知道当它被解雇时如何传递这些数据?

标签: flutter navigator flutter-showmodalbottomsheet


【解决方案1】:

您可以使用 WillPopScope 包装您的 ModalWidget。你可以看下面的例子

 WillPopScope(
  onWillPop: () async {
    Navigator.pop(context, data);
    return true; // return true if needs to be popped
  },
  child: ModelWidget(
    …
  ),
);

这将确保在使用返回按钮自动弹出时调用 Navigator.pop。

信用:How can I control what is passed to Navigator.pop() when clicking outside of a showModalBottomSheet?

【讨论】:

    猜你喜欢
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 2022-06-18
    • 2020-07-02
    • 1970-01-01
    相关资源
    最近更新 更多