Navigate to a new screen and back : Navigate to the second route using Navigator.push()

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

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

// Within the `FirstRoute` widget:

onPressed: () {

 Navigator.push(

  context,

  MaterialPageRoute(builder: (context) => const SecondRoute()),

 );

}

Leave a comment

Your email address will not be published. Required fields are marked *