In theNavigate to a new screen and backrecipe, we learned how to Navigate to a new screen by creating a new route andpushing it to theNavigator.

However, if we need to navigate to the same screen in many parts of our apps,this can result in code duplication. In these cases, it can be handy to definea “named route,” and use the named route for Navigation.

To work with named routes, we can use theNavigator.pushNamedfunction. This example will replicate the functionality from the originalrecipe, demonstrating how to use named routes instead.

Directions

  • Create two screens
  • Define the routes
  • Navigate to the second screen using Navigator.pushNamed
  • Return to the first screen using Navigator.pop

1. Create two screens

First, we’ll need two screens to work with. The first screen will contain abutton that navigates to the second screen. The second screen will contain abutton that navigates back to the first.

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

2. Define the routes

Next, we’ll need to define our routes by providing additional properties to theMaterialAppconstructor: the initialRoute and the routes themselves.

The initialRoute property defines which route our app should start with. Theroutes property defines the available named routes and the Widgets that shouldbe built when we navigate to those routes.

  1. MaterialApp(
  2. // Start the app with the "/" named route. In our case, the app will start
  3. // on the FirstScreen Widget
  4. initialRoute: '/',
  5. routes: {
  6. // When we navigate to the "/" route, build the FirstScreen Widget
  7. '/': (context) => FirstScreen(),
  8. // When we navigate to the "/second" route, build the SecondScreen Widget
  9. '/second': (context) => SecondScreen(),
  10. },
  11. );

Note: When using initialRoute, be sure you do not define a home property.

3. Navigate to the second screen

With our Widgets and routes in place, we can begin navigating! In this case,we’ll use theNavigator.pushNamedfunction. This tells Flutter to build the Widget defined in our routes tableand launch the screen.

In the build method of our FirstScreen Widget, we’ll update the onPressedcallback:

  1. // Within the `FirstScreen` Widget
  2. onPressed: () {
  3. // Navigate to the second screen using a named route
  4. Navigator.pushNamed(context, '/second');
  5. }

4. Return to the first screen

In order to navigate back to the first page, we can use theNavigator.popfunction.

  1. // Within the SecondScreen Widget
  2. onPressed: () {
  3. // Navigate back to the first screen by popping the current route
  4. // off the stack
  5. Navigator.pop(context);
  6. }

Complete example

  1. import 'package:flutter/material.dart';
  2. void main() {
  3. runApp(MaterialApp(
  4. title: 'Named Routes Demo',
  5. // Start the app with the "/" named route. In our case, the app will start
  6. // on the FirstScreen Widget
  7. initialRoute: '/',
  8. routes: {
  9. // When we navigate to the "/" route, build the FirstScreen Widget
  10. '/': (context) => FirstScreen(),
  11. // When we navigate to the "/second" route, build the SecondScreen Widget
  12. '/second': (context) => SecondScreen(),
  13. },
  14. ));
  15. }
  16. class FirstScreen extends StatelessWidget {
  17. @override
  18. Widget build(BuildContext context) {
  19. return Scaffold(
  20. appBar: AppBar(
  21. title: Text('First Screen'),
  22. ),
  23. body: Center(
  24. child: RaisedButton(
  25. child: Text('Launch screen'),
  26. onPressed: () {
  27. // Navigate to the second screen using a named route
  28. Navigator.pushNamed(context, '/second');
  29. },
  30. ),
  31. ),
  32. );
  33. }
  34. }
  35. class SecondScreen extends StatelessWidget {
  36. @override
  37. Widget build(BuildContext context) {
  38. return Scaffold(
  39. appBar: AppBar(
  40. title: Text("Second Screen"),
  41. ),
  42. body: Center(
  43. child: RaisedButton(
  44. onPressed: () {
  45. // Navigate back to the first screen by popping the current route
  46. // off the stack
  47. Navigator.pop(context);
  48. },
  49. child: Text('Go back!'),
  50. ),
  51. ),
  52. );
  53. }
  54. }

Navigation Basics Demo