Navigator 1.0 & Imperative Stack Architecture
Master Flutter's foundational Navigator 1.0 stack mechanics: route pushing/popping, returning results via Futures, onGenerateRoute mapping, and Android back-button interception with PopScope.
1. The LIFO Navigation Stack
Navigator 1.0 models screens as a Last-In, First-Out (LIFO) Stack of Route objects managed by the root NavigatorState.
2. Core Imperative Navigation API
// 1. PUSH: Add a new screen to top of stack
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const SettingsScreen()),
);
// 2. POP: Remove top screen and pass return data to previous caller
Navigator.pop(context, {'saved': true});
// 3. PUSH REPLACEMENT: Replace current screen (e.g. Splash -> Login)
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => const LoginScreen()),
);
// 4. PUSH AND REMOVE UNTIL: Clear all history (e.g. Logout -> Login)
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => const LoginScreen()),
(Route<dynamic> route) => false, // false removes ALL existing routes
);Awaiting Results from Pushed Routes
Routes in Flutter return typed Future<T?> instances:
// Caller screen:
Future<void> _selectFilter(BuildContext context) async {
final String? selectedCategory = await Navigator.push<String>(
context,
MaterialPageRoute(builder: (context) => const FilterSelectionScreen()),
);
if (selectedCategory != null) {
print("User selected filter: $selectedCategory");
}
}
// Child screen:
void _onCategoryTap(BuildContext context, String category) {
Navigator.pop(context, category); // Passes category back to caller's await!
}3. Dynamic Routing with onGenerateRoute
To centralize route definitions and enable deep argument parsing:
MaterialApp(
initialRoute: '/',
onGenerateRoute: (RouteSettings settings) {
switch (settings.name) {
case '/':
return MaterialPageRoute(builder: (_) => const HomeScreen());
case '/order-details':
// Extract typed arguments
final args = settings.arguments as OrderDetailsArguments;
return MaterialPageRoute(
builder: (_) => OrderDetailsScreen(orderId: args.orderId),
);
default:
return MaterialPageRoute(
builder: (_) => Scaffold(
body: Center(child: Text("Route not found: ${settings.name}")),
),
);
}
},
);4. Intercepting Android Back Navigation with PopScope
In modern Flutter (Flutter 3.16+), the legacy WillPopScope widget is deprecated in favor of PopScope:
class FormEditorScreen extends StatefulWidget {
const FormEditorScreen({super.key});
@override
State<FormEditorScreen> createState() => _FormEditorScreenState();
}
class _FormEditorScreenState extends State<FormEditorScreen> {
bool _hasUnsavedChanges = true;
@override
Widget build(BuildContext context) {
return PopScope(
canPop: !_hasUnsavedChanges, // If false, prevents automatic OS back pop
onPopInvokedWithResult: (bool didPop, dynamic result) async {
if (didPop) return; // Pop already succeeded
// Show confirmation dialog before allowing user to discard changes
final bool shouldDiscard = await showDialog<bool>(
context: context,
builder: (ctx) => AlertDialog(
title: const Text("Discard changes?"),
actions: [
TextButton(onPressed: () => Navigator.pop(ctx, false), child: const Text("Cancel")),
TextButton(onPressed: () => Navigator.pop(ctx, true), child: const Text("Discard")),
],
),
) ?? false;
if (shouldDiscard && context.mounted) {
Navigator.pop(context); // Manually trigger exit
}
},
child: Scaffold(
appBar: AppBar(title: const Text("Edit Record")),
body: const Center(child: Text("Form Fields")),
),
);
}
}❓ Knowledge Check
What is the function of the predicate function passed into Navigator.pushAndRemoveUntil(context, newRoute, predicate)?
Why was WillPopScope replaced by PopScope in modern Flutter?
Provider, Riverpod & BLoC Patterns
Compare production state management paradigms in Flutter: Provider context mechanics, Riverpod 2.x compile-safe reactive containers, and BLoC / Cubit unidirectional event architectures.
Declarative Routing & GoRouter Architecture
Master modern declarative routing in Flutter using GoRouter: URL synchronization, path/query parameters, authentication redirection guards, deep linking, and nested tab navigation with StatefulShellRoute.