Basic Page Navigation
Learn to push and pop routes using Navigator, enabling simple transitions between different screens in your Flutter app.
Basic Page Navigation is a free Flutter Mobile Development lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flutter Mobile Development learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Navigating Your App
Think of a mobile app as a collection of pages or "screens." Navigation is how users move between these screens, like going from a product list to a product detail page.
- Apps need a clear way to move forward.
- Apps also need a way to go back.
- Flutter provides powerful tools for this.
Meet the Navigator
In Flutter, the Navigator widget manages a stack of Route objects. When you want to show a new screen, you "push" a route onto this stack. When you want to go back, you "pop" a route off the stack.
- It's like a stack of playing cards.
- New cards go on top (push).
- You remove cards from the top (pop).
Understanding Routes
A Route is an abstraction for a "screen" or "page" in your app. Flutter provides a common type of route called MaterialPageRoute, which creates a platform-specific transition animation (like sliding in from the right on Android).
It's the blueprint for how a new screen will look and behave when shown.
Moving Forward with push()
To navigate to a new screen, you use Navigator.push(). This method takes a BuildContext and a MaterialPageRoute as arguments.
The builder function inside MaterialPageRoute tells Flutter which widget to build for the new screen.
Try Pushing a Route
This example shows how to push a new screen when a button is pressed. Run it and tap the button!
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home Screen')),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()),
);
},
child: const Text('Go to Second Screen'),
),
),
);
}
}
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Second Screen')),
body: const Center(
child: Text('You are on the second screen!'),
),
);
}
}What is BuildContext?
You might have noticed context being passed around. BuildContext is a handle to the location of a widget in the widget tree. It helps widgets locate other widgets or data higher up in the tree.
The Navigator uses this context to know which navigation stack to operate on.
Going Back with pop()
When you want to return to the previous screen, you "pop" the current route off the navigation stack using Navigator.pop(). This will reveal the screen that was underneath.
Flutter's AppBar often provides a back button automatically when a new route is pushed, which internally calls Navigator.pop().
Try Popping a Route
Let's modify our previous example to include a button on the second screen that allows us to explicitly go back to the home screen.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home Screen')),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()),
);
},
child: const Text('Go to Second Screen'),
),
),
);
}
}
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Second Screen')),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pop(context); // This will go back!
},
child: const Text('Go Back'),
),
),
);
}
}Visualizing the Stack
Imagine the Navigator maintains a stack of screens. When you push, a new screen is placed on top. When you pop, the top screen is removed.
The user always sees the topmost screen. This stack behavior is fundamental to how navigation works in Flutter.
- Push: Adds to top.
- Pop: Removes from top.
- First screen is at the bottom.
Navigation Check
Consider a Flutter app with two screens, ScreenA and ScreenB. If you are currently on ScreenA and want to move to ScreenB, then later return to ScreenA, which sequence of Navigator methods would you use?
Recap: Basic Navigation
Great job! You've learned the core concepts of basic navigation in Flutter:
- The
Navigatormanages a stack of routes. MaterialPageRoutedefines a new screen.Navigator.push()adds a new screen to the stack.Navigator.pop()removes the current screen from the stack.BuildContextis essential for navigation operations.
Next, we'll explore more advanced navigation techniques like named routes!
Frequently asked questions
Is the “Basic Page Navigation” lesson free?
Yes — the full text of “Basic Page Navigation” is free to read here on the web, and the Flutter Mobile Development course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flutter Mobile Development course, upgrade to CoddyKit PRO.
What will I learn in “Basic Page Navigation”?
Learn to push and pop routes using Navigator, enabling simple transitions between different screens in your Flutter app. You practise Flutter Mobile Development with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flutter Mobile Development?
No prior experience is required. Flutter Mobile Development on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Basic Page Navigation” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flutter Mobile Development lesson?
Yes. Every Flutter Mobile Development lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Basic Page Navigation
- Named Routes & Arguments
- TabBars & Drawers
- Deep Linking & URL Navigation