From e8343c0ec42194a5f25d6f8b9c81d476319555f8 Mon Sep 17 00:00:00 2001 From: Albert Wolszon Date: Thu, 15 Oct 2020 00:47:03 +0200 Subject: [PATCH] Add missing margin to SnackBarAction (#67167) --- .../flutter/lib/src/material/snack_bar.dart | 17 ++-- .../flutter/test/material/snack_bar_test.dart | 89 +++++++++++++++++-- 2 files changed, 92 insertions(+), 14 deletions(-) diff --git a/packages/flutter/lib/src/material/snack_bar.dart b/packages/flutter/lib/src/material/snack_bar.dart index be039e297c..f9afe083ab 100644 --- a/packages/flutter/lib/src/material/snack_bar.dart +++ b/packages/flutter/lib/src/material/snack_bar.dart @@ -427,6 +427,8 @@ class _SnackBarState extends State { 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 @@ class _SnackBarState extends State { ), ), 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 68e5d229aa..9f3f803cd4 100644 --- a/packages/flutter/test/material/snack_bar_test.dart +++ b/packages/flutter/test/material/snack_bar_test.dart @@ -972,8 +972,8 @@ void main() { 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 @@ void main() { 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 @@ void main() { 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 @@ void main() { 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 @@ void main() { 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 {