1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:math' as math;
import 'package:flutter/widgets.dart';
import 'colors.dart';
import 'debug.dart';
import 'drawer_header.dart';
import 'icons.dart';
import 'ink_well.dart';
import 'material_localizations.dart';
import 'theme.dart';
class _AccountPictures extends StatelessWidget {
const _AccountPictures({
Key? key,
this.currentAccountPicture,
this.otherAccountsPictures,
this.currentAccountPictureSize,
this.otherAccountsPicturesSize,
}) : super(key: key);
final Widget? currentAccountPicture;
final List<Widget>? otherAccountsPictures;
final Size? currentAccountPictureSize;
final Size? otherAccountsPicturesSize;
@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
PositionedDirectional(
top: 0.0,
end: 0.0,
child: Row(
children: (otherAccountsPictures ?? <Widget>[]).take(3).map<Widget>((Widget picture) {
return Padding(
padding: const EdgeInsetsDirectional.only(start: 8.0),
child: Semantics(
container: true,
child: Padding(
padding: const EdgeInsets.only(left: 8.0, bottom: 8.0),
child: SizedBox.fromSize(
size: otherAccountsPicturesSize,
child: picture,
),
),
),
);
}).toList(),
),
),
Positioned(
top: 0.0,
child: Semantics(
explicitChildNodes: true,
child: SizedBox.fromSize(
size: currentAccountPictureSize,
child: currentAccountPicture,
),
),
),
],
);
}
}
class _AccountDetails extends StatefulWidget {
const _AccountDetails({
Key? key,
required this.accountName,
required this.accountEmail,
this.onTap,
required this.isOpen,
this.arrowColor,
}) : super(key: key);
final Widget? accountName;
final Widget? accountEmail;
final VoidCallback? onTap;
final bool isOpen;
final Color? arrowColor;
@override
_AccountDetailsState createState() => _AccountDetailsState();
}
class _AccountDetailsState extends State<_AccountDetails> with SingleTickerProviderStateMixin {
late Animation<double> _animation;
late AnimationController _controller;
@override
void initState () {
super.initState();
_controller = AnimationController(
value: widget.isOpen ? 1.0 : 0.0,
duration: const Duration(milliseconds: 200),
vsync: this,
);
_animation = CurvedAnimation(
parent: _controller,
curve: Curves.fastOutSlowIn,
reverseCurve: Curves.fastOutSlowIn.flipped,
)
..addListener(() => setState(() {
// [animation]'s value has changed here.
}));
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
void didUpdateWidget (_AccountDetails oldWidget) {
super.didUpdateWidget(oldWidget);
// If the state of the arrow did not change, there is no need to trigger the animation
if (oldWidget.isOpen == widget.isOpen) {
return;
}
if (widget.isOpen) {
_controller.forward();
} else {
_controller.reverse();
}
}
@override
Widget build(BuildContext context) {
assert(debugCheckHasDirectionality(context));
assert(debugCheckHasMaterialLocalizations(context));
assert(debugCheckHasMaterialLocalizations(context));
final ThemeData theme = Theme.of(context);
final MaterialLocalizations localizations = MaterialLocalizations.of(context);
Widget accountDetails = CustomMultiChildLayout(
delegate: _AccountDetailsLayout(
textDirection: Directionality.of(context),
),
children: <Widget>[
if (widget.accountName != null)
LayoutId(
id: _AccountDetailsLayout.accountName,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 2.0),
child: DefaultTextStyle(
style: theme.primaryTextTheme.bodyText1!,
overflow: TextOverflow.ellipsis,
child: widget.accountName!,
),
),
),
if (widget.accountEmail != null)
LayoutId(
id: _AccountDetailsLayout.accountEmail,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 2.0),
child: DefaultTextStyle(
style: theme.primaryTextTheme.bodyText2!,
overflow: TextOverflow.ellipsis,
child: widget.accountEmail!,
),
),
),
if (widget.onTap != null)
LayoutId(
id: _AccountDetailsLayout.dropdownIcon,
child: Semantics(
container: true,
button: true,
onTap: widget.onTap,
child: SizedBox(
height: _kAccountDetailsHeight,
width: _kAccountDetailsHeight,
child: Center(
child: Transform.rotate(
angle: _animation.value * math.pi,
child: Icon(
Icons.arrow_drop_down,
color: widget.arrowColor,
semanticLabel: widget.isOpen
? localizations.hideAccountsLabel
: localizations.showAccountsLabel,
),
),
),
),
),
),
],
);
if (widget.onTap != null) {
accountDetails = InkWell(
onTap: widget.onTap,
excludeFromSemantics: true,
child: accountDetails,
);
}
return SizedBox(
height: _kAccountDetailsHeight,
child: accountDetails,
);
}
}
const double _kAccountDetailsHeight = 56.0;
class _AccountDetailsLayout extends MultiChildLayoutDelegate {
_AccountDetailsLayout({ required this.textDirection });
static const String accountName = 'accountName';
static const String accountEmail = 'accountEmail';
static const String dropdownIcon = 'dropdownIcon';
final TextDirection textDirection;
@override
void performLayout(Size size) {
Size? iconSize;
if (hasChild(dropdownIcon)) {
// place the dropdown icon in bottom right (LTR) or bottom left (RTL)
iconSize = layoutChild(dropdownIcon, BoxConstraints.loose(size));
positionChild(dropdownIcon, _offsetForIcon(size, iconSize));
}
final String? bottomLine = hasChild(accountEmail) ? accountEmail : (hasChild(accountName) ? accountName : null);
if (bottomLine != null) {
final Size constraintSize = iconSize == null ? size : Size(size.width - iconSize.width, size.height);
iconSize ??= const Size(_kAccountDetailsHeight, _kAccountDetailsHeight);
// place bottom line center at same height as icon center
final Size bottomLineSize = layoutChild(bottomLine, BoxConstraints.loose(constraintSize));
final Offset bottomLineOffset = _offsetForBottomLine(size, iconSize, bottomLineSize);
positionChild(bottomLine, bottomLineOffset);
// place account name above account email
if (bottomLine == accountEmail && hasChild(accountName)) {
final Size nameSize = layoutChild(accountName, BoxConstraints.loose(constraintSize));
positionChild(accountName, _offsetForName(size, nameSize, bottomLineOffset));
}
}
}
@override
bool shouldRelayout(MultiChildLayoutDelegate oldDelegate) => true;
Offset _offsetForIcon(Size size, Size iconSize) {
switch (textDirection) {
case TextDirection.ltr:
return Offset(size.width - iconSize.width, size.height - iconSize.height);
case TextDirection.rtl:
return Offset(0.0, size.height - iconSize.height);
}
}
Offset _offsetForBottomLine(Size size, Size iconSize, Size bottomLineSize) {
final double y = size.height - 0.5 * iconSize.height - 0.5 * bottomLineSize.height;
switch (textDirection) {
case TextDirection.ltr:
return Offset(0.0, y);
case TextDirection.rtl:
return Offset(size.width - bottomLineSize.width, y);
}
}
Offset _offsetForName(Size size, Size nameSize, Offset bottomLineOffset) {
final double y = bottomLineOffset.dy - nameSize.height;
switch (textDirection) {
case TextDirection.ltr:
return Offset(0.0, y);
case TextDirection.rtl:
return Offset(size.width - nameSize.width, y);
}
}
}
/// A material design [Drawer] header that identifies the app's user.
///
/// Requires one of its ancestors to be a [Material] widget.
///
/// See also:
///
/// * [DrawerHeader], for a drawer header that doesn't show user accounts.
/// * <https://material.io/design/components/navigation-drawer.html#anatomy>
class UserAccountsDrawerHeader extends StatefulWidget {
/// Creates a material design drawer header.
///
/// Requires one of its ancestors to be a [Material] widget.
const UserAccountsDrawerHeader({
Key? key,
this.decoration,
this.margin = const EdgeInsets.only(bottom: 8.0),
this.currentAccountPicture,
this.otherAccountsPictures,
this.currentAccountPictureSize = const Size.square(72.0),
this.otherAccountsPicturesSize = const Size.square(40.0),
required this.accountName,
required this.accountEmail,
this.onDetailsPressed,
this.arrowColor = Colors.white,
}) : super(key: key);
/// The header's background. If decoration is null then a [BoxDecoration]
/// with its background color set to the current theme's primaryColor is used.
final Decoration? decoration;
/// The margin around the drawer header.
final EdgeInsetsGeometry? margin;
/// A widget placed in the upper-left corner that represents the current
/// user's account. Normally a [CircleAvatar].
final Widget? currentAccountPicture;
/// A list of widgets that represent the current user's other accounts.
/// Up to three of these widgets will be arranged in a row in the header's
/// upper-right corner. Normally a list of [CircleAvatar] widgets.
final List<Widget>? otherAccountsPictures;
/// The size of the [currentAccountPicture].
final Size currentAccountPictureSize;
/// The size of each widget in [otherAccountsPicturesSize].
final Size otherAccountsPicturesSize;
/// A widget that represents the user's current account name. It is
/// displayed on the left, below the [currentAccountPicture].
final Widget? accountName;
/// A widget that represents the email address of the user's current account.
/// It is displayed on the left, below the [accountName].
final Widget? accountEmail;
/// A callback that is called when the horizontal area which contains the
/// [accountName] and [accountEmail] is tapped.
final VoidCallback? onDetailsPressed;
/// The [Color] of the arrow icon.
final Color arrowColor;
@override
State<UserAccountsDrawerHeader> createState() => _UserAccountsDrawerHeaderState();
}
class _UserAccountsDrawerHeaderState extends State<UserAccountsDrawerHeader> {
bool _isOpen = false;
void _handleDetailsPressed() {
setState(() {
_isOpen = !_isOpen;
});
widget.onDetailsPressed!();
}
@override
Widget build(BuildContext context) {
assert(debugCheckHasMaterial(context));
assert(debugCheckHasMaterialLocalizations(context));
return Semantics(
container: true,
label: MaterialLocalizations.of(context).signedInLabel,
child: DrawerHeader(
decoration: widget.decoration ?? BoxDecoration(
color: Theme.of(context).primaryColor,
),
margin: widget.margin,
padding: const EdgeInsetsDirectional.only(top: 16.0, start: 16.0),
child: SafeArea(
bottom: false,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsetsDirectional.only(end: 16.0),
child: _AccountPictures(
currentAccountPicture: widget.currentAccountPicture,
otherAccountsPictures: widget.otherAccountsPictures,
currentAccountPictureSize: widget.currentAccountPictureSize,
otherAccountsPicturesSize: widget.otherAccountsPicturesSize,
),
),
),
_AccountDetails(
accountName: widget.accountName,
accountEmail: widget.accountEmail,
isOpen: _isOpen,
onTap: widget.onDetailsPressed == null ? null : _handleDetailsPressed,
arrowColor: widget.arrowColor,
),
],
),
),
),
);
}
}