Flutter Navigate to Another Activity Screen on Button Click Example

Flutter comes with inbuilt method named as Navigator.push() which is used to navigate between multiple screens in flutter mobile application arena. In mobile application screens are also known as Routes or Activities. The navigator class in flutter is used to manage all the routes present in current application and index them one by one so they can be accessed easily via Parent. The screens can be called directly via Class name. There is no need to manage them individually via app developer, this work will automatically done by Flutter. So in this tutorial we would create project in Flutter Navigate to Another Activity Screen on Button Click Example Tutorial. This functionality is also known as Redirect to another Route Screen in Flutter Android iOS mobile app.

Contents in this project Flutter Navigate to Another Activity Screen on Button Click Android iOS Example Tutorial:

1. Import material.dart package in your app’s main.dart file.

2. Create our main void main() runApp() method and call our Home Screen activity class here using home parameter. The void main() method will always calls first every time when flutter app runs so our HomeScreen() class will be our main home screen of app.

3. Create a class named as HomeScreen extends StatelessWidget. This would be our first screen of app.

4. Create a function named as gotoSecondActivity() with Build Context parameter in HomeScreen class. Inside this function we would call the Navigator.push() method and pass our next Screen class name. We would class this function on button onPress event. So when user press the button it will redirect to given screen class.

5. Create 1 Raised button in widget build area in HomeScreen class and call the gotoSecondActivity() function on button onPressed event.

6. Create our second screen class named as SecondScreen extends StatelessWidget. This would be our next activity of application.

7. Create a function named as goBackToPreviousScreen() with Context parameter in SecondScreen class. In this function we would call the Navigator.pop(context) inbuilt method. This method would kill the current screen and navigate to previous screen.

8. Create 1 Raised button in Widget build area of SecondScreen class. We would call the goBackToPreviousScreen() method on button onPressed event.

9. Complete source code for main.dart file:

Screenshot:

Flutter Navigate to Another Activity Screen on Button Click Android iOS

Leave a Reply

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