Flutter Navigation & Routing Explained: Push, Pop, Named Routes & Arguments Guide
Learn Flutter navigation and routing in depth. Understand Navigator, push, pop, named routes, passing arguments, and best practices for scalable Flutter apps.
Introduction
Navigation is a core part of every mobile application. Users move from one screen to another — login screen, home screen, details page, settings page, and more.
In Flutter, navigation is handled using the Navigator class. Understanding how navigation works is essential for building real-world applications.
In this complete guide, we will explore:
- What is Navigator in Flutter?
- How push and pop work
- Named routes
- Passing data between screens
- Returning data from screens
- Best practices for scalable routing
Understanding Navigator
Navigator manages a stack of routes (screens). Think of it like a stack of pages:
- Push adds a new page on top
- Pop removes the top page
Basic Navigation Using push
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondScreen(),
),
);
This pushes SecondScreen onto the navigation stack.
Going Back Using pop
Navigator.pop(context);
This removes the current screen from the stack and returns to the previous one.
Creating a Simple Second Screen
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Second Screen")),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text("Go Back"),
),
),
);
}
}
Named Routes
Named routes allow cleaner navigation in larger applications. Instead of directly creating routes everywhere, you define routes centrally.
Defining Routes
MaterialApp(
initialRoute: '/',
routes: {
'/': (context) => HomeScreen(),
'/second': (context) => SecondScreen(),
},
)
Navigating Using Named Route
Navigator.pushNamed(context, '/second');
Passing Arguments Between Screens
Sending Data
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondScreen(name: "Ravi"),
),
);
Receiving Data
class SecondScreen extends StatelessWidget {
final String name;
SecondScreen({required this.name});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text("Hello $name"),
),
);
}
}
Returning Data from a Screen
Sending Data Back
Navigator.pop(context, "Success");
Receiving Returned Data
final result = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondScreen(),
),
);
print(result);
pushReplacement
Replaces the current screen instead of stacking on top.
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => HomeScreen(),
),
);
pushAndRemoveUntil
Used for clearing the navigation stack. Common in login flows.
Navigator.pushAndRemoveUntil( context, MaterialPageRoute(builder: (context) => HomeScreen()), (route) => false, );
Common Navigation Mistakes
- Using too many nested navigators
- Hardcoding routes everywhere
- Not handling back navigation properly
- Forgetting async when expecting return values
Best Practices
- Use named routes in larger apps
- Keep route management centralized
- Use pushReplacement for authentication flows
- Return data properly using await
Advanced Routing (Brief Overview)
For large apps, developers often use:
- onGenerateRoute
- Custom route transitions
- Router API (Navigator 2.0)
Conclusion
Navigation is fundamental in every Flutter app. Once you understand how the Navigator stack works, you can confidently build multi-screen applications.
Master push, pop, named routes, and argument passing, and you are ready to build real production apps.
Share
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Angry
0
Sad
0
Wow
0