I'm displaying a ModelBottomSheet when the FAB is pressed on a screen. When the ModelBottomSheet is popped I run a method with the data returned from the ModelBottomSheet which updates the screen.
onPressed: () {
print('FAB Pressed');
showModalBottomSheet<SearchData>(
isScrollControlled: true,
context: context,
builder: (context) => SingleChildScrollView(
child: SearchScreen(
searchData: _searchData,
locationEnabled: !userPosition.error,
),
),
).then((value) => _updateList(value));
},
On the ModelBottomSheet I have a button which pops the ModelBottomSheet and returns the data (_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);
},
How can achieve the same result as the press of the button on the ModelBottomSheet, when the ModelBottomSheet is dismissed (i.e. the user taps on the top half of the screen)?
You can wrap your ModalWidget with WillPopScope. You can see the example below
WillPopScope(
onWillPop: () async {
Navigator.pop(context, data);
return true; // return true if needs to be popped
},
child: ModelWidget(
…
),
);
This will ensure that Navigator.pop is called when auto popped using the back button.
Credit: How can I control what is passed to Navigator.pop() when clicking outside of a showModalBottomSheet?