最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

flutter - It is possible to push page and set up back page with Navigator? - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Jan 20 at 11:34 Karol WiśniewskiKarol Wiśniewski 5081 gold badge9 silver badges24 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

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()
  )
);
     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
发布评论

评论列表(0)

  1. 暂无评论