0

当 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 上的按钮相同的结果?

4

1 回答 1

1

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

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

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

信用:如何控制在 showModalBottomSheet 外部单击时传递给 Navigator.pop() 的内容?

于 2021-07-21T20:21:18.560 回答