Nested navigation helps you manage multiple screens inside a part of your app separately. It keeps navigation organized and easy to control.
Nested navigation in Flutter
Navigator(
key: navigatorKey,
onGenerateRoute: (RouteSettings settings) {
return MaterialPageRoute(
builder: (context) => YourScreen(),
);
},
)Navigator(
key: _tabNavigatorKey,
onGenerateRoute: (settings) {
if (settings.name == '/') {
return MaterialPageRoute(builder: (_) => TabHomeScreen());
} else if (settings.name == '/details') {
return MaterialPageRoute(builder: (_) => TabDetailsScreen());
}
return null;
},
)final GlobalKey<NavigatorState> _nestedKey = GlobalKey<NavigatorState>();
Navigator(
key: _nestedKey,
onGenerateRoute: (settings) => MaterialPageRoute(builder: (_) => NestedScreen()),
)This app shows a main screen with a nested navigator inside the body. The nested navigator starts at NestedHome with a button. Pressing the button navigates inside the nested navigator to NestedDetails. The main app bar stays the same.
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: MainScreen(), ); } } class MainScreen extends StatefulWidget { @override State<MainScreen> createState() => _MainScreenState(); } class _MainScreenState extends State<MainScreen> { final GlobalKey<NavigatorState> _nestedNavigatorKey = GlobalKey<NavigatorState>(); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Nested Navigation Example')), body: Navigator( key: _nestedNavigatorKey, onGenerateRoute: (settings) { if (settings.name == '/') { return MaterialPageRoute(builder: (_) => NestedHome(onNext: () { _nestedNavigatorKey.currentState?.pushNamed('/details'); })); } else if (settings.name == '/details') { return MaterialPageRoute(builder: (_) => NestedDetails()); } return null; }, ), ); } } class NestedHome extends StatelessWidget { final VoidCallback onNext; NestedHome({required this.onNext}); @override Widget build(BuildContext context) { return Center( child: ElevatedButton( onPressed: onNext, child: Text('Go to Details'), ), ); } } class NestedDetails extends StatelessWidget { @override Widget build(BuildContext context) { return Center( child: Text('Details Screen'), ); } }
Each nested navigator needs its own GlobalKey to manage its navigation stack.
Nested navigation keeps the main app navigation separate and clean.
Use nested navigation especially when you have tabs or sections with their own screens.
Nested navigation lets you manage screens inside a part of your app separately.
Use a Navigator widget with a unique key and onGenerateRoute inside your widget tree.
This helps keep navigation organized and user-friendly.
Practice
Solution
Step 1: Understand nested navigation concept
Nested navigation allows managing navigation stacks inside a part of the app independently from the main app navigation.Step 2: Identify the purpose
This helps keep navigation organized and user-friendly by isolating navigation flows.Final Answer:
To manage navigation inside a specific part of the app separately -> Option AQuick Check:
Nested navigation = Manage part navigation separately [OK]
- Thinking nested navigation replaces the main Navigator
- Confusing nested navigation with multiple apps
- Assuming nested navigation disables navigation
Solution
Step 1: Identify widget for navigation
The Navigator widget manages a stack of routes and is essential for navigation in Flutter.Step 2: Confirm nested navigation use
Nested navigation uses a Navigator with a unique key inside a widget subtree.Final Answer:
Navigator -> Option DQuick Check:
Navigator widget = navigation management [OK]
- Choosing MaterialApp which is for app-wide setup
- Selecting Scaffold which is for layout
- Picking Container which is a basic layout widget
Navigator(
key: _nestedKey,
onGenerateRoute: (settings) {
return MaterialPageRoute(
builder: (context) => NestedHomeScreen(),
);
},
)Solution
Step 1: Analyze onGenerateRoute usage
The onGenerateRoute callback returns a MaterialPageRoute with NestedHomeScreen as the builder.Step 2: Determine initial screen
Since this is the only route generated, NestedHomeScreen will be the first screen shown inside the nested Navigator.Final Answer:
NestedHomeScreen -> Option AQuick Check:
onGenerateRoute returns NestedHomeScreen [OK]
- Confusing main app screen with nested screen
- Assuming missing route causes error
- Expecting blank screen without routes
Navigator(
onGenerateRoute: (settings) {
return MaterialPageRoute(
builder: (context) => NestedScreen(),
);
},
)
It causes navigation issues inside the app.Solution
Step 1: Check Navigator setup
For nested navigation, Navigator should have a unique GlobalKey to manage its own stack separately.Step 2: Identify missing key
This code lacks a key, which can cause navigation conflicts or unexpected behavior.Final Answer:
Missing a unique key for the nested Navigator -> Option CQuick Check:
Nested Navigator needs unique key [OK]
- Thinking route type causes issues
- Believing Scaffold is mandatory for Navigator
- Assuming initialRoute is required
Solution
Step 1: Understand tab navigation needs
Each tab should maintain its own navigation stack independently to allow back navigation inside tabs.Step 2: Apply nested Navigator per tab
Using a Navigator with a unique key inside each tab widget isolates navigation stacks correctly.Step 3: Evaluate other options
Single Navigator can't isolate stacks; multiple MaterialApps is heavy and incorrect; IndexedStack alone doesn't handle navigation.Final Answer:
Use a Navigator with a unique key inside each tab widget to manage its own stack -> Option BQuick Check:
Nested Navigator per tab = isolated stacks [OK]
- Using one Navigator for all tabs causing stack conflicts
- Trying multiple MaterialApps which is not recommended
- Ignoring Navigator inside tabs and losing navigation control
Ready to prove it?
Pass the test and this pattern moves unknown
