0

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)?

RedHappyLlama
  • 53
  • 1
  • 8
  • I believe you just need to check if the result is null in that case – croxx5f Jul 14 '21 at 21:31
  • Hi, can you explain that a bit further? I don't see how that would achieve the desired outcome? The data needs to be passed from the ModelBottomSheet. I don't know how to pass this data when it is dismissed? – RedHappyLlama Jul 16 '21 at 07:20

1 Answers1

1

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?

RedHappyLlama
  • 53
  • 1
  • 8
  • Works well when clicking outside the modal, but `onWillPop` is not triggered when you pull the view down – ndelanou Oct 24 '22 at 15:36