In Flutter, I need to pop the route stack until I get to a modal bottom sheet, but I can't figure out how to do that using Navigator.popUntil .
The scenario is this: I have a settings screen (lets call it SettingsScreen
) which you can reach in a number of different ways from the main HomeScreen
, resulting in a different stack of routes in the navigator. So for example the stack could be:
HomeScreen -> ScreenA -> SettingsScreen
or
HomeScreen -> ScreenA -> ScreenB -> SettingsScreen
Now I need a way to pop until I get to the HomeScreen. Normally I would achieve this using:
Navigator.popUntil(
context,
(Route<dynamic> route) => route.settings.name == "HomeScreen"
);
But here's the catch: sometimes there will be a modal bottom sheet open in the HomeScreen
, and I need that to remain open. Since a modal bottom sheet is a route on the stack, if the sheet is open, the stack might be for example:
HomeScreen -> _ModalBottomSheetRoute<dynamic> -> ScreenA -> ScreenB -> SettingsScreen
So popping until I hit the HomeScreen
causes the modal bottom sheet to pop, which closes it. So, I need to pop until I get to the HomeScreen
OR until I get to _ModalBottomSheetRoute<dynamic>
.
Unfortunately the bottom sheet route has no name or arguments in it's settings, and the runtime type of _ModalBottomSheetRoute<dynamic>
is private so I can't even use that as a test. So how do I change the logic in Navigator.popUntil
to stop when it gets to the modal bottom sheet? Or is there another way of doing this?
how about passing route settings to showModalBottomSheet(... routeSettings: RouteSettings(name: 'MyModalBottomSheet'))? You could then test for the name given in popupUntil....