Hey there!
Did you know you can have more than one navigator in your Flutter app? This can be super helpful for organizing your app's flow.
Why Use Multiple Navigators?
Sometimes, you have parts of your app that are like a separate little world of pages, like an onboarding process or a login flow. Using a secondary navigator for these sections keeps things tidy and separate from your main app navigation.
Structure of Navigators
You can think of it like having a Main router navigator for your main app screens and a Secondary navigator nested inside for a specific feature, like onboarding.
Navigating to the Main Navigator
If you are deep inside a secondary navigator and need to jump back to a page in your main app flow (controlled by the root navigator), you need a special way to do it.
How to Navigate Up
You use Navigator.of(context, rootNavigator: true)
to get the main navigator, and then you can push a new page onto it.
Navigator.of(context, rootNavigator: true).pushNamed("premium")
This tells Flutter to use the very first, top-level navigator, not the one you are currently in.
Example: Onboarding Flow
A common use case is an onboarding screen that might have several steps (handled by a secondary navigator). Once onboarding is done, you navigate to the main app screen using rootNavigator: true
.