text stringlengths 1 474 |
|---|
final todo = ModalRoute.of(context)!.settings.arguments as Todo; |
// Use the Todo to create the UI. |
return Scaffold( |
appBar: AppBar( |
title: Text(todo.title), |
), |
body: Padding( |
padding: const EdgeInsets.all(16), |
child: Text(todo.description), |
), |
); |
} |
}<code_end> |
<topic_end> |
<topic_start>Return data from a screen |
In some cases, you might want to return data from a new screen. |
For example, say you push a new screen that presents two options to a user. |
When the user taps an option, you want to inform the first screen |
of the user’s selection so that it can act on that information.You can do this with the Navigator.pop() |
method using the following steps:<topic_end> |
<topic_start> |
1. Define the home screen |
The home screen displays a button. When tapped, |
it launches the selection screen. |
<code_start>class HomeScreen extends StatelessWidget { |
const HomeScreen({super.key}); |
@override |
Widget build(BuildContext context) { |
return Scaffold( |
appBar: AppBar( |
title: const Text('Returning Data Demo'), |
), |
// Create the SelectionButton widget in the next step. |
body: const Center( |
child: SelectionButton(), |
), |
); |
} |
}<code_end> |
<topic_end> |
<topic_start> |
2. Add a button that launches the selection screen |
Now, create the SelectionButton, which does the following: |
<code_start>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!'), |
); |
} |
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, |
// Create the SelectionScreen in the next step. |
MaterialPageRoute(builder: (context) => const SelectionScreen()), |
); |
} |
}<code_end> |
<topic_end> |
<topic_start> |
3. Show the selection screen with two buttons |
Now, build a selection screen that contains two buttons. |
When a user taps a button, |
that app closes the selection screen and lets the home |
screen know which button was tapped.This step defines the UI. |
The next step adds code to return data. |
<code_start>class SelectionScreen extends StatelessWidget { |
const SelectionScreen({super.key}); |
@override |
Widget build(BuildContext context) { |
return Scaffold( |
appBar: AppBar( |
title: const Text('Pick an option'), |
), |
body: Center( |
child: Column( |
mainAxisAlignment: MainAxisAlignment.center, |
children: [ |
Padding( |
padding: const EdgeInsets.all(8), |
child: ElevatedButton( |
onPressed: () { |
// Pop here with "Yep"... |
}, |
child: const Text('Yep!'), |
), |
), |
Padding( |
padding: const EdgeInsets.all(8), |
child: ElevatedButton( |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.