Repository navigation
Expand file tree
/
Copy pathbottom_app_bar.dart
More file actions
313 lines (279 loc) · 11 KB
/
Copy pathbottom_app_bar.dart
File metadata and controls
313 lines (279 loc) · 11 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
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/// @docImport 'app_bar.dart';
/// @docImport 'floating_action_button.dart';
/// @docImport 'floating_action_button_location.dart';
/// @docImport 'icon_button.dart';
/// @docImport 'icons.dart';
library;
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'bottom_app_bar_theme.dart';
import 'color_scheme.dart';
import 'colors.dart';
import 'elevation_overlay.dart';
import 'material.dart';
import 'scaffold.dart';
import 'theme.dart';
part 'generated/bottom_app_bar_defaults_m3.g.dart';
// Examples can assume:
// late Widget bottomAppBarContents;
/// A container that is typically used with [Scaffold.bottomNavigationBar].
///
/// Typically used with a [Scaffold] and a [FloatingActionButton].
///
/// <callout-box>
///
/// {@example /example/lib/bottom_app_bar/bottom_app_bar.snippet.0.dart#body indent=strip}
///
/// </callout-box>
///
/// <callout-box>
///
/// This example shows the [BottomAppBar], which can be configured to have a notch using the
/// [BottomAppBar.shape] property. This also includes an optional [FloatingActionButton], which illustrates
/// the [FloatingActionButtonLocation]s in relation to the [BottomAppBar].
///
// TODO(framework): Replace the following block with a @dartpad directive
// when it's supported. https://github.com/dart-lang/dartdoc/issues/4123
/// {@macro material_ui.dartpad_guide}
///
/// {@example /example/lib/bottom_app_bar/bottom_app_bar.1.dart#body}
///
/// </callout-box>
///
/// <callout-box>
///
/// This example shows Material 3 [BottomAppBar] with its expected look and behaviors.
///
/// This also includes an optional [FloatingActionButton], which illustrates
/// the [FloatingActionButtonLocation.endContained].
///
// TODO(framework): Replace the following block with a @dartpad directive
// when it's supported. https://github.com/dart-lang/dartdoc/issues/4123
/// {@macro material_ui.dartpad_guide}
///
/// {@example /example/lib/bottom_app_bar/bottom_app_bar.2.dart#body}
///
/// </callout-box>
///
/// See also:
///
/// * [NotchedShape] which calculates the notch for a notched [BottomAppBar].
/// * [FloatingActionButton] which the [BottomAppBar] makes a notch for.
/// * [AppBar] for a toolbar that is shown at the top of the screen.
class BottomAppBar extends StatefulWidget {
/// Creates a bottom application bar.
///
/// The [clipBehavior] argument defaults to [Clip.none].
/// Additionally, [elevation] must be non-negative.
///
/// If [color], [elevation], or [shape] are null, their [BottomAppBarThemeData] values will be used.
/// If the corresponding [BottomAppBarThemeData] property is null, then the default
/// specified in the property's documentation will be used.
const BottomAppBar({
super.key,
this.color,
this.elevation,
this.shape,
this.clipBehavior = Clip.none,
this.notchMargin = 4.0,
this.child,
this.padding,
this.surfaceTintColor,
this.shadowColor,
this.height,
}) : assert(elevation == null || elevation >= 0.0);
/// The widget below this widget in the tree.
///
/// {@macro flutter.widgets.ProxyWidget.child}
///
/// Typically the child will be a [Row] whose first child
/// is an [IconButton] with the [Icons.menu] icon.
final Widget? child;
/// The amount of space to surround the child inside the bounds of the [BottomAppBar].
///
/// In Material 3 the padding will default to `EdgeInsets.symmetric(vertical: 12.0, horizontal: 16.0)`
/// Otherwise the value will default to EdgeInsets.zero.
final EdgeInsetsGeometry? padding;
/// The bottom app bar's background color.
///
/// If this property is null then the ambient [BottomAppBarThemeData.color]
/// is used. If that's null and [ThemeData.useMaterial3] is true,
/// the default value is [ColorScheme.surface]; if [ThemeData.useMaterial3]
/// is false, then the default value is `Color(0xFF424242)` in dark theme and
/// [Colors.white] in light theme.
final Color? color;
/// The z-coordinate at which to place this bottom app bar relative to its
/// parent.
///
/// This controls the size of the shadow below the bottom app bar. The
/// value is non-negative.
///
/// If this property is null then the ambient [BottomAppBarThemeData.elevation]
/// is used. If that's null and [ThemeData.useMaterial3] is true,
/// then the default value is 3 else is 8.
final double? elevation;
/// The notch that is made for the floating action button.
///
/// If this property is null then the ambient [BottomAppBarThemeData.shape]
/// is used. If that's null then the shape will be rectangular with no notch.
final NotchedShape? shape;
/// {@macro cupertino_ui.Material.clipBehavior}
///
/// Defaults to [Clip.none].
final Clip clipBehavior;
/// The margin between the [FloatingActionButton] and the [BottomAppBar]'s
/// notch.
///
/// Not used if [shape] is null.
final double notchMargin;
/// A custom color for the Material 3 surface-tint elevation effect.
///
/// This is not recommended for use. [Material 3 spec](https://m3.material.io/styles/color/the-color-system/color-roles)
/// introduced a set of tone-based surfaces and surface containers in its [ColorScheme],
/// which provide more flexibility. The intention is to eventually remove surface tint color from
/// the framework.
///
/// If this property is null, then the ambient [BottomAppBarThemeData.surfaceTintColor]
/// is used. If that is also null, the default value is [Colors.transparent].
///
/// Ignored if [ThemeData.useMaterial3] is false.
///
/// See [Material.surfaceTintColor] for more details on how this overlay is applied.
final Color? surfaceTintColor;
/// The color of the shadow below the app bar.
///
/// If this property is null, then the ambient [BottomAppBarThemeData.shadowColor]
/// is used. If that is also null, the default value is fully opaque black for
/// Material 2, and transparent for Material 3.
///
/// See also:
///
/// * [elevation], which defines the size of the shadow below the app bar.
/// * [shape], which defines the shape of the app bar and its shadow.
final Color? shadowColor;
/// The double value used to indicate the height of the [BottomAppBar].
///
/// If this is null, the default value is the minimum in relation to the content,
/// unless [ThemeData.useMaterial3] is true, in which case it defaults to 80.0.
final double? height;
@override
State createState() => _BottomAppBarState();
}
class _BottomAppBarState extends State<BottomAppBar> {
late ValueListenable<ScaffoldGeometry> geometryListenable;
final GlobalKey materialKey = GlobalKey();
@override
void didChangeDependencies() {
super.didChangeDependencies();
geometryListenable = Scaffold.geometryOf(context);
}
@override
Widget build(BuildContext context) {
final ThemeData theme = Theme.of(context);
final bool isMaterial3 = theme.useMaterial3;
final BottomAppBarThemeData babTheme = BottomAppBarTheme.of(context);
final BottomAppBarThemeData defaults = isMaterial3
? _BottomAppBarDefaultsM3(context)
: _BottomAppBarDefaultsM2(context);
final bool hasFab = Scaffold.of(context).hasFloatingActionButton;
final NotchedShape? notchedShape = widget.shape ?? babTheme.shape ?? defaults.shape;
final CustomClipper<Path> clipper = notchedShape != null && hasFab
? _BottomAppBarClipper(
geometry: geometryListenable,
shape: notchedShape,
materialKey: materialKey,
notchMargin: widget.notchMargin,
)
: const ShapeBorderClipper(shape: RoundedRectangleBorder());
final double elevation = widget.elevation ?? babTheme.elevation ?? defaults.elevation!;
final double? height = widget.height ?? babTheme.height ?? defaults.height;
final Color color = widget.color ?? babTheme.color ?? defaults.color!;
final Color surfaceTintColor =
widget.surfaceTintColor ?? babTheme.surfaceTintColor ?? defaults.surfaceTintColor!;
final Color effectiveColor = isMaterial3
? ElevationOverlay.applySurfaceTint(color, surfaceTintColor, elevation)
: ElevationOverlay.applyOverlay(context, color, elevation);
final Color shadowColor = widget.shadowColor ?? babTheme.shadowColor ?? defaults.shadowColor!;
final Widget child = SizedBox(
height: height,
child: Padding(
padding:
widget.padding ??
babTheme.padding ??
(isMaterial3
? const EdgeInsets.symmetric(vertical: 12.0, horizontal: 16.0)
: EdgeInsets.zero),
child: widget.child,
),
);
final material = Material(
key: materialKey,
type: MaterialType.transparency,
child: SafeArea(child: child),
);
return PhysicalShape(
clipper: clipper,
elevation: elevation,
shadowColor: shadowColor,
color: effectiveColor,
clipBehavior: widget.clipBehavior,
child: material,
);
}
}
class _BottomAppBarClipper extends CustomClipper<Path> {
const _BottomAppBarClipper({
required this.geometry,
required this.shape,
required this.materialKey,
required this.notchMargin,
}) : super(reclip: geometry);
final ValueListenable<ScaffoldGeometry> geometry;
final NotchedShape shape;
final GlobalKey materialKey;
final double notchMargin;
// Returns the top of the BottomAppBar in global coordinates.
//
// If the Scaffold's bottomNavigationBar was specified, then we can use its
// geometry value, otherwise we compute the location based on the AppBar's
// Material widget.
double get bottomNavigationBarTop {
final double? bottomNavigationBarTop = geometry.value.bottomNavigationBarTop;
if (bottomNavigationBarTop != null) {
return bottomNavigationBarTop;
}
final box = materialKey.currentContext?.findRenderObject() as RenderBox?;
return box?.localToGlobal(Offset.zero).dy ?? 0;
}
@override
Path getClip(Size size) {
// button is the floating action button's bounding rectangle in the
// coordinate system whose origin is at the appBar's top left corner,
// or null if there is no floating action button.
final Rect? button = geometry.value.floatingActionButtonArea?.translate(
0.0,
bottomNavigationBarTop * -1.0,
);
return shape.getOuterPath(Offset.zero & size, button?.inflate(notchMargin));
}
@override
bool shouldReclip(_BottomAppBarClipper oldClipper) {
return oldClipper.geometry != geometry ||
oldClipper.shape != shape ||
oldClipper.notchMargin != notchMargin;
}
}
class _BottomAppBarDefaultsM2 extends BottomAppBarThemeData {
const _BottomAppBarDefaultsM2(this.context) : super(elevation: 8.0);
final BuildContext context;
@override
Color? get color =>
Theme.brightnessOf(context) == Brightness.dark ? Colors.grey[800]! : Colors.white;
@override
Color? get surfaceTintColor => Theme.of(context).colorScheme.surfaceTint;
@override
Color get shadowColor => const Color(0xFF000000);
}