How to get the onDismiss callback of showModalBottomSheet in flutter?

Viewed 658

I have a showModalBottomSheet, and isDismissible is set to true, When I click outside the showModalBottomSheet I want to receive the callback for it.

in showModalBottomSheet I have hide button, and on click of hide button i'm doing Navigator.pop(context) to hide the dialog,

Tried whenComplete() & then() but i get callback for every dismiss even for Hide button click.

How do I do it?

1 Answers

Uday,

You can pass parameter when popping the route to see how modal sheet was closed:

                showModalBottomSheet<bool>(
                  context: context,
                  isDismissible: true,
                  builder: (BuildContext context) {
                    return Center(
                      child: RaisedButton(
                        child: const Text("hide"),
                        onPressed: () => Navigator.of(context).pop(true), // pass true indicating that it was hidden via button
                      ),
                    );
                  },
                ).then(
                  (isManuallyHidden) {
                    if (isManuallyHidden ?? false) {
                      print("hidden via button");
                    } else {
                      print("dismissed");
                    }
                  },
                );
Related