-
-
Notifications
You must be signed in to change notification settings - Fork 645
Expand file tree
/
Copy pathgf_appbar.dart
More file actions
644 lines (583 loc) · 20.9 KB
/
gf_appbar.dart
File metadata and controls
644 lines (583 loc) · 20.9 KB
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:getwidget/getwidget.dart';
/// An app bar consists of a toolbar and potentially other widgets, such as a
/// [GFTabBar][TabBar] and a [FlexibleSpaceBar].
/// The GFAppBar displays the toolbar widgets, [leading], [title], and [actions],
/// above the [bottom] (if any). The [bottom] is usually used for a [TabBar].
class GFAppBar extends StatefulWidget implements PreferredSizeWidget {
/// Creates a material design app bar.
///
/// The arguments [primary], [toolbarOpacity], [bottomOpacity]
/// and [automaticallyImplyLeading] must not be null. Additionally, if
/// [elevation] is specified, it must be non-negative.
///
/// If [backgroundColor], [elevation], [brightness], [iconTheme],
/// [actionsIconTheme], or [textTheme] are null, then their GFAppBarTheme
/// values will be used. If the corresponding GFAppBarTheme property is null,
/// then the default specified in the property's documentation will be used.
///
/// Typically used in the [Scaffold.appBar] property.
GFAppBar({
Key? key,
this.leading,
this.automaticallyImplyLeading = true,
this.title,
this.actions,
this.flexibleSpace,
this.bottom,
this.elevation,
this.shape,
this.backgroundColor,
this.brightness,
this.iconTheme,
this.actionsIconTheme,
this.textTheme,
this.primary = true,
this.centerTitle,
this.titleSpacing = NavigationToolbar.kMiddleSpacing,
this.toolbarOpacity = 1.0,
this.bottomOpacity = 1.0,
this.searchBar = false,
this.searchHintText = 'Search...',
this.searchHintStyle = const TextStyle(
color: Colors.white,
fontSize: 14,
),
this.searchTextStyle = const TextStyle(
color: Colors.white,
),
this.searchBarColorTheme = Colors.white,
this.searchController,
this.onTap,
this.onChanged,
this.onSubmitted,
}) : assert(elevation == null || elevation >= 0.0),
preferredSize = Size.fromHeight(
kToolbarHeight + (bottom?.preferredSize.height ?? 0),
),
super(key: key);
/// A widget to display before the [title].
final Widget? leading;
/// Controls whether we should try to imply the leading widget if null.
///
/// If true and [leading] is null, automatically try to deduce what the leading
/// widget should be. If false and [leading] is null, leading space is given to [title].
/// If leading widget is not null, this parameter has no effect.
final bool automaticallyImplyLeading;
/// The primary widget displayed in the app bar.
///
/// Typically a [Text] widget containing a description of the current contents
/// of the app.
final Widget? title;
/// Widgets to display after the [title] widget.
///
/// Typically these widgets are [IconButton]s representing common operations.
/// For less common operations, consider using a [PopupMenuButton] as the
/// last action.
final List<Widget>? actions;
/// This widget is stacked behind the toolbar and the tab bar. It's height will
/// be the same as the app bar's overall height.
///
/// A flexible space isn't actually flexible unless the [GFAppBar]'s container
/// changes the [GFAppBar]'s size. A SliverGFAppBar in a [CustomScrollView]
/// changes the [GFAppBar]'s height when scrolled.
///
/// Typically a [FlexibleSpaceBar]. See [FlexibleSpaceBar] for details.
final Widget? flexibleSpace;
/// This widget appears across the bottom of the app bar.
///
/// Typically a [TabBar]. Only widgets that implement [PreferredSizeWidget] can
/// be used at the bottom of an app bar.
///
/// See also:
///
/// * [PreferredSize], which can be used to give an arbitrary widget a preferred size.
final PreferredSizeWidget? bottom;
/// The z-coordinate at which to place this app bar relative to its parent.
///
/// This controls the size of the shadow below the app bar.
///
/// The value is non-negative.
///
/// If this property is null, then [AppBarTheme.elevation] is used,
/// if that is also null, the default value is 4, the appropriate elevation
/// for app bars.
final double? elevation;
/// The material's shape as well its shadow.
///
/// A shadow is only displayed if the [elevation] is greater than
/// zero.
final ShapeBorder? shape;
/// The color to use for the app bar's material. Typically this should be set
/// along with [brightness], [iconTheme], [textTheme].
///
/// If this property is null, then [AppBarTheme.backgroundColor] is used,
/// if that is also null, then [ThemeData.primaryColor] is used.
final Color? backgroundColor;
/// The brightness of the app bar's material. Typically this is set along
/// with [backgroundColor], [iconTheme], [textTheme].
///
/// If this property is null, then [ThemeData.brightness] is used,
/// if that is also null, then the appBarTheme can be used to configure the
/// appearance of AppBars. The appearance of Keyboards for TextFields now uses
/// the overall theme's ThemeData.brightness and can also be customized with
/// TextField.keyboardAppearance. The brightness of any color can be found with
/// ThemeData.estimateBrightnessForColor.
final Brightness? brightness;
/// The color, opacity, and size to use for app bar icons. Typically this
/// is set along with [backgroundColor], [brightness], [textTheme].
///
/// If this property is null, then [AppBarTheme.iconTheme] is used,
/// if that is also null, then [ThemeData.primaryIconTheme] is used.
final IconThemeData? iconTheme;
/// The color, opacity, and size to use for the icons that appear in the app
/// bar's [actions]. This should only be used when the [actions] should be
/// themed differently than the icon that appears in the app bar's [leading]
/// widget.
///
/// If this property is null, then [AppBarTheme.actionsIconTheme] is
/// used, if that is also null, then this falls back to [iconTheme].
final IconThemeData? actionsIconTheme;
/// The typographic styles to use for text in the app bar. Typically this is
/// set along with [brightness] [backgroundColor], [iconTheme].
///
/// If this property is null, then [ThemeData.textTheme] is used,
/// if that is also null, then [ThemeData.primaryTextTheme] is used.
final TextTheme? textTheme;
/// Whether this app bar is being displayed at the top of the screen.
///
/// If true, the app bar's toolbar elements and [bottom] widget will be
/// padded on top by the height of the system status bar. The layout
/// of the [flexibleSpace] is not affected by the [primary] property.
final bool primary;
/// Whether the title should be centered.
///
/// Defaults to being adapted to the current [TargetPlatform].
final bool? centerTitle;
/// The spacing around [title] content on the horizontal axis. This spacing is
/// applied even if there is no [leading] content or [actions]. If you want
/// [title] to take all the space available, set this value to 0.0.
///
/// Defaults to [NavigationToolbar.kMiddleSpacing].
final double titleSpacing;
/// How opaque the toolbar part of the app bar is.
///
/// A value of 1.0 is fully opaque, and a value of 0.0 is fully transparent.
///
/// Typically, this value is not changed from its default value (1.0). It is
/// used by SliverGFAppBar to animate the opacity of the toolbar when the app
/// bar is scrolled.
final double toolbarOpacity;
/// How opaque the bottom part of the app bar is.
///
/// A value of 1.0 is fully opaque, and a value of 0.0 is fully transparent.
///
/// Typically, this value is not changed from its default value (1.0). It is
/// used by SliverGFAppBar to animate the opacity of the toolbar when the app
/// bar is scrolled.
final double bottomOpacity;
/// A size whose height is the sum of [kToolbarHeight] and the [bottom] widget's
/// preferred height.
///
/// [Scaffold] uses this size to set its app bar's height.
@override
final Size preferredSize;
/// If true, displays search bar in the title space
final bool searchBar;
/// It takes text to displays the search bar hint text
final String searchHintText;
/// It styles the [searchHintText]
final TextStyle searchHintStyle;
/// It styles the search text
final TextStyle searchTextStyle;
/// It defines the search bar icons color
final Color searchBarColorTheme;
/// {@macro flutter.widgets.editableText.onChanged}
///
/// See also:
///
/// * inputFormatters, which are called before [onChanged]
/// runs and can validate and change ("format") the input value.
/// * onEditingComplete, [onSubmitted]:
/// which are more specialized input change notifications.
final ValueChanged<String>? onChanged;
/// {@macro flutter.widgets.editableText.onSubmitted}
///
/// See also:
///
/// * [EditableText.onSubmitted] for an example of how to handle moving to
/// the next/previous field when using [TextInputAction.next] and
/// [TextInputAction.previous] for [TextInputAction].
final ValueChanged<String>? onSubmitted;
/// Controls the text being edited.
///
/// If null, this widget will create its own [TextEditingController].
final TextEditingController? searchController;
/// {@template flutter.material.textfield.onTap}
/// Called for each distinct tap except for every second tap of a double tap.
///
/// The text field builds a [GestureDetector] to handle input events like tap,
/// to trigger focus requests, to move the caret, adjust the selection, etc.
/// Handling some of those events by wrapping the text field with a competing
/// GestureDetector is problematic.
///
/// To unconditionally handle taps, without interfering with the text field's
/// internal gesture detector, provide this callback.
///
/// If the text field is created with enabled false, taps will not be
/// recognized.
///
/// To be notified when the text field gains or loses the focus, provide a
/// focusNode and add a listener to that.
///
/// To listen to arbitrary pointer events without competing with the
/// text field's internal gesture detector, use a [Listener].
/// {@endtemplate}
final GestureTapCallback? onTap;
bool _getEffectiveCenterTitle(ThemeData theme) {
if (centerTitle != null) {
return centerTitle!;
}
switch (theme.platform) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
return false;
case TargetPlatform.iOS:
return actions == null || actions!.length < 2;
default:
return false;
}
}
@override
_GFAppBarState createState() => _GFAppBarState();
}
class _GFAppBarState extends State<GFAppBar> {
static const double _defaultElevation = 4;
Widget? searchBar;
bool showSearchBar = false;
final TextEditingController _searchController = TextEditingController();
void _handleDrawerButton() {
Scaffold.of(context).openDrawer();
}
void _handleDrawerButtonEnd() {
Scaffold.of(context).openEndDrawer();
}
@override
Widget build(BuildContext context) {
assert(!widget.primary || debugCheckHasMediaQuery(context));
assert(debugCheckHasMaterialLocalizations(context));
final ThemeData theme = Theme.of(context);
final appBarTheme = AppBarTheme.of(context);
final ScaffoldState scaffold = Scaffold.of(context);
final ModalRoute<dynamic>? parentRoute = ModalRoute.of(context);
final bool hasDrawer = scaffold.hasDrawer;
final bool hasEndDrawer = scaffold.hasEndDrawer;
final bool canPop = parentRoute?.canPop ?? false;
final bool useCloseButton =
parentRoute is PageRoute<dynamic> && parentRoute.fullscreenDialog;
IconThemeData overallIconTheme =
widget.iconTheme ?? appBarTheme.iconTheme ?? theme.primaryIconTheme;
IconThemeData actionsIconTheme = widget.actionsIconTheme ??
appBarTheme.actionsIconTheme ??
overallIconTheme;
TextStyle? centerStyle = widget.textTheme?.headlineSmall ??
appBarTheme.titleTextStyle ??
theme.primaryTextTheme.headlineSmall;
TextStyle? sideStyle = widget.textTheme?.bodyLarge ??
appBarTheme.toolbarTextStyle ??
theme.primaryTextTheme.bodyLarge;
if (widget.toolbarOpacity != 1.0) {
final double opacity = const Interval(
0.25,
1,
curve: Curves.fastOutSlowIn,
).transform(widget.toolbarOpacity);
if (centerStyle?.color != null) {
centerStyle = centerStyle!.copyWith(
color: centerStyle.color!.withValues(alpha: opacity),
);
}
if (sideStyle?.color != null) {
sideStyle = sideStyle!.copyWith(
color: sideStyle.color!.withValues(alpha: opacity),
);
}
overallIconTheme = overallIconTheme.copyWith(
opacity: opacity * (overallIconTheme.opacity ?? 1.0),
);
actionsIconTheme = actionsIconTheme.copyWith(
opacity: opacity * (actionsIconTheme.opacity ?? 1.0),
);
}
Widget? leading = widget.leading;
if (leading == null && widget.automaticallyImplyLeading) {
if (hasDrawer) {
leading = IconButton(
icon: const Icon(Icons.menu),
onPressed: _handleDrawerButton,
tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
);
} else {
if (canPop) {
leading = useCloseButton ? const CloseButton() : const BackButton();
}
}
}
if (leading != null) {
leading = ConstrainedBox(
constraints: const BoxConstraints.tightFor(width: _kLeadingWidth),
child: leading,
);
}
Widget? title = widget.title;
if (title != null && centerStyle != null) {
bool? namesRoute;
switch (theme.platform) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
namesRoute = true;
break;
case TargetPlatform.iOS:
break;
default:
break;
}
title = DefaultTextStyle(
style: centerStyle,
softWrap: false,
overflow: TextOverflow.ellipsis,
child: Semantics(
namesRoute: namesRoute,
child: GFAppBarTitleBar(child: title),
header: true,
),
);
}
Widget? actions;
if (widget.actions != null && widget.actions!.isNotEmpty) {
actions = Row(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: widget.actions!,
);
} else if (hasEndDrawer) {
actions = IconButton(
icon: const Icon(Icons.menu),
onPressed: _handleDrawerButtonEnd,
tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
);
}
// Allow the actions actions to have their own theme if necessary.
if (actions != null) {
actions = IconTheme.merge(
data: actionsIconTheme,
child: actions,
);
}
if (showSearchBar) {
searchBar = TextField(
cursorColor: widget.searchBarColorTheme,
style: widget.searchTextStyle,
decoration: InputDecoration(
prefixIcon: Icon(
Icons.search,
color: widget.searchBarColorTheme,
size: 18,
),
suffixIcon: GFIconButton(
icon: Icon(
Icons.close,
color: widget.searchBarColorTheme,
size: 20,
),
type: GFButtonType.transparent,
onPressed: () {
widget.onSubmitted?.call('');
final controller = widget.searchController ?? _searchController;
setState(() {
showSearchBar = !showSearchBar;
controller.text = '';
});
},
),
hintText: widget.searchHintText,
hintStyle: widget.searchHintStyle,
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
width: 1,
color: widget.searchBarColorTheme,
),
),
),
onTap: widget.onTap,
onChanged: widget.onChanged,
controller: widget.searchController ?? _searchController,
onSubmitted: widget.onSubmitted,
);
}
if (!showSearchBar) {
searchBar = ListTile(
contentPadding: EdgeInsets.zero,
title: title,
trailing: GFIconButton(
icon: Icon(
Icons.search,
color: widget.searchBarColorTheme,
size: 20,
),
type: GFButtonType.transparent,
onPressed: () {
setState(() {
showSearchBar = true;
});
},
),
);
}
final Widget toolbar = NavigationToolbar(
leading: leading,
middle: widget.searchBar ? searchBar : title,
trailing: actions,
centerMiddle: widget._getEffectiveCenterTitle(theme),
middleSpacing: widget.titleSpacing,
);
// If the toolbar is allocated less than kToolbarHeight make it
// appear to scroll upwards within its shrinking container.
Widget appBar = ClipRect(
child: CustomSingleChildLayout(
delegate: const _ToolbarContainerLayout(),
child: IconTheme.merge(
data: overallIconTheme,
child: DefaultTextStyle(
style: sideStyle ?? const TextStyle(),
child: toolbar,
),
),
),
);
if (widget.bottom != null) {
appBar = Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Flexible(
child: ConstrainedBox(
constraints: const BoxConstraints(maxHeight: kToolbarHeight),
child: appBar,
),
),
Opacity(
opacity: const Interval(
0.25,
1,
curve: Curves.fastOutSlowIn,
).transform(widget.bottomOpacity),
child: widget.bottom,
),
],
);
}
// The padding applies to the toolbar and tabBar, not the flexible space.
if (widget.primary) {
appBar = SafeArea(
top: true,
child: appBar,
);
}
appBar = Align(
alignment: Alignment.topCenter,
child: appBar,
);
if (widget.flexibleSpace != null) {
appBar = Stack(
fit: StackFit.passthrough,
children: <Widget>[
widget.flexibleSpace!,
appBar,
],
);
}
final Brightness brightness = widget.brightness ?? theme.brightness;
final SystemUiOverlayStyle overlayStyle = brightness == Brightness.dark
? SystemUiOverlayStyle.light
: SystemUiOverlayStyle.dark;
return Semantics(
container: true,
child: AnnotatedRegion<SystemUiOverlayStyle>(
value: overlayStyle,
child: Material(
color: widget.backgroundColor ??
appBarTheme.backgroundColor ??
theme.primaryColor,
elevation:
widget.elevation ?? appBarTheme.elevation ?? _defaultElevation,
shape: widget.shape,
child: Semantics(
explicitChildNodes: true,
child: appBar,
),
),
),
);
}
}
const double _kLeadingWidth = kToolbarHeight;
class _ToolbarContainerLayout extends SingleChildLayoutDelegate {
const _ToolbarContainerLayout();
@override
BoxConstraints getConstraintsForChild(BoxConstraints constraints) =>
constraints.tighten(
height: kToolbarHeight,
);
@override
Size getSize(BoxConstraints constraints) => Size(
constraints.maxWidth,
kToolbarHeight,
);
@override
Offset getPositionForChild(Size size, Size childSize) =>
Offset(0, size.height - childSize.height);
@override
bool shouldRelayout(_ToolbarContainerLayout oldDelegate) => false;
}
// Layout the GFAppBar's title with unconstrained height, vertically
// center it within its (NavigationToolbar) parent, and allow the
// parent to constrain the title's actual height.
class GFAppBarTitleBar extends SingleChildRenderObjectWidget {
const GFAppBarTitleBar({Key? key, required Widget child})
: super(key: key, child: child);
@override
RenderGFAppBarTitleBar createRenderObject(BuildContext context) =>
RenderGFAppBarTitleBar(
textDirection: Directionality.of(context),
);
@override
void updateRenderObject(
BuildContext context, RenderGFAppBarTitleBar renderObject) {
renderObject.textDirection = Directionality.of(context);
}
}
class RenderGFAppBarTitleBar extends RenderAligningShiftedBox {
RenderGFAppBarTitleBar({
RenderBox? child,
TextDirection? textDirection,
}) : super(
child: child,
alignment: Alignment.center,
textDirection: textDirection,
);
@override
void performLayout() {
final BoxConstraints innerConstraints =
constraints.copyWith(maxHeight: double.infinity);
child?.layout(
innerConstraints,
parentUsesSize: true,
);
if (child != null) {
size = constraints.constrain(child!.size);
}
alignChild();
}
}