Most apps contain several screens for displaying different types of information.For example, an app might have a screen that displays products. Users can thentap the image of a product to get more detailed information on a new screen.

Terminology: In Flutter, screens and pages are called routes. The remainder of this doc refers to routes.

In Android, a route is equivalent to an Activity.In iOS, a route is equivalent to a ViewController.In Flutter, a route is just a widget.

How do you navigate to a new route? By using theNavigator.

Directions

The next few sections show how to navigate between two routes,using these steps:

  • Create two routes
  • Navigate to the second route using Navigator.push()
  • Return to the first route using Navigator.pop()

1. Create two routes

First, create two routes to work with. Since this is a basic example,each route contains only a single button. Tapping the button on thefirst route navigates to the second route. Tapping the button on thesecond route returns to the first route.

First, set up the visual structure:

  1. class FirstRoute extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return Scaffold(
  5. appBar: AppBar(
  6. title: Text('First Route'),
  7. ),
  8. body: Center(
  9. child: RaisedButton(
  10. child: Text('Open route'),
  11. onPressed: () {
  12. // Navigate to second route when tapped.
  13. },
  14. ),
  15. ),
  16. );
  17. }
  18. }
  19. class SecondRoute extends StatelessWidget {
  20. @override
  21. Widget build(BuildContext context) {
  22. return Scaffold(
  23. appBar: AppBar(
  24. title: Text("Second Route"),
  25. ),
  26. body: Center(
  27. child: RaisedButton(
  28. onPressed: () {
  29. // Navigate back to first route when tapped.
  30. },
  31. child: Text('Go back!'),
  32. ),
  33. ),
  34. );
  35. }
  36. }

2. Navigate to the second route using Navigator.push()

To switch to a new route, use theNavigator.push()method. The push() method adds a Route to the stack of routes managed bythe Navigator. Where does the Route come from?You can create your own, or use aMaterialPageRoute,out of the box. MaterialPageRoute is handy because it transitions to thenew route using a platform-specific animation.

In the build() method of the FirstRoute widget, update the onPressed()callback:

  1. // Within the `FirstRoute` widget
  2. onPressed: () {
  3. Navigator.push(
  4. context,
  5. MaterialPageRoute(builder: (context) => SecondRoute()),
  6. );
  7. }

3. Return to the first route using Navigator.pop()

How do you close the second route and return to the first? By using theNavigator.pop()method. The pop() method removes the current Route from the stack ofroutes managed by the navigator.

To implement a return to the original route, update the onPressed()callback in the SecondRoute widget:

  1. // Within the SecondRoute widget
  2. onPressed: () {
  3. Navigator.pop(context);
  4. }

Complete example

  1. import 'package:flutter/material.dart';
  2. void main() {
  3. runApp(MaterialApp(
  4. title: 'Navigation Basics',
  5. home: FirstRoute(),
  6. ));
  7. }
  8. class FirstRoute extends StatelessWidget {
  9. @override
  10. Widget build(BuildContext context) {
  11. return Scaffold(
  12. appBar: AppBar(
  13. title: Text('First Route'),
  14. ),
  15. body: Center(
  16. child: RaisedButton(
  17. child: Text('Open route'),
  18. onPressed: () {
  19. Navigator.push(
  20. context,
  21. MaterialPageRoute(builder: (context) => SecondRoute()),
  22. );
  23. },
  24. ),
  25. ),
  26. );
  27. }
  28. }
  29. class SecondRoute extends StatelessWidget {
  30. @override
  31. Widget build(BuildContext context) {
  32. return Scaffold(
  33. appBar: AppBar(
  34. title: Text("Second Route"),
  35. ),
  36. body: Center(
  37. child: RaisedButton(
  38. onPressed: () {
  39. Navigator.pop(context);
  40. },
  41. child: Text('Go back!'),
  42. ),
  43. ),
  44. );
  45. }
  46. }

Navigation Basics Demo