Skip to content

Commit 4e8df8b

Browse files
authored
Add minimum height for MaterialBanner (flutter#153951)
This pr allows defining custom minimum height for material banner fixes this flutter#153666
1 parent 07fcfd1 commit 4e8df8b

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class MaterialBanner extends StatefulWidget {
120120
this.overflowAlignment = OverflowBarAlignment.end,
121121
this.animation,
122122
this.onVisible,
123+
this.minActionBarHeight = 52.0,
123124
}) : assert(elevation == null || elevation >= 0.0);
124125

125126
/// The content of the [MaterialBanner].
@@ -156,6 +157,11 @@ class MaterialBanner extends StatefulWidget {
156157
/// Typically an [Icon] widget.
157158
final Widget? leading;
158159

160+
/// The optional minimum action bar height.
161+
///
162+
/// Default to 52.0.
163+
final double minActionBarHeight;
164+
159165
/// The color of the surface of this [MaterialBanner].
160166
///
161167
/// If `null`, [MaterialBannerThemeData.backgroundColor] is used. If that is
@@ -252,6 +258,7 @@ class MaterialBanner extends StatefulWidget {
252258
actions: actions,
253259
elevation: elevation,
254260
leading: leading,
261+
minActionBarHeight: minActionBarHeight,
255262
backgroundColor: backgroundColor,
256263
surfaceTintColor: surfaceTintColor,
257264
shadowColor: shadowColor,
@@ -346,7 +353,7 @@ class _MaterialBannerState extends State<MaterialBanner> {
346353
?? const EdgeInsetsDirectional.only(end: 16.0);
347354

348355
final Widget actionsBar = ConstrainedBox(
349-
constraints: const BoxConstraints(minHeight: 52.0),
356+
constraints: BoxConstraints(minHeight: widget.minActionBarHeight),
350357
child: Padding(
351358
padding: const EdgeInsets.symmetric(horizontal: 8),
352359
child: Align(

packages/flutter/test/material/banner_test.dart

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,66 @@ void main() {
11501150
/// Compare the offset of banner from top left
11511151
expect(topLeft.dx, margin.left);
11521152
});
1153+
1154+
testWidgets('minActionBarHeight is respected', (WidgetTester tester) async {
1155+
const double minActionBarHeight = 20.0;
1156+
await tester.pumpWidget(
1157+
MaterialApp(
1158+
home:Scaffold(
1159+
appBar: AppBar(),
1160+
body: const MaterialBanner(
1161+
minActionBarHeight: minActionBarHeight,
1162+
padding: EdgeInsets.zero,
1163+
margin: EdgeInsets.zero,
1164+
content: SizedBox.shrink(),
1165+
actions: <Widget>[
1166+
SizedBox.shrink(),
1167+
],
1168+
),
1169+
)
1170+
),
1171+
);
1172+
1173+
final Size size = tester.getSize(find.byType(MaterialBanner));
1174+
expect(size.height, equals(minActionBarHeight));
1175+
});
1176+
1177+
testWidgets('minimumActionBarHeight is respected when presented by ScaffoldMessenger', (WidgetTester tester) async {
1178+
const Key tapTarget = Key('tap-target');
1179+
const double minActionBarHeight = 20.0;
1180+
await tester.pumpWidget(MaterialApp(
1181+
home: Scaffold(
1182+
body: Builder(
1183+
builder: (BuildContext context) {
1184+
return GestureDetector(
1185+
key: tapTarget,
1186+
onTap: () {
1187+
ScaffoldMessenger.of(context).showMaterialBanner(const MaterialBanner(
1188+
content: SizedBox.shrink(),
1189+
padding: EdgeInsets.zero,
1190+
margin: EdgeInsets.zero,
1191+
minActionBarHeight: minActionBarHeight,
1192+
actions: <Widget>[
1193+
SizedBox.shrink()
1194+
],
1195+
));
1196+
},
1197+
behavior: HitTestBehavior.opaque,
1198+
child: const SizedBox(
1199+
height: 100.0,
1200+
width: 100.0,
1201+
),
1202+
);
1203+
},
1204+
),
1205+
),
1206+
));
1207+
await tester.tap(find.byKey(tapTarget));
1208+
await tester.pumpAndSettle();
1209+
1210+
final Size materialBarSize = tester.getSize(find.byType(MaterialBanner));
1211+
expect(materialBarSize.height, equals(minActionBarHeight));
1212+
});
11531213
}
11541214

11551215
Material _getMaterialFromBanner(WidgetTester tester) {

0 commit comments

Comments
 (0)