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
diff --git a/packages/flutter/lib/src/material/banner.dart b/packages/flutter/lib/src/material/banner.dart
index 867326a..b687be8 100644
--- a/packages/flutter/lib/src/material/banner.dart
+++ b/packages/flutter/lib/src/material/banner.dart
@@ -106,6 +106,7 @@
this.shadowColor,
this.dividerColor,
this.padding,
+ this.margin,
this.leadingPadding,
this.forceActionsBelow = false,
this.overflowAlignment = OverflowBarAlignment.end,
@@ -185,6 +186,12 @@
/// `EdgeInsetsDirectional.only(start: 16.0, top: 2.0)`.
final EdgeInsetsGeometry? padding;
+ /// Empty space to surround the [MaterialBanner].
+ ///
+ /// If the [margin] is null then this defaults to
+ /// 0 if the banner's [elevation] is 0, 10 otherwise.
+ final EdgeInsetsGeometry? margin;
+
/// The amount of space by which to inset the [leading] widget.
///
/// This defaults to `EdgeInsetsDirectional.only(end: 16.0)`.
@@ -237,6 +244,7 @@
leading: leading,
backgroundColor: backgroundColor,
padding: padding,
+ margin: margin,
leadingPadding: leadingPadding,
forceActionsBelow: forceActionsBelow,
overflowAlignment: overflowAlignment,
@@ -318,6 +326,7 @@
);
final double elevation = widget.elevation ?? bannerTheme.elevation ?? 0.0;
+ final EdgeInsetsGeometry margin = widget.margin ?? EdgeInsets.only(bottom: elevation > 0 ? 10.0 : 0.0);
final Color backgroundColor = widget.backgroundColor
?? bannerTheme.backgroundColor
?? defaults.backgroundColor!;
@@ -334,7 +343,7 @@
?? defaults.contentTextStyle;
Widget materialBanner = Container(
- margin: EdgeInsets.only(bottom: elevation > 0 ? 10.0 : 0.0),
+ margin: margin,
child: Material(
elevation: elevation,
color: backgroundColor,
diff --git a/packages/flutter/test/material/banner_test.dart b/packages/flutter/test/material/banner_test.dart
index 4a6b520..0f55080 100644
--- a/packages/flutter/test/material/banner_test.dart
+++ b/packages/flutter/test/material/banner_test.dart
@@ -1105,6 +1105,28 @@
),
);
});
+
+ testWidgets('Custom Margin respected', (WidgetTester tester) async {
+ const EdgeInsets margin = EdgeInsets.all(30);
+ await tester.pumpWidget(
+ MaterialApp(
+ home: MaterialBanner(
+ margin: margin,
+ content: const Text('I am a banner'),
+ actions: <Widget>[
+ TextButton(
+ child: const Text('Action'),
+ onPressed: () { },
+ ),
+ ],
+ ),
+ ),
+ );
+
+ final Offset topLeft = tester.getTopLeft(find.descendant(of: find.byType(MaterialBanner), matching: find.byType(Material)).first);
+ /// Compare the offset of banner from top left
+ expect(topLeft.dx, margin.left);
+ });
}
Material _getMaterialFromBanner(WidgetTester tester) {