Reputation: 43
Quick question on navigation between Flutter screens from a re-usable drawer in Material App.
I have a drawer set up with two navigation options in a list to Screen1() and Screen2() eg:
// Go to Screen 1
Navigator.push(context, MaterialPageRoute(
builder: (context) => Screen1()),
//
// Go to Screen 2
Navigator.push(context, MaterialPageRoute(
builder: (context) => Screen2()),
If a user repeatedly navigates between Screen1 and Screen2 via the drawer, will this create an inefficient and endless stack of screens? My intention was to change the drawer on each screen - to use Navigator.pop() so that the user could go back to the other screen, but as I understand it, Navigator.pop() would only destroy the drawer itself - not the screen!
This seems a bit weird to me. What would you recommend I do in the drawer to keep things efficient? Can I code navigator pop to break out of the drawer to pop the actual screen?
Thank you.
Upvotes: 2
Views: 1195
Reputation: 450
Instead of pushAndRemoveUnitl you can use pushReplacement. that replaces the previous screen with a new one that prevents the endless stack. do upvote if useful :)
Upvotes: 4
Reputation: 5200
You can use pushAndRemoveUntil:
Push the given route onto the navigator, and then remove all the previous routes until the predicate returns true.
If you'd like to remove every previous route, the second argument, predicate
, should be like this: (_) => false
. There's also a named variant.
Upvotes: 1