skip to Main Content

I have specific situation.
I have splash screen page, when I go want to next page, but when user will press back button it will not go back to splash screen but to other page.

Splash Page -> Third page

Back schema:

Third page -> Second page -> Quit app

When user is in this Third page, I want to go back to Second page.

It is possible without using named routes?

2

Answers


  1. You can use the Navigator widget’s pushAndRemoveUntil method for solve this issue.

    Navigator.pushAndRemoveUntil(
      context,
      MaterialPageRoute(builder: (context) => ThirdPage()),
      (route) {
        //::::::::::>Only keep SecondPage in the stack<:::::::::://
        return route.settings.name == "SecondPage";
      },
    );
    

    Look at below full code example ,

    Navigator.of(context).pushAndRemoveUntil(
      MaterialPageRoute(builder: (context) => ThirdPage()),
      (route) => false, // Removes all previous routes 
    );
    Navigator.of(context).push(
      MaterialPageRoute(builder: (context) => ThirdPage()
      )
    );
    
    Login or Signup to reply.
  2.      Step 1:
         // Navigate to the Third Page and remove Splash Page from the stack
          Navigator.pushAndRemoveUntil(
              context,
              MaterialPageRoute(builder: (context) => ThirdPage()),
              (route) => false, // Removes all previous routes
            );
    
          Step 2:
           // Navigate to the Second Page
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => SecondPage()),
            );
          Step 3: for third page
          onWillPop: () async {
          // Navigate to the Second Page when back button is pressed
           Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => SecondPage()),
          );
        return false; // Prevent default back button behavior
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search