Search code examples
flutternavigatordrawer

Correct Flutter navigation practice in Drawer


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.


Solution

  • 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 :)