Calling Navigator.of(context, rootNavigator: true).pop() gives black screen - flutter

I have a open Chrome custom tab and then I open another app. This other app then opens my app via Asset Links and somehow my navigation stack is not preserved correctly. This means that a new page/widget is placed on top of my custom tab and I need the user to land on the custom tab again (I have another issue on this also, but now I just want to try a dirty fix).
So I want to just close that added page/widget. If I manually navigate back on my Android device via the back arrow, it works great and the added page/widget is closed and I see my custom tab.
I have tried calling
Navigator.of(context, rootNavigator: true).pop();
If I call "pop" once I get a black page and still if I then manually navigate back I come back to the custom tab. If I pop 2 times like this
Navigator.of(context, rootNavigator: true)..pop()..pop();
I get
The following assertion was thrown building Navigator-[LabeledGlobalKey#ad310](dirty, dependencies: [HeroControllerScope, UnmanagedRestorationScope], state: NavigatorState#e4365(tickers: tracking 1 ticker)):
'package:flutter/src/widgets/navigator.dart': Failed assertion: line 5203 pos 12: '!_debugLocked': is not true.
Still I can manually navigate back to the custom tab... Any ideas on mow I can close this widget on top and get back to my custom tab?

You have to remove rootNavigator: true which is causing you to pop from the first instance in your navigator. The navigator is like a list of pages that you opened in the order you opened them. That attribute is forcing all routing from the first page on the navigator, so when you pop, you remove everything and remain with no screen at all.
Try Navigator.of(context).pop();

Related

How do you create a side navigation drawer that persists across pages?

I've looked through many tutorials for the side nav drawer. I can create one that works fine to lead to different pages. However, when I travel to a page that's different from home, it only gives me the arrow icon to go back to home at the top left instead of keeping the button to bring me back to the side navbar. How can I prevent this?
I can't use the home page to navigate everywhere because it's just supposed to be a blank splash screen.
You can define your drawer in a separate widget file, that you can import everywhere you have a scafold.
I created a package for it because I was missing similar functionality. If you want a Flutter approach for this navigation check out: https://api.flutter.dev/flutter/material/NavigationRail-class.html
Or if you want to have a look at my package: https://pub.dev/packages/side_navigation
It's because you're moving to a new page/Scaffold (probably using Navigator.push()). So, the default button in the AppBar will be the back button.
You can either have the same Drawer in every Scaffold you navigate to, which is not recommended since you'll just keep pushing routes to the navigation stack.
Or, you can change pages within the Scaffold. Check the interactive examples in BottomNavigationBar and NavigationRail to get an idea of how to do it. Basically instead of calling Navigator.push() when a tile in Drawer is tapped, just update the selected index and call setState().

Flutter close drawer on back button press in bottom navigation bar

I'm building a flutter app with persistent bottom navigation bar that contains a menu button.
The menu is supposed to open a drawer from any screen and trough the back button it should close it.
For that I'm using a scaffold including the bottom navigation widget and also the drawer.
This works fine for all screens that are directly accessible trough the bottom navigation bar.
The problem arises if I navigate (I'm using auto_route for routing) to another sub page.
In the sub page it seems that it is not possible to close the drawer.
In the sub page I dont provide a Scaffold since I would have nested scaffolds.
I'm using the WillPopScope and onPop looks like this:
if (Scaffold.of(context).isEndDrawerOpen) {
Navigator.of(context).pop();
Unfortunately this just pops the sub page while the drawer stays open.
One solution would be to pass a closing function to every child screen (or something like a global key for the global scaffold) but I would appreciate a more elegant solution
Do you have any idea how I can fix that?
Thanks in advance!

How to enable back button in a screen after using Navigator.pushReplacementNamed() to replace the page in flutter?

Let's say I have two screens A and B .
I am on screen A and I use pushReplacementNamed() to get to screen B.
Now I want to go back to screen A when user swipes right (default in iOS for back button).
Screen A :
Navigator.pushReplacementNamed(context, '/B'),
Screen B :
WillPopScope(
onWillPop: () async {
Navigator.pushReplacementNamed(context, '/A');
return false;
}),
But this is not working. Back button is still disabled.
Note - I don't want to make custom back button in AppBar to achieve this. I want to use the default back button behaviour.
What I think is wrong with your code is that you're returning false from onWillPop, return false disregards the action of the back tap. Try changing that to true.
Also if you want to eventually go back to screen A from screen B it makes more sense to just use a push() on screen A and then on screen B inside onWillPop you no longer need to manually pop the route using navigator, you can just return true.
Update
In response to the your comment below, for what you want to achieve, you can normally push a route when on Home page and then on other pages you could just use pushNamedAndRemoveUntil:
Navigator.of(context).pushAndRemoveUntil("B",ModalRoute.withName('Home'));
It will push the page you want and remove everything until page Home. Since "Home" will now always be the root, you no longer need to override onWillPop and can proceed with a normal pop on back button.
Note: Page "Home" has to already be present in the stack for this to work.
If you want to push and remove everything else from the stack change:
ModelRoute.withName("Home"),
To
(Route<dynamic> route) => false,

From CupertinoTabView open a page that covers the tabs

I'm using CupertinoTabScaffold and CupertinoTabView for my App. For one screen that opens, I would like it to open so it entirely covers the screen, also the tabs. However, I want to use the same Navigator (as opposed to Navigator.of(context, rootNavigator: true) ) because I'm also using RouteAware and switching between navigators is breaking some RouteAware functionality. How can I do that?

How to use CircularProgressBar in flutter

I want to show a CircularProgressBarIndicator in the flutter on a button click for some seconds and navigate to another page and loads the current page for some duration on button click and while loading it shows the CircularProgressBarIndicator.
Well you can actually achieve this by wrapping your button onPressed function with setState() where you change a variable isLoading to either true or false when you want the CircularProgressIndicator to work. Also, dont forget to call false to stop loader before Navigating to next page because you don't want the user to pop the page and meet a loading spinner. But enough talking since there is a flutter package i always use for that. Check it out at:
https://pub.dev/packages/modal_progress_hud
I guess the documentation of the package is pretty easy too.