text stringlengths 1 372 |
|---|
navigator.pop(context, 'nope.'); |
}, |
child: const Text('Nope.'), |
) |
<code_end> |
<topic_end> |
<topic_start> |
5. show a snackbar on the home screen with the selection |
now that you’re launching a selection screen and awaiting the result, |
you’ll want to do something with the information that’s returned. |
in this case, show a snackbar displaying the result by using the |
_navigateAndDisplaySelection() method in SelectionButton: |
<code_start> |
// a method that launches the SelectionScreen and awaits the result from |
// navigator.pop. |
future<void> _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) => const SelectionScreen()), |
); |
// when a BuildContext is used from a StatefulWidget, the mounted property |
// must be checked after an asynchronous gap. |
if (!context.mounted) return; |
// after the selection screen returns a result, hide any previous snackbars |
// and show the new result. |
ScaffoldMessenger.of(context) |
..removeCurrentSnackBar() |
..showSnackBar(SnackBar(content: text('$result'))); |
} |
<code_end> |
<topic_end> |
<topic_start> |
interactive example |
<code_start> |
import 'package:flutter/material.dart'; |
void main() { |
runApp( |
const MaterialApp( |
title: 'returning data', |
home: HomeScreen(), |
), |
); |
} |
class HomeScreen extends StatelessWidget { |
const HomeScreen({super.key}); |
@override |
widget build(BuildContext context) { |
return scaffold( |
appBar: AppBar( |
title: const Text('Returning data demo'), |
), |
body: const center( |
child: SelectionButton(), |
), |
); |
} |
} |
class SelectionButton extends StatefulWidget { |
const SelectionButton({super.key}); |
@override |
State<SelectionButton> createState() => _SelectionButtonState(); |
} |
class _SelectionButtonState extends State<SelectionButton> { |
@override |
widget build(BuildContext context) { |
return ElevatedButton( |
onPressed: () { |
_navigateAndDisplaySelection(context); |
}, |
child: const Text('Pick an option, any option!'), |
); |
} |
// a method that launches the SelectionScreen and awaits the result from |
// navigator.pop. |
future<void> _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) => const SelectionScreen()), |
); |
// when a BuildContext is used from a StatefulWidget, the mounted property |
// must be checked after an asynchronous gap. |
if (!context.mounted) return; |
// after the selection screen returns a result, hide any previous snackbars |
// and show the new result. |
ScaffoldMessenger.of(context) |
..removeCurrentSnackBar() |
..showSnackBar(SnackBar(content: text('$result'))); |
} |
} |
class SelectionScreen extends StatelessWidget { |
const SelectionScreen({super.key}); |
@override |
widget build(BuildContext context) { |
return scaffold( |
appBar: AppBar( |
title: const Text('Pick an option'), |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.