text stringlengths 1 372 |
|---|
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( |
onPressed: () { |
// pop here with "nope"... |
}, |
child: const Text('Nope.'), |
), |
) |
], |
), |
), |
); |
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
4. when a button is tapped, close the selection screen |
now, update the onPressed() callback for both of the buttons. |
to return data to the first screen, |
use the navigator.pop() method, |
which accepts an optional second argument called result. |
any result is returned to the future in the SelectionButton. |
<topic_end> |
<topic_start> |
yep button |
<code_start> |
ElevatedButton( |
onPressed: () { |
// close the screen and return "yep!" as the result. |
navigator.pop(context, 'yep!'); |
}, |
child: const Text('Yep!'), |
) |
<code_end> |
<topic_end> |
<topic_start> |
nope button |
<code_start> |
ElevatedButton( |
onPressed: () { |
// close the screen and return "nope." as the result. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.