Sub topic 1
How the Navigator and its route stack work
Prerequisites
This chapter assumes you know Dart syntax, async and await, basic Flutter widgets such as Scaffold and AppBar, and how BuildContext connects a widget to the widget tree.
The Navigator's working model
Flutter's Navigator manages a stack of routes. A route represents a screen, or a layer of screen content, that can be shown in the app. The route at the top of the stack is visible. Routes below it remain in the stack and can become visible again when the top route is removed.
The stack follows last-in, first-out behavior:
| Operation | Stack effect | Visible route |
|---|---|---|
| Start at home | `[Home]` | Home |
| Push details | `[Home, Details]` | Details |
| Pop details | `[Home]` | Home |
A MaterialApp creates a default navigator for its application. You access that navigator through a BuildContext:
Navigator.of(context)The context must belong to a widget below the intended navigator. In an app with nested navigators, choosing the correct context matters because each navigator owns a different route stack.
Adding and removing routes
To display another screen, push a route onto the stack. A MaterialPageRoute builds a page using its builder callback.
To return, pop the current route. Flutter also connects platform back actions, such as Android's system back button, to the navigator's pop behavior when the route allows it.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: HomeScreen()));
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (_) => const DetailsScreen(),
),
);
},
child: const Text('Open details'),
),
),
);
}
}
class DetailsScreen extends StatelessWidget {
const DetailsScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Details')),
body: Center(
child: ElevatedButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Back'),
),
),
);
}
}The push call adds DetailsScreen above HomeScreen. The pop call removes the details route. The home route is then visible again, but its state may depend on how that screen was constructed and whether Flutter kept it alive.
Common stack mistakes
- Calling
popwhen only the root route remains can leave the app without a useful screen. CheckNavigator.canPop(context)when a pop may be optional. - Using the wrong context can target a nested navigator instead of the app-level navigator.
- Pushing repeatedly for the same destination can create duplicate routes. Choose a stack operation deliberately when a screen should be replaced or reused.
- Treating navigation as global state makes flows harder to test. Keep navigation calls close to the user action or centralize them when the app needs declarative routing.
Exercise
If the stack is [Home, Settings, Account] and the current route is Account, which route becomes visible after one pop?
Show answer
Settings becomes visible because Account is removed from the top of the stack.