Flutter Navigation & Routes
Real apps have more than one screen. In Flutter, each screen is usually called a route, and the Navigator widget manages them as a stack: pushing a route puts a new screen on top, popping it removes the top screen and reveals the one underneath. MaterialApp creates a Navigator for you.
push and pop
Navigator.push takes a Route — most commonly a MaterialPageRoute, which animates the new screen in the platform's usual way. Navigator.pop goes back. The AppBar automatically shows a back arrow when there is something to go back to.
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: FilledButton(
child: const Text('Open details'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const DetailScreen()),
);
},
),
),
);
}
}
class DetailScreen extends StatelessWidget {
const DetailScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Details')),
body: Center(
child: OutlinedButton(
onPressed: () => Navigator.pop(context),
child: const Text('Go back'),
),
),
);
}
}Passing data to a screen
Because screens are just widgets, you pass data through their constructor.
class ProductScreen extends StatelessWidget {
const ProductScreen({super.key, required this.name, required this.price});
final String name;
final double price;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(name)),
body: Center(
child: Text('\${price.toStringAsFixed(2)}',
style: const TextStyle(fontSize: 32)),
),
);
}
}
// Somewhere in a list:
// Navigator.push(context, MaterialPageRoute(
// builder: (_) => const ProductScreen(name: 'Headphones', price: 59.9)));Getting a result back
Navigator.push returns a Future that completes with whatever the new screen passes to pop. Use await to wait for the user's choice.
// On the first screen
Future<void> _pickColor(BuildContext context) async {
final color = await Navigator.push<String>(
context,
MaterialPageRoute(builder: (_) => const ColorPicker()),
);
debugPrint('User picked: $color');
}
// On the second screen
class ColorPicker extends StatelessWidget {
const ColorPicker({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Pick a color')),
body: ListView(
children: [
for (final c in ['Red', 'Green', 'Blue'])
ListTile(title: Text(c), onTap: () => Navigator.pop(context, c)),
],
),
);
}
}User picked: Green
If the user presses the system back button instead, the result is null — that is why the type is String? after awaiting.
Named routes
For small apps you can register routes by name in MaterialApp and navigate with pushNamed:
MaterialApp(
initialRoute: '/',
routes: {
'/': (context) => const HomeScreen(),
'/settings': (context) => const SettingsScreen(),
},
);
// Later:
Navigator.pushNamed(context, '/settings');(Settings screen slides in)
Navigator methods at a glance
| Method | Effect on the stack |
|---|---|
push(route) |
Adds a screen on top |
pop([result]) |
Removes the top screen |
pushReplacement(route) |
Replaces the top screen (e.g. login → home) |
pushAndRemoveUntil(route, predicate) |
Pushes and clears older screens |
pushNamed('/name') |
Pushes a registered named route |
canPop() |
true if there is a screen to go back to |
Tip: Larger apps, deep links and the web URL bar are easier with a routing package such as go_router (maintained by the Flutter team). It builds on the same Navigator ideas.
Test yourself
What does Navigator.pop(context) do?
- Opens a new screen
- Removes the top route and returns to the previous one
- Closes the app
- Reloads the screen
How does a pushed screen send a value back?
- With a global variable
Navigator.pop(context, value)— the value completes the Future returned by push- With
setStateon the old screen - It cannot
After logging in, which method stops the user from going back to the login screen?
- push
- pushNamed
- pushReplacement
- canPop