Skip to main content
Flutter Basics – Complete Beginner to Advanced Guide
CHAPTER 10 Beginner

Navigation and Routing in Flutter

Updated: May 16, 2026
25 min read

# CHAPTER 10

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 Routes for cleaner architecture.
  • Pass variables and data between different screens.

3. The Navigator Stack

Imagine a stack of plates.
  • The Home Screen is the first plate.
  • When the user clicks "Settings", Flutter drops the Settings Screen plate on top of the Home Screen. The user now sees Settings.
  • When the user clicks the "Back" arrow, Flutter throws the top plate away, revealing the Home Screen underneath it.
This concept is managed by the 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
12345678910
ElevatedButton(
  child: Text("Go to Details Screen"),
  onPressed: () {
    // 1. Push a new Route onto the Navigator stack
    Navigator.push(
      context, // The context tells Flutter where we currently are in the app
      MaterialPageRoute(builder: (context) => DetailsScreen()), // The new screen to load!
    );
  },
)

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
1234567
ElevatedButton(
  child: Text("Go Back"),
  onPressed: () {
    // 2. Pop the top screen off the stack
    Navigator.pop(context);
  },
)

5. Passing Data Between Screens

If a user taps an image of a red shoe, the DetailsScreen 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
1234567891011
class DetailsScreen extends StatelessWidget {
  final String productName; // The data we need

  // Constructor requiring the data
  DetailsScreen({required this.productName});

  @override
  Widget build(BuildContext context) {
    return Scaffold(body: Text("Details for: $productName"));
  }
}

Screen A (Sending the Data):

dart
12345
Navigator.push(
  context,
  // Pass the string "Red Shoe" into the constructor!
  MaterialPageRoute(builder: (context) => DetailsScreen(productName: "Red Shoe")),
);

6. Named Routes (Advanced Architecture)

If you have 50 screens in your app, typing out MaterialPageRoute everywhere becomes messy. Professional apps use Named Routes. You define a dictionary of URLs in your MaterialApp!
dart
123456789101112
// 1. Define the routes in MaterialApp
MaterialApp(
  initialRoute: '/', // The starting screen
  routes: {
    '/': (context) => HomeScreen(),
    '/settings': (context) => SettingsScreen(),
    '/profile': (context) => ProfileScreen(),
  },
)

// 2. Navigate effortlessly from anywhere in the app!
Navigator.pushNamed(context, '/settings');

7. Visual Learning: The Route Stack

txt
12345678
Action: User opens app.
Stack: [ Home ] -> User sees Home.

Action: User clicks "Profile". (Navigator.push)
Stack: [ Home, Profile ] -> User sees Profile.

Action: User clicks "Back". (Navigator.pop)
Stack: [ Home ] -> Profile is destroyed. User sees Home again.

8. Common Mistakes

  • The Context Error: If you try to use Navigator.push(context, ...) directly inside the runApp or MaterialApp widget before a Scaffold is built, Flutter will crash with an error saying *"Navigator operation requested with a context that does not include a Navigator."* The context must come from a widget *inside* or *below* the MaterialApp!

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. 1. Create HomeScreen (StatelessWidget). Add a TextField (we will cover forms later, so just hardcode a string String userName = "Alice"; for now).
  1. 2. Add an ElevatedButton. In the onPressed, write Navigator.push.
  1. 3. Create a second screen called WelcomeScreen. Ensure its constructor requires a String name;.
  1. 4. In WelcomeScreen's Scaffold body, display a large Text("Welcome, $name!").
  1. 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. 1. What class manages the stack of screens in a Flutter application?
  1. 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.push with Navigator.pushReplacement. Provide a specific UX scenario where pushReplacement is strictly required.

14. FAQs

Q: Is Navigator.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 used Navigator.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.

16. Next Chapter Recommendation

Our screens are connected, but users need a way to type in their names, passwords, and search queries. Proceed to Chapter 11: Forms and User Input.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·