Hans Muller | d05c7f6 | 2016-12-21 06:19:42 -0800 | [diff] [blame] | 1 | // 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 | |
| 5 | import 'package:flutter/material.dart'; |
| 6 | |
Sarah Zakarias | a0e91bf | 2017-09-26 15:21:01 +0200 | [diff] [blame] | 7 | const String _kAsset0 = 'shrine/vendors/zach.jpg'; |
| 8 | const String _kAsset1 = 'shrine/vendors/16c477b.jpg'; |
| 9 | const String _kAsset2 = 'shrine/vendors/sandra-adams.jpg'; |
| 10 | const String _kGalleryAssetsPackage = 'flutter_gallery_assets'; |
Hans Muller | d05c7f6 | 2016-12-21 06:19:42 -0800 | [diff] [blame] | 11 | |
| 12 | class DrawerDemo extends StatefulWidget { |
xster | a76c352 | 2017-03-01 16:06:48 -0800 | [diff] [blame] | 13 | static const String routeName = '/material/drawer'; |
Hans Muller | d05c7f6 | 2016-12-21 06:19:42 -0800 | [diff] [blame] | 14 | |
| 15 | @override |
| 16 | _DrawerDemoState createState() => new _DrawerDemoState(); |
| 17 | } |
| 18 | |
| 19 | class _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 Barth | 6128f48 | 2017-10-02 23:30:14 -0700 | [diff] [blame] | 28 | Animation<Offset> _drawerDetailsPosition; |
Hans Muller | d05c7f6 | 2016-12-21 06:19:42 -0800 | [diff] [blame] | 29 | 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 Barth | 6128f48 | 2017-10-02 23:30:14 -0700 | [diff] [blame] | 42 | _drawerDetailsPosition = new Tween<Offset>( |
| 43 | begin: const Offset(0.0, -1.0), |
| 44 | end: Offset.zero, |
Hans Muller | d05c7f6 | 2016-12-21 06:19:42 -0800 | [diff] [blame] | 45 | ).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 Ardhuin | 83fce21 | 2017-04-23 22:37:29 +0200 | [diff] [blame] | 71 | _scaffoldKey.currentState.showSnackBar(const SnackBar( |
Ian Hickson | 3eb8783 | 2017-04-07 12:24:32 -0700 | [diff] [blame] | 72 | content: const Text("The drawer's items don't do anything") |
Hans Muller | d05c7f6 | 2016-12-21 06:19:42 -0800 | [diff] [blame] | 73 | )); |
| 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 Barth | 0044ea2 | 2017-10-02 00:06:24 -0700 | [diff] [blame] | 83 | alignment: Alignment.centerLeft, |
Hans Muller | d05c7f6 | 2016-12-21 06:19:42 -0800 | [diff] [blame] | 84 | tooltip: 'Back', |
| 85 | onPressed: () { |
| 86 | Navigator.pop(context); |
| 87 | }, |
| 88 | ), |
Ian Hickson | 3eb8783 | 2017-04-07 12:24:32 -0700 | [diff] [blame] | 89 | title: const Text('Navigation drawer'), |
Hans Muller | d05c7f6 | 2016-12-21 06:19:42 -0800 | [diff] [blame] | 90 | ), |
| 91 | drawer: new Drawer( |
xster | 9db8966 | 2017-12-05 11:58:03 -0800 | [diff] [blame] | 92 | child: new Column( |
Hans Muller | d05c7f6 | 2016-12-21 06:19:42 -0800 | [diff] [blame] | 93 | children: <Widget>[ |
| 94 | new UserAccountsDrawerHeader( |
Ian Hickson | 3eb8783 | 2017-04-07 12:24:32 -0700 | [diff] [blame] | 95 | accountName: const Text('Zach Widget'), |
| 96 | accountEmail: const Text('zach.widget@example.com'), |
Sarah Zakarias | a0e91bf | 2017-09-26 15:21:01 +0200 | [diff] [blame] | 97 | currentAccountPicture: const CircleAvatar( |
| 98 | backgroundImage: const AssetImage( |
| 99 | _kAsset0, |
| 100 | package: _kGalleryAssetsPackage, |
| 101 | ), |
| 102 | ), |
Alexandre Ardhuin | c02b6a8 | 2018-02-02 23:27:29 +0100 | [diff] [blame^] | 103 | otherAccountsPictures: <Widget>[ |
Michael Goderbauer | 94f48c2 | 2018-01-02 16:28:31 -0800 | [diff] [blame] | 104 | 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 Zakarias | a0e91bf | 2017-09-26 15:21:01 +0200 | [diff] [blame] | 116 | ), |
| 117 | ), |
Michael Goderbauer | 94f48c2 | 2018-01-02 16:28:31 -0800 | [diff] [blame] | 118 | 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 Zakarias | a0e91bf | 2017-09-26 15:21:01 +0200 | [diff] [blame] | 130 | ), |
| 131 | ), |
Hans Muller | d05c7f6 | 2016-12-21 06:19:42 -0800 | [diff] [blame] | 132 | ], |
xster | 9db8966 | 2017-12-05 11:58:03 -0800 | [diff] [blame] | 133 | margin: EdgeInsets.zero, |
Hans Muller | d05c7f6 | 2016-12-21 06:19:42 -0800 | [diff] [blame] | 134 | onDetailsPressed: () { |
| 135 | _showDrawerContents = !_showDrawerContents; |
| 136 | if (_showDrawerContents) |
| 137 | _controller.reverse(); |
| 138 | else |
| 139 | _controller.forward(); |
| 140 | }, |
| 141 | ), |
xster | 9db8966 | 2017-12-05 11:58:03 -0800 | [diff] [blame] | 142 | 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 Muller | d05c7f6 | 2016-12-21 06:19:42 -0800 | [diff] [blame] | 165 | ), |
xster | 9db8966 | 2017-12-05 11:58:03 -0800 | [diff] [blame] | 166 | ), |
| 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 Muller | d05c7f6 | 2016-12-21 06:19:42 -0800 | [diff] [blame] | 188 | ), |
xster | 9db8966 | 2017-12-05 11:58:03 -0800 | [diff] [blame] | 189 | ), |
| 190 | ], |
Hans Muller | d05c7f6 | 2016-12-21 06:19:42 -0800 | [diff] [blame] | 191 | ), |
xster | 9db8966 | 2017-12-05 11:58:03 -0800 | [diff] [blame] | 192 | ], |
| 193 | ), |
Hans Muller | d05c7f6 | 2016-12-21 06:19:42 -0800 | [diff] [blame] | 194 | ), |
| 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 Bracken | 60f5ae8 | 2017-05-09 21:15:42 -0700 | [diff] [blame] | 210 | decoration: const BoxDecoration( |
Hans Muller | d05c7f6 | 2016-12-21 06:19:42 -0800 | [diff] [blame] | 211 | shape: BoxShape.circle, |
Chris Bracken | 60f5ae8 | 2017-05-09 21:15:42 -0700 | [diff] [blame] | 212 | image: const DecorationImage( |
Sarah Zakarias | a0e91bf | 2017-09-26 15:21:01 +0200 | [diff] [blame] | 213 | image: const AssetImage( |
| 214 | _kAsset0, |
| 215 | package: _kGalleryAssetsPackage, |
| 216 | ), |
Hans Muller | d05c7f6 | 2016-12-21 06:19:42 -0800 | [diff] [blame] | 217 | ), |
| 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 Goderbauer | 94f48c2 | 2018-01-02 16:28:31 -0800 | [diff] [blame] | 232 | |
| 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 Muller | d05c7f6 | 2016-12-21 06:19:42 -0800 | [diff] [blame] | 249 | } |