blob: 54f6f35af73804723cf64f98dfe975e1f7e5e8c3 [file] [log] [blame]
Hans Mullerd05c7f62016-12-21 06:19:42 -08001// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'package:flutter/material.dart';
6
Sarah Zakariasa0e91bf2017-09-26 15:21:01 +02007const String _kAsset0 = 'shrine/vendors/zach.jpg';
8const String _kAsset1 = 'shrine/vendors/16c477b.jpg';
9const String _kAsset2 = 'shrine/vendors/sandra-adams.jpg';
10const String _kGalleryAssetsPackage = 'flutter_gallery_assets';
Hans Mullerd05c7f62016-12-21 06:19:42 -080011
12class DrawerDemo extends StatefulWidget {
xstera76c3522017-03-01 16:06:48 -080013 static const String routeName = '/material/drawer';
Hans Mullerd05c7f62016-12-21 06:19:42 -080014
15 @override
16 _DrawerDemoState createState() => new _DrawerDemoState();
17}
18
19class _DrawerDemoState extends State<DrawerDemo> with TickerProviderStateMixin {
20 final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
21
22 static const List<String> _drawerContents = const <String>[
23 'A', 'B', 'C', 'D', 'E',
24 ];
25
26 AnimationController _controller;
27 Animation<double> _drawerContentsOpacity;
Adam Barth6128f482017-10-02 23:30:14 -070028 Animation<Offset> _drawerDetailsPosition;
Hans Mullerd05c7f62016-12-21 06:19:42 -080029 bool _showDrawerContents = true;
30
31 @override
32 void initState() {
33 super.initState();
34 _controller = new AnimationController(
35 vsync: this,
36 duration: const Duration(milliseconds: 200),
37 );
38 _drawerContentsOpacity = new CurvedAnimation(
39 parent: new ReverseAnimation(_controller),
40 curve: Curves.fastOutSlowIn,
41 );
Adam Barth6128f482017-10-02 23:30:14 -070042 _drawerDetailsPosition = new Tween<Offset>(
43 begin: const Offset(0.0, -1.0),
44 end: Offset.zero,
Hans Mullerd05c7f62016-12-21 06:19:42 -080045 ).animate(new CurvedAnimation(
46 parent: _controller,
47 curve: Curves.fastOutSlowIn,
48 ));
49 }
50
51 @override
52 void dispose() {
53 _controller.dispose();
54 super.dispose();
55 }
56
57 IconData _backIcon() {
58 switch (Theme.of(context).platform) {
59 case TargetPlatform.android:
60 case TargetPlatform.fuchsia:
61 return Icons.arrow_back;
62 case TargetPlatform.iOS:
63 return Icons.arrow_back_ios;
64 }
65 assert(false);
66 return null;
67 }
68
69 void _showNotImplementedMessage() {
70 Navigator.of(context).pop(); // Dismiss the drawer.
Alexandre Ardhuin83fce212017-04-23 22:37:29 +020071 _scaffoldKey.currentState.showSnackBar(const SnackBar(
Ian Hickson3eb87832017-04-07 12:24:32 -070072 content: const Text("The drawer's items don't do anything")
Hans Mullerd05c7f62016-12-21 06:19:42 -080073 ));
74 }
75
76 @override
77 Widget build(BuildContext context) {
78 return new Scaffold(
79 key: _scaffoldKey,
80 appBar: new AppBar(
81 leading: new IconButton(
82 icon: new Icon(_backIcon()),
Adam Barth0044ea22017-10-02 00:06:24 -070083 alignment: Alignment.centerLeft,
Hans Mullerd05c7f62016-12-21 06:19:42 -080084 tooltip: 'Back',
85 onPressed: () {
86 Navigator.pop(context);
87 },
88 ),
Ian Hickson3eb87832017-04-07 12:24:32 -070089 title: const Text('Navigation drawer'),
Hans Mullerd05c7f62016-12-21 06:19:42 -080090 ),
91 drawer: new Drawer(
xster9db89662017-12-05 11:58:03 -080092 child: new Column(
Hans Mullerd05c7f62016-12-21 06:19:42 -080093 children: <Widget>[
94 new UserAccountsDrawerHeader(
Ian Hickson3eb87832017-04-07 12:24:32 -070095 accountName: const Text('Zach Widget'),
96 accountEmail: const Text('zach.widget@example.com'),
Sarah Zakariasa0e91bf2017-09-26 15:21:01 +020097 currentAccountPicture: const CircleAvatar(
98 backgroundImage: const AssetImage(
99 _kAsset0,
100 package: _kGalleryAssetsPackage,
101 ),
102 ),
Alexandre Ardhuinc02b6a82018-02-02 23:27:29 +0100103 otherAccountsPictures: <Widget>[
Michael Goderbauer94f48c22018-01-02 16:28:31 -0800104 new GestureDetector(
105 onTap: () {
106 _onOtherAccountsTap(context);
107 },
108 child: new Semantics(
109 label: 'Switch to Account B',
110 child: const CircleAvatar(
111 backgroundImage: const AssetImage(
112 _kAsset1,
113 package: _kGalleryAssetsPackage,
114 ),
115 ),
Sarah Zakariasa0e91bf2017-09-26 15:21:01 +0200116 ),
117 ),
Michael Goderbauer94f48c22018-01-02 16:28:31 -0800118 new GestureDetector(
119 onTap: () {
120 _onOtherAccountsTap(context);
121 },
122 child: new Semantics(
123 label: 'Switch to Account C',
124 child: const CircleAvatar(
125 backgroundImage: const AssetImage(
126 _kAsset2,
127 package: _kGalleryAssetsPackage,
128 ),
129 ),
Sarah Zakariasa0e91bf2017-09-26 15:21:01 +0200130 ),
131 ),
Hans Mullerd05c7f62016-12-21 06:19:42 -0800132 ],
xster9db89662017-12-05 11:58:03 -0800133 margin: EdgeInsets.zero,
Hans Mullerd05c7f62016-12-21 06:19:42 -0800134 onDetailsPressed: () {
135 _showDrawerContents = !_showDrawerContents;
136 if (_showDrawerContents)
137 _controller.reverse();
138 else
139 _controller.forward();
140 },
141 ),
xster9db89662017-12-05 11:58:03 -0800142 new MediaQuery.removePadding(
143 context: context,
144 // DrawerHeader consumes top MediaQuery padding.
145 removeTop: true,
146 child: new Expanded(
147 child: new ListView(
148 padding: const EdgeInsets.only(top: 8.0),
149 children: <Widget>[
150 new Stack(
151 children: <Widget>[
152 // The initial contents of the drawer.
153 new FadeTransition(
154 opacity: _drawerContentsOpacity,
155 child: new Column(
156 mainAxisSize: MainAxisSize.min,
157 crossAxisAlignment: CrossAxisAlignment.stretch,
158 children: _drawerContents.map((String id) {
159 return new ListTile(
160 leading: new CircleAvatar(child: new Text(id)),
161 title: new Text('Drawer item $id'),
162 onTap: _showNotImplementedMessage,
163 );
164 }).toList(),
Hans Mullerd05c7f62016-12-21 06:19:42 -0800165 ),
xster9db89662017-12-05 11:58:03 -0800166 ),
167 // The drawer's "details" view.
168 new SlideTransition(
169 position: _drawerDetailsPosition,
170 child: new FadeTransition(
171 opacity: new ReverseAnimation(_drawerContentsOpacity),
172 child: new Column(
173 mainAxisSize: MainAxisSize.min,
174 crossAxisAlignment: CrossAxisAlignment.stretch,
175 children: <Widget>[
176 new ListTile(
177 leading: const Icon(Icons.add),
178 title: const Text('Add account'),
179 onTap: _showNotImplementedMessage,
180 ),
181 new ListTile(
182 leading: const Icon(Icons.settings),
183 title: const Text('Manage accounts'),
184 onTap: _showNotImplementedMessage,
185 ),
186 ],
187 ),
Hans Mullerd05c7f62016-12-21 06:19:42 -0800188 ),
xster9db89662017-12-05 11:58:03 -0800189 ),
190 ],
Hans Mullerd05c7f62016-12-21 06:19:42 -0800191 ),
xster9db89662017-12-05 11:58:03 -0800192 ],
193 ),
Hans Mullerd05c7f62016-12-21 06:19:42 -0800194 ),
195 ),
196 ],
197 ),
198 ),
199 body: new Center(
200 child: new InkWell(
201 onTap: () {
202 _scaffoldKey.currentState.openDrawer();
203 },
204 child: new Column(
205 mainAxisSize: MainAxisSize.min,
206 children: <Widget>[
207 new Container(
208 width: 100.0,
209 height: 100.0,
Chris Bracken60f5ae82017-05-09 21:15:42 -0700210 decoration: const BoxDecoration(
Hans Mullerd05c7f62016-12-21 06:19:42 -0800211 shape: BoxShape.circle,
Chris Bracken60f5ae82017-05-09 21:15:42 -0700212 image: const DecorationImage(
Sarah Zakariasa0e91bf2017-09-26 15:21:01 +0200213 image: const AssetImage(
214 _kAsset0,
215 package: _kGalleryAssetsPackage,
216 ),
Hans Mullerd05c7f62016-12-21 06:19:42 -0800217 ),
218 ),
219 ),
220 new Padding(
221 padding: const EdgeInsets.only(top: 8.0),
222 child: new Text('Tap here to open the drawer',
223 style: Theme.of(context).textTheme.subhead,
224 ),
225 ),
226 ],
227 ),
228 ),
229 ),
230 );
231 }
Michael Goderbauer94f48c22018-01-02 16:28:31 -0800232
233 void _onOtherAccountsTap(BuildContext context) {
234 showDialog<Null>(
235 context: context,
236 child: new AlertDialog(
237 title: const Text('Account switching not implemented.'),
238 actions: <Widget>[
239 new FlatButton(
240 child: const Text('OK'),
241 onPressed: () {
242 Navigator.of(context).pop();
243 },
244 ),
245 ],
246 ),
247 );
248 }
Hans Mullerd05c7f62016-12-21 06:19:42 -0800249}