Reputation: 3079
Currently, I have Navigation Stack
PageA => PageB => PageC
I want to pus Push PageD as the root page
So the final result would be
=>PageD
How can I achieve that using Flutter?
Upvotes: 1
Views: 732
Reputation: 6178
Push a root page:
static void pushRootPage({
required BuildContext context,
required Widget page,
}) {
var router = MaterialPageRoute(builder: (context) => page);
Navigator.pushAndRemoveUntil(context, router, (route) => false);
}
Upvotes: 0
Reputation: 13698
Take a look at this blog right here.
https://medium.com/flutter-community/flutter-push-pop-push-1bb718b13c31
Your exact scenario is at pushNamedAndRemoveUntil
section.
Upvotes: 1
Reputation: 5381
You can use pushNamedAndRemoveUntil
like this-
Navigator.of(context).pushNamedAndRemoveUntil('/screenD', (Route<dynamic> route) => false);
(Basically pop all - A, B and C and then push D) Refer this for details.
Upvotes: 2