In this tutorial, you will learn how to navigate from one screen to another using a button click in Flutter. For this purpose we will follow these steps:
- Create a new Flutter app
- Create the UI
- Create the navigation
2. Create the UI
We are going to create a very simple screen with only one button in the middle. Below is a code example that does just that.
As for the second screen, we will use the same layout but will change colors. This will help us see if the navigation is working.
Below is a code example for screen 2.
2. Creating the Navigation
In Flutter there is a built-in method for navigation between screens, called “Navigator“. To switch to a new screen, we will use the “Navigator.push” method. The push() method adds a screen to the stack of routes managed by the Navigator. we will create our own route using the MaterialPageRoute, which is useful because it transitions to the new route using a platform-specific animation. Here is the sample code for the navigation:
As you can see in the “onPressed” property we have a function( Navigator.of(context).push(“MaterialPageRoute(builder: (context)=>Screen2()));“) that will take us to the desired screen. Here is the code for the whole app:
0 Comments
Don't Spam In Comments