Skip to content

Instantly share code, notes, and snippets.

@iampato
Created May 24, 2020 15:43
Show Gist options
  • Save iampato/119b81942aef817838e7d194a0382096 to your computer and use it in GitHub Desktop.
Save iampato/119b81942aef817838e7d194a0382096 to your computer and use it in GitHub Desktop.
Navigating using scaffold key
class FirstPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Material(
child: Center(child: Text("First Page")),
);
}
}
class MyHomePage extends StatelessWidget {
final GlobalKey<ScaffoldState> _scaffoldkey = new GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldkey,
appBar: AppBar(
title: Text("Example"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text("Go to 1st"),
onPressed: () => Navigator.push(
_scaffoldkey.currentContext,
MaterialPageRoute(
builder: (_) => FirstPage(),
),
),
),
RaisedButton(
child: Text("Go to 2st"),
onPressed: () => Navigator.push(
_scaffoldkey.currentContext,
MaterialPageRoute(
builder: (_) => SecondPage(),
),
),
),
],
),
),
);
}
}
class SecondPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Material(
child: Center(child: Text("Second Page")),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment