Navigator
Navigator
▲▼▲▼▲▼
tags : #coding #flutter
references : Widget
▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼
▲▼▲▼▲▼
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Page2()),
);
Navigator.pop(context);
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => Page2()),
);
Useful for login/logout flows (e.g., replace login screen with home screen).
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => HomeScreen()),
(route) => false, // Remove all previous routes
);
Use when navigating to the main screen after logout.
if (Navigator.canPop(context)) {
Navigator.pop(context);
}
Navigator.maybePop(context);
Navigator.popUntil(context, ModalRoute.withName('/home'));
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text(data)), // Displays "Hello"
);
}
}
The value "User clicked OK" will be returned to the previous screen.
4. Named Routes (Recommended for Large Apps)
Named routes help organize navigation instead of manually defining each route.
void main() {
runApp(MaterialApp(
initialRoute: '/',
routes: {
'/': (context) => HomeScreen(),
'/page1': (context) => Page1(),
'/page2': (context) => Page2(),
},
));
}
Navigator.pushNamed(context, '/page2');
Navigator.pushNamed(
context,
'/page2',
arguments: "Hello from Page1",
);
return Scaffold(
body: Center(child: Text(data)), // Displays: "Hello from Page1"
);
}
}
5. Custom Page Transitions (Animations)
You can customize page transitions.
PageRouteBuilder _createRoute() {
return PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) => Page2(),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
return FadeTransition(
opacity: animation,
child: child,
);
},
);
}
Navigator.push(context, _createRoute());
PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) => Page2(),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
return SlideTransition(
position: Tween<Offset>(
begin: Offset(1.0, 0.0), // Start from right
end: Offset.zero,
).animate(animation),
child: child,
);
},
);
6. Nested Navigators (For Bottom Navigation &
Tab Views)
You can have multiple Navigators inside different sections of an app.
Navigator(
key: navigatorKey, // Separate navigator key for each tab
onGenerateRoute: (routeSettings) {
return MaterialPageRoute(builder: (context) => Page2());
},
);
7. Best Practices
✅ Use named routes for large projects.
✅ Handle back navigation properly using canPop() .
✅ Return data using pop(context, data) .
✅ Optimize navigation in BottomNavigationBar with nested navigators.
✅ Use custom transitions for better UX.
8. Summary
Function Purpose
push() Navigate to new screen
pop() Go back
pushReplacement() Replace the current screen
pushAndRemoveUntil() Remove all previous screens
popUntil() Pop multiple screens
pushNamed() Navigate using named routes
canPop() Check if popping is possible
pop(context, data) Return data from a screen
Final Thoughts
Mastering Flutter’s Navigator is essential for building multi-screen apps efficiently. Whether
you use basic navigation, named routes, or advanced animations, it ensures a smooth user
experience. 🚀