Flutter pop until reaching modal bottom sheet - flutter

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....

Related

Flutter: How to navigate to a specific tab in TabBar on button click which is in another screen?

I have HomeScreen with two tabs, SecondScreen and a ThirdScreen. I wish to navigate to second tab by clicking on the button located inside the ThirdScreen. How to achieve this?
Navigator.push gets me back to first tab, which is not what I want.
my navigation map in my app is somewhat like this: second tab -> SecondScreen -> ThirdScreen -> HomeScreen. how to go back to the tab i came from , from the third screen.

Handling Android's native back button on flutter_split_view

I'm currently developing an app which uses the flutter_split_view plugin to automatically display split view. There's one main annoyance, though, pressing the Android's native back button from the child screen (i.e. the right widget) simply closes the app.
I have tried using WillPopScope to call SplitView.of(context) on the child screen, because the SplitView constructor does not accept external controllers (e.g. TabController for tabs) which I could call to redirect the back button calls to the SplitView instead.
import 'package:flutter/material.dart';
import 'package:flutter_split_view/flutter_split_view.dart';
class ChildPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
SplitViewState splitView = SplitView.of(context);
return WillPopScope(
onWillPop: () async {
splitView.pop();
return false;
},
child: Scaffold(...),
);
}
}
Is there a way to solve this?
You can use the WillPopScope widget to capture the back button press event, and then use the splitView.pop() method to navigate to the previous screen, instead of closing the app.
By wrapping the Scaffold widget with a WillPopScope widget, you can intercept the back button press event, and then use the splitView.pop() method to navigate to the previous screen. The onWillPop callback should return Future, returning false will prevent the back button from doing its default action, in this case close the app. And that way you can redirect the back button calls to the SplitView instead.
This should work as expected if implemented correctly. However, you should test it on the actual device to make sure the behavior is the one you expect.
Don't think that split_view can help you split the current page into several single running StateFullWidget's. All the pages you see are only controlled by the context of the current page. So, the pop you reference in the subclassed widget will only apply to the current page. If the current page is the last page, there will be no page after running pop, so the program exits

how do I know if a page is pushed or not in navigation stack?

let say I have 2 pages, PageA and PageB.
if a user tap a button in PageA then it will move to PageB using the code below
Navigator.of(context).pushNamed("pageB");
so as a result, there is back button in the top left app bar in PageB
my question is .....
how do I know if a page is pushed or not? I want to do something only if there is a back button in the appbar in a page
You can use this:
final hasPagePushed = Navigator.of(context).canPop();
This is actually what the flutter's AppBar is using to know whether or not it should display the back button.

is it posible to not to show all poped screens when using popUntil?

when I use
Navigator.popUntil(context, ModalRoute.withName('/myPage))
all screens that are being poped appears until I get the "myPage" screen
is there any way to only show myPage screen without the poped screens
Check this out pushAndRemoveUntil
It will allow you to push the route you want and remove all routes stacked till the predicate returns true.

Page transitions issue [Flutter]

I have an app with bottom navbar. I can navigate between pages through the navbar. My problem is when i go to another page (not a button for this page on navbar) navbar disappears. I used that code below when click button and go to another page. But as i said navbar dissappears. I want navbar always stay.
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ProgrammingScreen()));
I tried PageView widget but i could'nt navigate pages from buttons. How can i solve this problem?
One option is to all those screens be in the page view widget.
But another thing you can do is put the exact same bottom nav bar in the new pushed screen and if the user selects an option. You can use Navigator.pop() and then navigate to the pageView with the selected option