Skip to content

Instantly share code, notes, and snippets.

@cvr
Last active May 8, 2020 09:11
Show Gist options
  • Save cvr/72c08aaab4d4b518de69e13de3c123c2 to your computer and use it in GitHub Desktop.
Save cvr/72c08aaab4d4b518de69e13de3c123c2 to your computer and use it in GitHub Desktop.
A Dart/Flutter example of an Index page allowing navigation to three screens
// Run this in https://dartpad.dartlang.org/flutter
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
title: 'Navigation Basics',
home: MainRoute(),
));
}
class MainRoute extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Index"),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
//crossAxisAlignment: CrossAxisAlignment.center,
//mainAxisSize: MainAxisSize.max,
//mainAxisAlignment: MainAxisAlignment.center,
//mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Expanded(
flex: 1, // 1/3 of space
child: Container(
child: RaisedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => FirstRoute()),
);
},
child: Text('Goto 1'),
color: Colors.blueGrey[50],
),
),
),
Expanded(
flex: 1, // 1/3 of space
child: Container(
child: FlatButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
},
child: Text('Goto 2'),
color: Colors.blueGrey[50],
),
),
),
Expanded(
flex: 1, // 1/3 of space
child: Container(
child: FlatButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ThirdRoute()),
);
},
child: Text('Goto 3'),
color: Colors.blueGrey[50],
),
),
),
],
),
);
}
}
class FirstRoute extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('First Route'),
),
body: Center(
child: RaisedButton(
child: Text('Goto 2'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
},
),
),
);
}
}
class SecondRoute extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Second Route"),
),
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ThirdRoute()),
);
},
child: Text('Goto 3'),
),
),
);
}
}
class ThirdRoute extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Third Route"),
),
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MainRoute()),
);
},
child: Text('Go back!'),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment