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 badges2 Answers
Reset to default 0You 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