Add missing margin to SnackBarAction (#67167)
diff --git a/packages/flutter/lib/src/material/snack_bar.dart b/packages/flutter/lib/src/material/snack_bar.dart
index be039e2..f9afe08 100644
--- a/packages/flutter/lib/src/material/snack_bar.dart
+++ b/packages/flutter/lib/src/material/snack_bar.dart
@@ -427,6 +427,8 @@
final EdgeInsetsGeometry padding = widget.padding
?? EdgeInsetsDirectional.only(start: horizontalPadding, end: widget.action != null ? 0 : horizontalPadding);
+ final double actionHorizontalMargin = (widget.padding?.resolve(TextDirection.ltr).right ?? horizontalPadding) / 2;
+
final CurvedAnimation heightAnimation = CurvedAnimation(parent: widget.animation!, curve: _snackBarHeightCurve);
final CurvedAnimation fadeInAnimation = CurvedAnimation(parent: widget.animation!, curve: _snackBarFadeInCurve);
final CurvedAnimation fadeOutAnimation = CurvedAnimation(
@@ -450,14 +452,17 @@
),
),
if (widget.action != null)
- TextButtonTheme(
- data: TextButtonThemeData(
- style: TextButton.styleFrom(
- primary: buttonColor,
- padding: EdgeInsets.symmetric(horizontal: horizontalPadding),
+ Padding(
+ padding: EdgeInsets.symmetric(horizontal: actionHorizontalMargin),
+ child: TextButtonTheme(
+ data: TextButtonThemeData(
+ style: TextButton.styleFrom(
+ primary: buttonColor,
+ padding: EdgeInsets.symmetric(horizontal: horizontalPadding),
+ ),
),
+ child: widget.action!,
),
- child: widget.action!,
),
],
),
diff --git a/packages/flutter/test/material/snack_bar_test.dart b/packages/flutter/test/material/snack_bar_test.dart
index 68e5d22..9f3f803 100644
--- a/packages/flutter/test/material/snack_bar_test.dart
+++ b/packages/flutter/test/material/snack_bar_test.dart
@@ -972,8 +972,8 @@
expect(textBottomLeft.dx - snackBarBottomLeft.dx, 24.0 + 10.0); // margin + left padding
expect(snackBarBottomLeft.dy - textBottomLeft.dy, 17.0 + 40.0); // margin + bottom padding
- expect(actionTextBottomLeft.dx - textBottomRight.dx, 24.0);
- expect(snackBarBottomRight.dx - actionTextBottomRight.dx, 24.0 + 30.0); // margin + right padding
+ expect(actionTextBottomLeft.dx - textBottomRight.dx, 24.0 + 12.0); // action padding + margin
+ expect(snackBarBottomRight.dx - actionTextBottomRight.dx, 24.0 + 12.0 + 30.0); // action (padding + margin) + right padding
expect(snackBarBottomRight.dy - actionTextBottomRight.dy, 17.0 + 40.0); // margin + bottom padding
});
@@ -1027,8 +1027,8 @@
expect(textBottomLeft.dx - snackBarBottomLeft.dx, 24.0 + 10.0); // margin + left padding
expect(snackBarBottomLeft.dy - textBottomLeft.dy, 17.0); // margin (with no bottom padding)
- expect(actionTextBottomLeft.dx - textBottomRight.dx, 24.0);
- expect(snackBarBottomRight.dx - actionTextBottomRight.dx, 24.0 + 30.0); // margin + right padding
+ expect(actionTextBottomLeft.dx - textBottomRight.dx, 24.0 + 12.0); // action padding + margin
+ expect(snackBarBottomRight.dx - actionTextBottomRight.dx, 24.0 + 12.0 + 30.0); // action (padding + margin) + right padding
expect(snackBarBottomRight.dy - actionTextBottomRight.dy, 17.0); // margin (with no bottom padding)
});
@@ -1132,8 +1132,8 @@
expect(textBottomLeft.dx - snackBarBottomLeft.dx, 31.0 + 10.0); // margin + left padding
expect(snackBarBottomLeft.dy - textBottomLeft.dy, 27.0); // margin (with no bottom padding)
- expect(actionTextBottomLeft.dx - textBottomRight.dx, 16.0);
- expect(snackBarBottomRight.dx - actionTextBottomRight.dx, 31.0 + 30.0); // margin + right padding
+ expect(actionTextBottomLeft.dx - textBottomRight.dx, 16.0 + 8.0); // action padding + margin
+ expect(snackBarBottomRight.dx - actionTextBottomRight.dx, 31.0 + 30.0 + 8.0); // margin + right (padding + margin)
expect(snackBarBottomRight.dy - actionTextBottomRight.dy, 27.0); // margin (with no bottom padding)
});
@@ -1190,8 +1190,8 @@
expect(textBottomLeft.dx - snackBarBottomLeft.dx, 31.0 + 10.0); // margin + left padding
expect(snackBarBottomLeft.dy - textBottomLeft.dy, 27.0); // margin (with no bottom padding)
- expect(actionTextBottomLeft.dx - textBottomRight.dx, 16.0);
- expect(snackBarBottomRight.dx - actionTextBottomRight.dx, 31.0 + 30.0); // margin + right padding
+ expect(actionTextBottomLeft.dx - textBottomRight.dx, 16.0 + 8.0); // action (margin + padding)
+ expect(snackBarBottomRight.dx - actionTextBottomRight.dx, 31.0 + 30.0 + 8.0); // margin + right (padding + margin)
expect(snackBarBottomRight.dy - actionTextBottomRight.dy, 27.0); // margin (with no bottom padding)
});
@@ -1942,6 +1942,79 @@
expect(snackBarBottomRight.dy, equals(fabTopRight.dy));
},
);
+
+ testWidgets(
+ 'SnackBar has correct end padding when it contains an action with fixed behavior',
+ (WidgetTester tester) async {
+ await tester.pumpWidget(
+ MaterialApp(
+ home: Scaffold(
+ body: Builder(
+ builder: (BuildContext context) {
+ return GestureDetector(
+ onTap: () {
+ Scaffold.of(context)!.showSnackBar(SnackBar(
+ content: const Text('Some content'),
+ behavior: SnackBarBehavior.fixed,
+ action: SnackBarAction(
+ label: 'ACTION',
+ onPressed: () {},
+ ),
+ ));
+ },
+ child: const Text('X'),
+ );
+ },
+ ),
+ ),
+ ),
+ );
+
+ await tester.tap(find.text('X'));
+ await tester.pumpAndSettle();
+
+ final Offset snackBarTopRight = tester.getTopRight(find.byType(SnackBar));
+ final Offset actionTopRight = tester.getTopRight(find.byType(SnackBarAction));
+
+ expect(snackBarTopRight.dx - actionTopRight.dx, 12.0);
+ },
+ );
+
+ testWidgets(
+ 'SnackBar has correct end padding when it contains an action with floating behavior',
+ (WidgetTester tester) async {
+ await tester.pumpWidget(
+ MaterialApp(
+ home: Scaffold(
+ body: Builder(
+ builder: (BuildContext context) {
+ return GestureDetector(
+ onTap: () {
+ Scaffold.of(context)!.showSnackBar(SnackBar(
+ content: const Text('Some content'),
+ behavior: SnackBarBehavior.floating,
+ action: SnackBarAction(
+ label: 'ACTION',
+ onPressed: () {},
+ ),
+ ));
+ },
+ child: const Text('X'),
+ );
+ },
+ ),
+ ),
+ ),
+ );
+
+ await tester.tap(find.text('X'));
+ await tester.pumpAndSettle();
+ final Offset snackBarTopRight = tester.getTopRight(find.byType(SnackBar));
+ final Offset actionTopRight = tester.getTopRight(find.byType(SnackBarAction));
+
+ expect(snackBarTopRight.dx - actionTopRight.dx, 8.0 + 15.0); // button margin + horizontal scaffold outside margin
+ },
+ );
});
testWidgets('SnackBars hero across transitions when using ScaffoldMessenger', (WidgetTester tester) async {