Created
January 13, 2020 22:52
-
-
Save zbarbuto/22aaa011c7f0cd40e9bd5e8fec2e4828 to your computer and use it in GitHub Desktop.
Flutter Drawer Routes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
title: 'Flutter Nav Demo 3', | |
theme: new ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: new MyHomePage(), | |
onGenerateRoute: (settings) { | |
if (settings.name == "/home") { | |
return PageRouteBuilder( | |
pageBuilder: (_, __, ___) => MyHomePage(), | |
transitionsBuilder: (_, anim, __, child) { | |
return FadeTransition(opacity: anim, child: child); | |
}, | |
); | |
} | |
if (settings.name == "/about") { | |
return PageRouteBuilder( | |
pageBuilder: (_, __, ___) => AboutPage(), | |
transitionsBuilder: (_, anim, __, child) { | |
return FadeTransition(opacity: anim, child: child); | |
}, | |
); | |
} | |
if (settings.name == "/contact") { | |
return PageRouteBuilder( | |
pageBuilder: (_, __, ___) => ContactPage(), | |
transitionsBuilder: (_, anim, __, child) { | |
return FadeTransition(opacity: anim, child: child); | |
}, | |
); | |
} | |
}, | |
); | |
} | |
} | |
class AboutPage extends StatefulWidget { | |
@override | |
_AboutPageState createState() => new _AboutPageState(); | |
} | |
class _AboutPageState extends State<AboutPage> { | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text('About'), | |
backgroundColor: Colors.deepOrangeAccent, | |
), | |
drawer: MyDrawer(), | |
body: new Center( | |
child: new Text("About Page", style: new TextStyle(fontSize: 35.0)), | |
), | |
); | |
} | |
} | |
class ContactPage extends StatefulWidget { | |
@override | |
_ContactPageState createState() => new _ContactPageState(); | |
} | |
class _ContactPageState extends State<ContactPage> { | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text('Contact'), | |
backgroundColor: Colors.deepOrangeAccent, | |
), | |
drawer: MyDrawer(), | |
body: new Center( | |
child: new Text("Contact Page", style: new TextStyle(fontSize: 35.0)), | |
), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
@override | |
_MyHomePageState createState() => new _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text("Navigation Demo"), | |
backgroundColor: Colors.deepOrangeAccent, | |
), | |
drawer: MyDrawer(), | |
body: new Center( | |
child: new Text("Home Page", style: new TextStyle(fontSize: 35.0)), | |
)); | |
} | |
} | |
class MyDrawer extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new Drawer( | |
child: new ListView( | |
children: <Widget>[ | |
new ListTile( | |
title: new Text("WELCOME"), | |
), | |
new Divider(), | |
new ListTile( | |
title: new Text("About"), | |
trailing: new Icon(Icons.info), | |
onTap: () { | |
Navigator.of(context).pop(); | |
Navigator.of(context).pushReplacementNamed('/about'); | |
}), | |
new ListTile( | |
title: new Text("Contact"), | |
trailing: new Icon(Icons.phone), | |
onTap: () { | |
Navigator.of(context).pop(); | |
Navigator.of(context).pushReplacementNamed('/contact'); | |
}), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment