Skip to content

Commit

Permalink
Fix: Added margin parameter for MaterialBanner class (#119005)
Browse files Browse the repository at this point in the history
* Fix: Added Margin Parameter for Material Banner

* Fix: Comment for default value added and test improved

* Fix: Comment updated

* Fix: Comment added
  • Loading branch information
hasnentai authored Jan 26, 2023
1 parent c9affdb commit 7d3b762
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/flutter/lib/src/material/banner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class MaterialBanner extends StatefulWidget {
this.shadowColor,
this.dividerColor,
this.padding,
this.margin,
this.leadingPadding,
this.forceActionsBelow = false,
this.overflowAlignment = OverflowBarAlignment.end,
Expand Down Expand Up @@ -185,6 +186,12 @@ class MaterialBanner extends StatefulWidget {
/// `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)`.
Expand Down Expand Up @@ -237,6 +244,7 @@ class MaterialBanner extends StatefulWidget {
leading: leading,
backgroundColor: backgroundColor,
padding: padding,
margin: margin,
leadingPadding: leadingPadding,
forceActionsBelow: forceActionsBelow,
overflowAlignment: overflowAlignment,
Expand Down Expand Up @@ -318,6 +326,7 @@ class _MaterialBannerState extends State<MaterialBanner> {
);

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!;
Expand All @@ -334,7 +343,7 @@ class _MaterialBannerState extends State<MaterialBanner> {
?? defaults.contentTextStyle;

Widget materialBanner = Container(
margin: EdgeInsets.only(bottom: elevation > 0 ? 10.0 : 0.0),
margin: margin,
child: Material(
elevation: elevation,
color: backgroundColor,
Expand Down
22 changes: 22 additions & 0 deletions packages/flutter/test/material/banner_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,28 @@ void main() {
),
);
});

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) {
Expand Down

0 comments on commit 7d3b762

Please sign in to comment.