CHAPTER 10
Beginner
Navigation and Routing in Flutter
Updated: May 16, 2026
25 min read
# CHAPTER 10
Navigation and Routing in Flutter
1. Introduction
A professional application is rarely contained on a single screen. Users must tap a product to view its details, swipe open a settings menu, or log in to access a dashboard. Managing this movement between screens is called Routing. In Flutter, screens are just widgets, and the system that moves between them works exactly like a stack of playing cards. In this chapter, we will master Navigation and Routing in Flutter. We will learn how to push new screens onto the stack, pop them off to return, define architectural Named Routes, and pass data (like a user's ID) from one screen to another.2. Learning Objectives
By the end of this chapter, you will be able to:-
Understand the stack-based architecture of the Flutter
Navigator.
-
Use
Navigator.push()to transition to a new screen.
-
Use
Navigator.pop()to return to the previous screen.
-
Configure and use
Named Routesfor cleaner architecture.
- Pass variables and data between different screens.
3. The Navigator Stack
Imagine a stack of plates.-
The
Home Screenis the first plate.
-
When the user clicks "Settings", Flutter drops the
Settings Screenplate on top of theHome Screen. The user now sees Settings.
-
When the user clicks the "Back" arrow, Flutter throws the top plate away, revealing the
Home Screenunderneath it.
Navigator class using Push (add to stack) and Pop (remove from stack).
4. Basic Navigation (Push and Pop)
To move from Screen A to Screen B:
dart
To return from Screen B back to Screen A: *(Note: If your Scaffold has an AppBar, Flutter automatically adds a Back Arrow that does this for you!)*
dart
5. Passing Data Between Screens
If a user taps an image of a red shoe, theDetailsScreen needs to know *which* shoe to display!
You pass data by requiring it in the constructor of the second screen.
Screen B (Receiving the Data):
dart
Screen A (Sending the Data):
dart
6. Named Routes (Advanced Architecture)
If you have 50 screens in your app, typing outMaterialPageRoute everywhere becomes messy. Professional apps use Named Routes. You define a dictionary of URLs in your MaterialApp!
dart
7. Visual Learning: The Route Stack
txt
8. Common Mistakes
-
The Context Error: If you try to use
Navigator.push(context, ...)directly inside therunApporMaterialAppwidget before aScaffoldis built, Flutter will crash with an error saying *"Navigator operation requested with a context that does not include a Navigator."* Thecontextmust come from a widget *inside* or *below* theMaterialApp!
9. Best Practices
-
PushReplacement: If you are on a "Login Screen", and the user logs in successfully, you don't want them to be able to press the "Back" button and go back to the Login Screen. Use
Navigator.pushReplacement(context, newRoute). This throws away the Login screen and replaces it with the Dashboard, preventing the user from returning.
10. Mini Project: A Multi-Screen App
Objective: Build an app with a Home screen that passes data to a Welcome screen.-
1.
Create
HomeScreen(StatelessWidget). Add a TextField (we will cover forms later, so just hardcode a stringString userName = "Alice";for now).
-
2.
Add an
ElevatedButton. In theonPressed, writeNavigator.push.
-
3.
Create a second screen called
WelcomeScreen. Ensure its constructor requires aString name;.
-
4.
In
WelcomeScreen's Scaffold body, display a largeText("Welcome, $name!").
- 5. Run the app. Click the button on Home. Watch the screen slide over, displaying "Welcome, Alice!". Click the back arrow in the AppBar to return!
11. Practice Exercises
- 1. What class manages the stack of screens in a Flutter application?
-
2.
If an e-commerce app has 100 products, how do you prevent having to create 100 different
ProductScreens?
12. MCQs with Answers
Question 1
A user is on the 'Shopping Cart' screen. They click the 'Checkout' button to move to the 'Payment' screen. Which Navigator method adds the Payment screen to the top of the stack?
Question 2
When defining Named Routes in MaterialApp, what data type represents the dictionary map of string URLs to Widget screens?
13. Interview Questions
-
Q: Explain the push and pop stack architecture of the Flutter
Navigator. How does this align with standard mobile OS behaviors (like the physical Android back button)?
-
Q: Describe how to pass complex data (like a User object containing an ID, name, and email) from a ListView screen to a Details screen using
MaterialPageRoute.
-
Q: Contrast
Navigator.pushwithNavigator.pushReplacement. Provide a specific UX scenario wherepushReplacementis strictly required.
14. FAQs
Q: IsNavigator.push the only way to route in modern Flutter?
A: Flutter recently introduced Navigator 2.0 (Router API), which is highly complex but necessary for advanced deep-linking and web browser URL bars. To make it easier, the community uses packages like go_router for modern apps, but you must understand the basic push/pop stack taught here first!
15. Summary
In Chapter 10, our app expanded into a multi-dimensional experience. We mastered the Navigator class, understanding its stack-based (plate-stacking) architecture. We usedNavigator.push to slide new screens into view and Navigator.pop to dismiss them. We learned how to inject dynamic data into screen constructors via MaterialPageRoute, and architected a clean, scalable URL system using Named Routes.