|
import 'package:flutter/material.dart'; |
|
|
|
void main() => runApp(MaterialApp( |
|
title: 'Navigation Basics', |
|
home: FirstRoute(), |
|
)); |
|
|
|
class FirstRoute extends StatelessWidget { |
|
@override |
|
Widget build(BuildContext context) => Scaffold( |
|
appBar: AppBar( |
|
title: const Text('First Route'), |
|
), |
|
body: Builder( |
|
builder: (context) => Center( |
|
child: ElevatedButton( |
|
child: const Text('Open route'), |
|
onPressed: () async { |
|
// Navigator.push returns a Future that completes after calling |
|
// Navigator.pop on the SecondRoute Screen. |
|
var result = await Navigator.push<String>( |
|
context, |
|
MaterialPageRoute( |
|
builder: (context) => SecondRoute(), |
|
), |
|
); |
|
// After the SecondRoute Screen returns a result, hide any previous snackbars |
|
// and show the new result. |
|
Scaffold.of(context) |
|
..removeCurrentSnackBar() |
|
..showSnackBar(SnackBar(content: Text('$result'))); |
|
}, |
|
), |
|
), |
|
), |
|
); |
|
} |
|
|
|
class SecondRoute extends StatelessWidget { |
|
final String result01 = 'Yep!'; |
|
final String result02 = 'Nope!'; |
|
@override |
|
Widget build(BuildContext context) => Scaffold( |
|
appBar: AppBar( |
|
title: const Text('Second Route'), |
|
), |
|
body: Center( |
|
child: Column( |
|
mainAxisAlignment: MainAxisAlignment.center, |
|
children: [ |
|
Padding( |
|
padding: const EdgeInsets.all(8.0), |
|
child: ElevatedButton( |
|
onPressed: () { |
|
// Close the screen and return "Yep!" as the result. |
|
Navigator.maybePop(context, result01); |
|
}, |
|
child: Text(result01), |
|
), |
|
), |
|
Padding( |
|
padding: const EdgeInsets.all(8.0), |
|
child: ElevatedButton( |
|
onPressed: () { |
|
// Close the screen and return "Nope!" as the result. |
|
Navigator.maybePop(context, result02); |
|
}, |
|
child: Text(result02), |
|
), |
|
) |
|
], |
|
), |
|
), |
|
); |
|
} |