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 createState() => _SelectionButtonState(); } class _SelectionButtonState extends State { @override Widget build(BuildContext context) { return ElevatedButton( onPressed: () { _navigateAndDisplaySelection(context); }, child: const Text('Pick an option, any option!'), ); } Future _navigateAndDisplaySelection(BuildContext context) async { final result = await Navigator.push( context, MaterialPageRoute(builder: (context) => const SelectionScreen()), ); if (!context.mounted) return; if (result == null) return; 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')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Padding( padding: const EdgeInsets.all(8), child: ElevatedButton( onPressed: () { Navigator.pop(context, 'Yep!'); }, child: const Text('Yep!'), ), ), Padding( padding: const EdgeInsets.all(8), child: ElevatedButton( onPressed: () { Navigator.pop(context, 'Nope.'); }, child: const Text('Nope.'), ), ), ], ), ), ); } }