Skip to content

Instantly share code, notes, and snippets.

@Andrious
Last active January 6, 2021 01:51
Show Gist options
  • Save Andrious/deeeb26424a41652d4bfccfd4bdc4406 to your computer and use it in GitHub Desktop.
Save Andrious/deeeb26424a41652d4bfccfd4bdc4406 to your computer and use it in GitHub Desktop.
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