Interaction with UI is an essential part of any application. During the same, there might raise a need to return data from the screen. This kind of interaction can range from selecting an option to navigating to different routes through the various buttons in the UI. In this article, we will explore the process of returning data from a screen in a Flutter application.
In Flutter, it can be done using the Navigator.pop() method. We will try this by implementing a simple application. To do so follow the below steps:
- Add a home screen
- Add a button to launch the selection screen
- Display the options
- Transition to the home screen after option selection
- Display the selection on the home screen
Let’s build a simple app now:
Create a home screen:
We will need a home screen with a button. The button when tapped should load to an options screen. It can be done like shown below:
Adding the launch button and selection screen:
To create a launch button to the launch the selection screen and add the selection screen use the following:
Displaying the options:
We will have 2 options for the sake of simplicity and have data associate with both of them which when tapped can be returned to the home screen.
Adding data to the options:
We will attach a onPressed() callback to both option 1 and option 2 which returns the data associated with each of them as shown below:
Displaying the selection:
We will use snackbar by using the _navigateAndDisplaySelection() method in SelectionButton:
Complete Source Code:
- Flutter - Send Data to Screen
- Flutter - Send Data to Screen using RouteSettings
- Splash Screen in Flutter
- Flutter - Animated Splash Screen
- Retrieve Data From TextFields in Flutter
- Flutter - Updating Data on the Internet
- Flutter - Fetching Data From the Internet
- Flutter - Deleting Data On The Internet
- Flutter - Sending Data To The Internet
- Flutter - Read and Write Data on Firebase
- Android | Creating a Splash Screen
- Android | Creating multiple Screen app
- How to scale different Views to all screen sizes in Android Studio?
- Screen Orientations in Android with Examples
- Screen Pinning
- Locking Screen Orientation in Android
- How to Keep the Device Screen On in Android?
- How to Increase/Decrease Screen Brightness in Steps Programmatically in Android?
- How to Increase/Decrease Screen Brightness using Volume Keys Programmatically in Android?
- How to Detect Touch Event on Screen Programmatically in Android?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.