Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 7d3b762

Browse files
authored
Fix: Added margin parameter for MaterialBanner class (#119005)
* Fix: Added Margin Parameter for Material Banner * Fix: Comment for default value added and test improved * Fix: Comment updated * Fix: Comment added
1 parent c9affdb commit 7d3b762

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class MaterialBanner extends StatefulWidget {
106106
this.shadowColor,
107107
this.dividerColor,
108108
this.padding,
109+
this.margin,
109110
this.leadingPadding,
110111
this.forceActionsBelow = false,
111112
this.overflowAlignment = OverflowBarAlignment.end,
@@ -185,6 +186,12 @@ class MaterialBanner extends StatefulWidget {
185186
/// `EdgeInsetsDirectional.only(start: 16.0, top: 2.0)`.
186187
final EdgeInsetsGeometry? padding;
187188

189+
/// Empty space to surround the [MaterialBanner].
190+
///
191+
/// If the [margin] is null then this defaults to
192+
/// 0 if the banner's [elevation] is 0, 10 otherwise.
193+
final EdgeInsetsGeometry? margin;
194+
188195
/// The amount of space by which to inset the [leading] widget.
189196
///
190197
/// This defaults to `EdgeInsetsDirectional.only(end: 16.0)`.
@@ -237,6 +244,7 @@ class MaterialBanner extends StatefulWidget {
237244
leading: leading,
238245
backgroundColor: backgroundColor,
239246
padding: padding,
247+
margin: margin,
240248
leadingPadding: leadingPadding,
241249
forceActionsBelow: forceActionsBelow,
242250
overflowAlignment: overflowAlignment,
@@ -318,6 +326,7 @@ class _MaterialBannerState extends State<MaterialBanner> {
318326
);
319327

320328
final double elevation = widget.elevation ?? bannerTheme.elevation ?? 0.0;
329+
final EdgeInsetsGeometry margin = widget.margin ?? EdgeInsets.only(bottom: elevation > 0 ? 10.0 : 0.0);
321330
final Color backgroundColor = widget.backgroundColor
322331
?? bannerTheme.backgroundColor
323332
?? defaults.backgroundColor!;
@@ -334,7 +343,7 @@ class _MaterialBannerState extends State<MaterialBanner> {
334343
?? defaults.contentTextStyle;
335344

336345
Widget materialBanner = Container(
337-
margin: EdgeInsets.only(bottom: elevation > 0 ? 10.0 : 0.0),
346+
margin: margin,
338347
child: Material(
339348
elevation: elevation,
340349
color: backgroundColor,

packages/flutter/test/material/banner_test.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,28 @@ void main() {
11051105
),
11061106
);
11071107
});
1108+
1109+
testWidgets('Custom Margin respected', (WidgetTester tester) async {
1110+
const EdgeInsets margin = EdgeInsets.all(30);
1111+
await tester.pumpWidget(
1112+
MaterialApp(
1113+
home: MaterialBanner(
1114+
margin: margin,
1115+
content: const Text('I am a banner'),
1116+
actions: <Widget>[
1117+
TextButton(
1118+
child: const Text('Action'),
1119+
onPressed: () { },
1120+
),
1121+
],
1122+
),
1123+
),
1124+
);
1125+
1126+
final Offset topLeft = tester.getTopLeft(find.descendant(of: find.byType(MaterialBanner), matching: find.byType(Material)).first);
1127+
/// Compare the offset of banner from top left
1128+
expect(topLeft.dx, margin.left);
1129+
});
11081130
}
11091131

11101132
Material _getMaterialFromBanner(WidgetTester tester) {

0 commit comments

Comments
 (0)