Instantly share code, notes, and snippets.
Andrious/navigate_two_routes.dart
Last active Jan 6, 2021
Demonstration of Flutter's Naviagtion system
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), | |
), | |
) | |
], | |
), | |
), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment