Created
July 25, 2020 23:49
-
-
Save Nailik/a48f39b2745b470a1974338cd6b2d4f8 to your computer and use it in GitHub Desktop.
Return data from a screen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MaterialApp( | |
title: 'Returning Data', | |
home: HomeScreen(), | |
)); | |
} | |
class HomeScreen extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Returning Data Demo'), | |
), | |
body: Center(child: SelectionButton()), | |
); | |
} | |
} | |
class SelectionButton extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return RaisedButton( | |
onPressed: () { | |
_navigateAndDisplaySelection(context); | |
}, | |
child: Text('Pick an option, any option!'), | |
); | |
} | |
// A method that launches the SelectionScreen and awaits the result from | |
// Navigator.pop. | |
_navigateAndDisplaySelection(BuildContext context) async { | |
// Navigator.push returns a Future that completes after calling | |
// Navigator.pop on the Selection Screen. | |
final result = await Navigator.push( | |
context, | |
MaterialPageRoute(builder: (context) => SelectionScreen()), | |
); | |
// After the Selection Screen returns a result, hide any previous snackbars | |
// and show the new result. | |
Scaffold.of(context) | |
..removeCurrentSnackBar() | |
..showSnackBar(SnackBar(content: Text("$result"))); | |
} | |
} | |
class SelectionScreen extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Pick an option'), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: RaisedButton( | |
onPressed: () { | |
// Close the screen and return "Yep!" as the result. | |
Navigator.pop(context, 'Yep!'); | |
}, | |
child: Text('Yep!'), | |
), | |
), | |
Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: RaisedButton( | |
onPressed: () { | |
// Close the screen and return "Nope!" as the result. | |
Navigator.pop(context, 'Nope.'); | |
}, | |
child: Text('Nope.'), | |
), | |
) | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment