Skip to content

Instantly share code, notes, and snippets.

@branflake2267
Last active May 7, 2021 03:36
Show Gist options
  • Save branflake2267/87e0a21eff8736a83646ef8c67c1ba6c to your computer and use it in GitHub Desktop.
Save branflake2267/87e0a21eff8736a83646ef8c67c1ba6c to your computer and use it in GitHub Desktop.
Flutter - Navigation Drawer Left or Right
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.brown,
),
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var _scaffoldKey = new GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return new Scaffold(
key: _scaffoldKey,
drawer: new AppDrawer(), // left side
//endDrawer: new AppDrawer(), // right side
appBar: new AppBar(
title: new Text("Title"),
),
body: new ListView(
children: <Widget>[
new ListTile(
title: new Text("Body"),
),
new ListTile(
title: new RaisedButton(
child: new Text("Open Drawer"),
onPressed: () {
_scaffoldKey.currentState.openDrawer(); // left side
//_scaffoldKey.currentState.openEndDrawer(); // right side
},
),
),
],
),
);
}
}
class AppDrawer extends StatefulWidget {
@override
_AppDrawerState createState() => new _AppDrawerState();
}
class _AppDrawerState extends State<AppDrawer> {
@override
Widget build(BuildContext context) {
return new Drawer(
child: new ListView(
children: <Widget>[
new DrawerHeader(
child: new Text("Header"),
),
new ListTile(
title: new Text("Item 1"),
),
],
),
);
}
}
@gautamchitnis
Copy link

Great gist. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment