From ec69f001fc4a6221f5e3c0aa4a92c7bb76a7aa70 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Fri, 24 May 2024 01:42:52 +0300 Subject: [PATCH] Fix `SnackBar` action text button overlay color (#148961) fixes [`SnackBar` action hover state background too hard to see](https://github.com/flutter/flutter/issues/141343) ### Code sample
expand to view the code sample ```dart import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return const MaterialApp( home: Scaffold( body: Center( child: ScaffoldButton(), ), ), ); } } class ScaffoldButton extends StatelessWidget { const ScaffoldButton({super.key}); @override Widget build(BuildContext context) { return TextButton( onPressed: () { ScaffoldMessenger.of(context).showSnackBar( SnackBar( behavior: SnackBarBehavior.floating, width: 320, content: const Text('This is a snackbar!'), action: SnackBarAction( label: 'Close', onPressed: () {}, ), ), ); }, child: const Text('Launch snackbar'), ); } } ```
### Before ### After --- .../flutter/lib/src/material/snack_bar.dart | 13 +++----- .../flutter/test/material/snack_bar_test.dart | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/packages/flutter/lib/src/material/snack_bar.dart b/packages/flutter/lib/src/material/snack_bar.dart index b0a466ed8a..4911b7b96c 100644 --- a/packages/flutter/lib/src/material/snack_bar.dart +++ b/packages/flutter/lib/src/material/snack_bar.dart @@ -5,7 +5,6 @@ import 'package:flutter/rendering.dart'; import 'package:flutter/widgets.dart'; -import 'button_style.dart'; import 'color_scheme.dart'; import 'colors.dart'; import 'icon_button.dart'; @@ -204,10 +203,11 @@ class _SnackBarActionState extends State { } return TextButton( - style: ButtonStyle( - foregroundColor: resolveForegroundColor(), - backgroundColor: resolveBackgroundColor(), - ), + style: TextButton.styleFrom(overlayColor: resolveForegroundColor()) + .copyWith( + foregroundColor: resolveForegroundColor(), + backgroundColor: resolveBackgroundColor(), + ), onPressed: _haveTriggeredAction ? null : _handlePressed, child: Text(widget.label), ); @@ -671,9 +671,6 @@ class _SnackBarState extends State { final double actionHorizontalMargin = (widget.padding?.resolve(TextDirection.ltr).right ?? horizontalPadding) / 2; final double iconHorizontalMargin = (widget.padding?.resolve(TextDirection.ltr).right ?? horizontalPadding) / 12.0; - - - final IconButton? iconButton = showCloseIcon ? IconButton( icon: const Icon(Icons.close), diff --git a/packages/flutter/test/material/snack_bar_test.dart b/packages/flutter/test/material/snack_bar_test.dart index 105d197d7c..703ee43459 100644 --- a/packages/flutter/test/material/snack_bar_test.dart +++ b/packages/flutter/test/material/snack_bar_test.dart @@ -4045,6 +4045,39 @@ testWidgets('SnackBarAction backgroundColor works as a Color', (WidgetTester tes expect(completer.isCompleted, false); }); + + testWidgets('Action text button uses correct overlay color', (WidgetTester tester) async { + final ThemeData theme = ThemeData(); + await tester.pumpWidget(MaterialApp( + theme: theme, + home: Scaffold( + body: Builder( + builder: (BuildContext context) { + return GestureDetector( + onTap: () { + ScaffoldMessenger.of(context).showSnackBar(SnackBar( + content: const Text('I am a snack bar.'), + action: SnackBarAction(label: 'ACTION', onPressed: () { }), + )); + }, + child: const Text('X'), + ); + }, + ), + ), + )); + + await tester.tap(find.text('X')); + await tester.pumpAndSettle(); + + final ButtonStyle? actionButtonStyle = tester.widget( + find.widgetWithText(TextButton, 'ACTION'), + ).style; + expect( + actionButtonStyle?.overlayColor?.resolve({MaterialState.hovered}), + theme.colorScheme.inversePrimary.withOpacity(0.08), + ); + }); } /// Start test for "SnackBar dismiss test".