Skip to content

Instantly share code, notes, and snippets.

@Andrious
Created June 6, 2019 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Andrious/f88903b1ea9387bea7a4d2d09e9fc988 to your computer and use it in GitHub Desktop.
Save Andrious/f88903b1ea9387bea7a4d2d09e9fc988 to your computer and use it in GitHub Desktop.
Demonstrating the Drawers and Bottom Sheet for the Scaffold widget
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
MyApp({Key key}) : super(key: key);
State createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int _count = 0;
final GlobalKey keyScaffold = new GlobalKey();
String hasAppBar = '';
String hasDrawer = '';
String hasEndDrawer = '';
String hasFloatingActionButton = '';
String appBarMaxHeight = '';
String isDrawerOpen = '';
String isEndDrawerOpen = '';
bool hadDrawerOpened = false;
PersistentBottomSheetController<Null> sheet;
@override
Widget build(BuildContext context) {
final title = 'Floating App Bar';
final ThemeData theme = Theme.of(context);
return MaterialApp(
debugShowCheckedModeBanner: false,
title: title,
home: Scaffold(
key: keyScaffold,
appBar: AppBar(
title: Text('Sample Code'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('You have pressed the button $_count times.'),
RaisedButton(
onPressed: () {
ScaffoldState state = keyScaffold.currentState;
if (hadDrawerOpened) {
if (state.hasEndDrawer) state.openEndDrawer();
} else {
if (state.hasDrawer) state.openDrawer();
}
hadDrawerOpened = state.isDrawerOpen;
setState(() {});
sheet = state.showBottomSheet<Null>((BuildContext context) {
return Container(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Center(
child: Text(
'Opened ${hadDrawerOpened ? 'left' : 'right'}-hand Drawer!',
)),
],
));
});
},
elevation: 20.0,
textColor: theme.canvasColor,
color: theme.primaryColor,
child: Text('Open Drawers'),
),
],
)),
bottomNavigationBar: BottomAppBar(
child: Container(
height: 50.0,
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() {
_count++;
}),
tooltip: 'Increment Counter',
child: Icon(Icons.add),
),
floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
drawer: _drawer,
endDrawer: _drawer,
),
);
}
Widget get _drawer => Drawer(
// Add a ListView to the drawer. This ensures the user can scroll
// through the options in the Drawer if there isn't enough vertical
// space to fit everything.
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text('Drawer Header'),
decoration: BoxDecoration(
color: Colors.blue,
),
),
ListTile(
title: Text('Item 1'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
ListTile(
title: Text('Item 2'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
],
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment