Favas Kv
Favas Kv

Reputation: 3079

Flutter Navigate to a Page as Root page

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

Answers (3)

无夜之星辰
无夜之星辰

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

Archie G. Quiñones
Archie G. Quiñones

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

Keerti Purswani
Keerti Purswani

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

Related Questions