Sub topic 1
How the Navigator and its route stack work
This chapter explains how Flutter tracks which screen is visible and how screens pass data to each other.
Prerequisites
This chapter assumes you know:
- Dart syntax, including
asyncandawait - Basic widgets such as
StatelessWidget,Scaffold, andAppBar - What
BuildContextis and how it is passed tobuild
The route stack
The Navigator is a widget that manages a stack of routes. A route is a screen plus the transition used to show it. The top route is the visible screen. Adding a route puts it on top, and removing the top route reveals the one below it.
Moving between screens with push and pop
push adds a screen to the stack, and pop removes the top screen.
import 'package:flutter/material.dart';
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 DetailScreen(title: 'Details'),
),
);
},
child: const Text('Open details'),
),
),
);
}
}
class DetailScreen extends StatelessWidget {
const DetailScreen({super.key, required this.title});
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(title)),
body: Center(
child: ElevatedButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Go back'),
),
),
);
}
}Passing data to the next screen
Pass data through the constructor. The destination screen declares a field, and the caller sets it when building the route. This keeps the data typed and checked at compile time.
Getting a result back from a screen
push returns a Future. Call pop with a value, and the Future completes with that value. If the user leaves with the system back button, the value is null.
Future<void> pickColor(BuildContext context) async {
final color = await Navigator.of(context).push<String>(
MaterialPageRoute<String>(
builder: (_) => const ColorPickerScreen(),
),
);
if (!context.mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Picked: ${color ?? 'nothing'}')),
);
}
class ColorPickerScreen extends StatelessWidget {
const ColorPickerScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Pick a color')),
body: Center(
child: ElevatedButton(
onPressed: () => Navigator.of(context).pop('red'),
child: const Text('Red'),
),
),
);
}
}Exercise
A user opens a detail screen, presses the system back button, and the caller receives null. Why does the caller need to handle null, and how would you change ColorPickerScreen so the caller always receives a value?
Show answer
The system back button calls pop without a value, so the Future completes with null. The caller should use a default such as color ?? 'nothing', as the example does. To always return a value, wrap the screen in a PopScope and call Navigator.of(context).pop('red') from its onPopInvokedWithResult callback, or handle the default in the caller.