Skip to content

Commit d0e5b2f

Browse files
hannah-hyjHans Muller
and
Hans Muller
authored
Add a way to customize padding in BottomAppBar (#115175)
* squash 5 into 1 fix Update packages/flutter/lib/src/material/bottom_app_bar.dart lint update tests Co-Authored-By: Hans Muller <[email protected]> * lint Co-authored-by: Hans Muller <[email protected]>
1 parent e66183d commit d0e5b2f

File tree

3 files changed

+92
-10
lines changed

3 files changed

+92
-10
lines changed

packages/flutter/lib/src/material/bottom_app_bar.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class BottomAppBar extends StatefulWidget {
6969
this.clipBehavior = Clip.none,
7070
this.notchMargin = 4.0,
7171
this.child,
72+
this.padding,
7273
this.surfaceTintColor,
7374
this.height,
7475
}) : assert(elevation == null || elevation >= 0.0),
@@ -83,6 +84,12 @@ class BottomAppBar extends StatefulWidget {
8384
/// being an [IconButton] with the [Icons.menu] icon.
8485
final Widget? child;
8586

87+
/// The amount of space to surround the child inside the bounds of the [BottomAppBar].
88+
///
89+
/// In Material 3 the padding will default to `EdgeInsets.symmetric(vertical: 12.0, horizontal: 16.0)`
90+
/// Otherwise the value will default to EdgeInsets.zero.
91+
final EdgeInsetsGeometry? padding;
92+
8693
/// The bottom app bar's background color.
8794
///
8895
/// If this property is null then [BottomAppBarTheme.color] of
@@ -173,10 +180,10 @@ class _BottomAppBarState extends State<BottomAppBar> {
173180
final Color surfaceTintColor = widget.surfaceTintColor ?? babTheme.surfaceTintColor ?? defaults.surfaceTintColor!;
174181
final Color effectiveColor = isMaterial3 ? color : ElevationOverlay.applyOverlay(context, color, elevation);
175182

176-
final Widget? child = isMaterial3 ? Padding(
177-
padding: const EdgeInsets.symmetric(vertical: 12.0, horizontal: 16.0),
183+
final Widget child = Padding(
184+
padding: widget.padding ?? babTheme.padding ?? (isMaterial3 ? const EdgeInsets.symmetric(vertical: 12.0, horizontal: 16.0) : EdgeInsets.zero),
178185
child: widget.child,
179-
) : widget.child;
186+
);
180187

181188
return SizedBox(
182189
height: height,

packages/flutter/lib/src/material/bottom_app_bar_theme.dart

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,35 @@ class BottomAppBarTheme with Diagnosticable {
3434
this.shape,
3535
this.height,
3636
this.surfaceTintColor,
37+
this.padding,
3738
});
3839

39-
/// Default value for [BottomAppBar.color].
40+
/// Overrides the default value for [BottomAppBar.color].
4041
///
4142
/// If null, [BottomAppBar] uses [ThemeData.bottomAppBarColor].
4243
final Color? color;
4344

44-
/// Default value for [BottomAppBar.elevation].
45+
/// Overrides the default value for [BottomAppBar.elevation].
4546
final double? elevation;
4647

47-
/// Default value for [BottomAppBar.shape].
48+
/// Overrides the default value for [BottomAppBar.shape].
4849
final NotchedShape? shape;
4950

50-
/// Default value for [BottomAppBar.height].
51+
/// Overrides the default value for [BottomAppBar.height].
5152
///
5253
/// If null, [BottomAppBar] height will be the minimum on the non material 3.
5354
final double? height;
5455

55-
/// Default value for [BottomAppBar.surfaceTintColor].
56+
/// Overrides the default value for [BottomAppBar.surfaceTintColor].
5657
///
5758
/// If null, [BottomAppBar] will not display an overlay color.
5859
///
5960
/// See [Material.surfaceTintColor] for more details.
6061
final Color? surfaceTintColor;
6162

63+
/// Overrides the default value for [BottomAppBar.padding].
64+
final EdgeInsetsGeometry? padding;
65+
6266
/// Creates a copy of this object but with the given fields replaced with the
6367
/// new values.
6468
BottomAppBarTheme copyWith({
@@ -67,13 +71,15 @@ class BottomAppBarTheme with Diagnosticable {
6771
NotchedShape? shape,
6872
double? height,
6973
Color? surfaceTintColor,
74+
EdgeInsetsGeometry? padding,
7075
}) {
7176
return BottomAppBarTheme(
7277
color: color ?? this.color,
7378
elevation: elevation ?? this.elevation,
7479
shape: shape ?? this.shape,
7580
height: height ?? this.height,
7681
surfaceTintColor: surfaceTintColor ?? this.surfaceTintColor,
82+
padding: padding ?? this.padding,
7783
);
7884
}
7985

@@ -94,7 +100,8 @@ class BottomAppBarTheme with Diagnosticable {
94100
elevation: lerpDouble(a?.elevation, b?.elevation, t),
95101
shape: t < 0.5 ? a?.shape : b?.shape,
96102
height: lerpDouble(a?.height, b?.height, t),
97-
surfaceTintColor: Color.lerp(a?.color, b?.color, t),
103+
surfaceTintColor: Color.lerp(a?.surfaceTintColor, b?.surfaceTintColor, t),
104+
padding: EdgeInsetsGeometry.lerp(a?.padding, b?.padding, t),
98105
);
99106
}
100107

@@ -105,6 +112,7 @@ class BottomAppBarTheme with Diagnosticable {
105112
shape,
106113
height,
107114
surfaceTintColor,
115+
padding,
108116
);
109117

110118
@override
@@ -120,7 +128,8 @@ class BottomAppBarTheme with Diagnosticable {
120128
&& other.elevation == elevation
121129
&& other.shape == shape
122130
&& other.height == height
123-
&& other.surfaceTintColor == surfaceTintColor;
131+
&& other.surfaceTintColor == surfaceTintColor
132+
&& other.padding == padding;
124133
}
125134

126135
@override
@@ -131,5 +140,6 @@ class BottomAppBarTheme with Diagnosticable {
131140
properties.add(DiagnosticsProperty<NotchedShape>('shape', shape, defaultValue: null));
132141
properties.add(DiagnosticsProperty<double>('height', height, defaultValue: null));
133142
properties.add(ColorProperty('surfaceTintColor', surfaceTintColor, defaultValue: null));
143+
properties.add(DiagnosticsProperty<EdgeInsetsGeometry>('padding', padding, defaultValue: null));
134144
}
135145
}

packages/flutter/test/material/bottom_app_bar_test.dart

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,71 @@ void main() {
8585
);
8686
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/44572
8787

88+
89+
testWidgets('Custom Padding', (WidgetTester tester) async {
90+
const EdgeInsets customPadding = EdgeInsets.all(10);
91+
await tester.pumpWidget(
92+
MaterialApp(
93+
theme: ThemeData.from(colorScheme: const ColorScheme.light()),
94+
home: Builder(
95+
builder: (BuildContext context) {
96+
return const Scaffold(
97+
body: Align(
98+
alignment: Alignment.bottomCenter,
99+
child: BottomAppBar(
100+
padding: customPadding,
101+
child:ColoredBox(
102+
color:Colors.green,
103+
child:SizedBox(width: 300, height: 60),
104+
),
105+
),
106+
),
107+
);
108+
},
109+
),
110+
),
111+
);
112+
113+
final BottomAppBar bottomAppBar = tester.widget(find.byType(BottomAppBar));
114+
expect(bottomAppBar.padding, customPadding);
115+
final Rect babRect = tester.getRect(find.byType(BottomAppBar));
116+
final Rect childRect = tester.getRect(find.byType(ColoredBox));
117+
expect(childRect, const Rect.fromLTRB(250, 530, 550, 590));
118+
expect(babRect, const Rect.fromLTRB(240, 520, 560, 600));
119+
});
120+
121+
testWidgets('Custom Padding in Material 3', (WidgetTester tester) async {
122+
const EdgeInsets customPadding = EdgeInsets.all(10);
123+
await tester.pumpWidget(
124+
MaterialApp(
125+
theme: ThemeData.from(colorScheme: const ColorScheme.light(), useMaterial3: true),
126+
home: Builder(
127+
builder: (BuildContext context) {
128+
return const Scaffold(
129+
body: Align(
130+
alignment: Alignment.bottomCenter,
131+
child: BottomAppBar(
132+
padding: customPadding,
133+
child:ColoredBox(
134+
color:Colors.green,
135+
child:SizedBox(width: 300, height: 60),
136+
),
137+
),
138+
),
139+
);
140+
},
141+
),
142+
),
143+
);
144+
145+
final BottomAppBar bottomAppBar = tester.widget(find.byType(BottomAppBar));
146+
expect(bottomAppBar.padding, customPadding);
147+
final Rect babRect = tester.getRect(find.byType(BottomAppBar));
148+
final Rect childRect = tester.getRect(find.byType(ColoredBox));
149+
expect(childRect, const Rect.fromLTRB(250, 530, 550, 590));
150+
expect(babRect, const Rect.fromLTRB(240, 520, 560, 600));
151+
});
152+
88153
testWidgets('color defaults to Theme.bottomAppBarColor', (WidgetTester tester) async {
89154
await tester.pumpWidget(
90155
MaterialApp(

0 commit comments

Comments
 (0)