Screens are Routes
In Flutter a screen is called a route (or page). The Navigator keeps a stack of routes, like a pile of cards. Opening a new screen puts a card on top. Going back removes the top card.
Open a New Screen
dart
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const DetailPage()),
);
},
child: const Text('Open details'),
)
Go Back
dart
Navigator.pop(context);
An AppBar on a pushed screen shows a back arrow automatically. On Android, the system back button also calls pop.
Full Two-Screen Example
dart
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Center(
child: FilledButton(
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (_) => const DetailPage()),
),
child: const Text('Go to details'),
),
),
);
}
}
class DetailPage extends StatelessWidget {
const DetailPage({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'),
),
),
);
}
}
Return Data to the Previous Screen
dart
// Screen A: wait for the result
final color = await Navigator.push<String>(
context,
MaterialPageRoute(builder: (_) => const PickColorPage()),
);
if (!context.mounted) return;
debugPrint('User picked: $color');
// Screen B: send the result back
Navigator.pop(context, 'blue');
Other Useful Methods
| Method | What it does | Use for |
Navigator.push | Add a screen on top | Open details |
Navigator.pop | Remove the top screen | Go back |
Navigator.pushReplacement | Replace the current screen | Splash to Home |
Navigator.pushAndRemoveUntil | Push and clear the stack | After login or logout |
Navigator.popUntil | Go back to a specific screen | Close a flow |
Navigator.maybePop | Pop only if possible | Custom back buttons |
dart
// After login: open Home and remove all previous screens
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (_) => const HomePage()),
(route) => false,
);
Named Routes (Simple Apps)
dart
MaterialApp(
initialRoute: '/',
routes: {
'/': (context) => const HomePage(),
'/settings': (context) => const SettingsPage(),
},
)
// Open it
Navigator.pushNamed(context, '/settings');
What next? Named routes are fine for small apps, but they are weak for URLs, parameters and deep links. For real apps, the next lessons teach go_router, which the Flutter team recommends.
Pop Warning: BuildContext After await
After an await, the widget may have been removed. Check context.mounted (or mounted in a State class) before you use the context again, as shown above.
Conclusion
Navigator.push and pop are the foundation of all navigation. Learn them well, even if you later use go_router.
Quick Summary: Navigator is a stack. push opens a screen, pop goes back, and pushAndRemoveUntil clears the history.