Skip to content

Commit ec69f00

Browse files
authored
Fix SnackBar action text button overlay color (#148961)
fixes [`SnackBar` action hover state background too hard to see](flutter/flutter#141343) ### Code sample <details> <summary>expand to view the code sample</summary> ```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'), ); } } ``` </details> ### Before <img src="https://github.com/flutter/flutter/assets/48603081/88b53c92-6184-4faf-88e1-ac70f78993b3"/> ### After <img src="https://github.com/flutter/flutter/assets/48603081/3e03c903-90c0-4da4-b49a-0070208d56d1" />
1 parent 881e29f commit ec69f00

2 files changed

Lines changed: 38 additions & 8 deletions

File tree

packages/flutter/lib/src/material/snack_bar.dart

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import 'package:flutter/rendering.dart';
66
import 'package:flutter/widgets.dart';
77

8-
import 'button_style.dart';
98
import 'color_scheme.dart';
109
import 'colors.dart';
1110
import 'icon_button.dart';
@@ -204,10 +203,11 @@ class _SnackBarActionState extends State<SnackBarAction> {
204203
}
205204

206205
return TextButton(
207-
style: ButtonStyle(
208-
foregroundColor: resolveForegroundColor(),
209-
backgroundColor: resolveBackgroundColor(),
210-
),
206+
style: TextButton.styleFrom(overlayColor: resolveForegroundColor())
207+
.copyWith(
208+
foregroundColor: resolveForegroundColor(),
209+
backgroundColor: resolveBackgroundColor(),
210+
),
211211
onPressed: _haveTriggeredAction ? null : _handlePressed,
212212
child: Text(widget.label),
213213
);
@@ -671,9 +671,6 @@ class _SnackBarState extends State<SnackBar> {
671671
final double actionHorizontalMargin = (widget.padding?.resolve(TextDirection.ltr).right ?? horizontalPadding) / 2;
672672
final double iconHorizontalMargin = (widget.padding?.resolve(TextDirection.ltr).right ?? horizontalPadding) / 12.0;
673673

674-
675-
676-
677674
final IconButton? iconButton = showCloseIcon
678675
? IconButton(
679676
icon: const Icon(Icons.close),

packages/flutter/test/material/snack_bar_test.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4045,6 +4045,39 @@ testWidgets('SnackBarAction backgroundColor works as a Color', (WidgetTester tes
40454045

40464046
expect(completer.isCompleted, false);
40474047
});
4048+
4049+
testWidgets('Action text button uses correct overlay color', (WidgetTester tester) async {
4050+
final ThemeData theme = ThemeData();
4051+
await tester.pumpWidget(MaterialApp(
4052+
theme: theme,
4053+
home: Scaffold(
4054+
body: Builder(
4055+
builder: (BuildContext context) {
4056+
return GestureDetector(
4057+
onTap: () {
4058+
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
4059+
content: const Text('I am a snack bar.'),
4060+
action: SnackBarAction(label: 'ACTION', onPressed: () { }),
4061+
));
4062+
},
4063+
child: const Text('X'),
4064+
);
4065+
},
4066+
),
4067+
),
4068+
));
4069+
4070+
await tester.tap(find.text('X'));
4071+
await tester.pumpAndSettle();
4072+
4073+
final ButtonStyle? actionButtonStyle = tester.widget<TextButton>(
4074+
find.widgetWithText(TextButton, 'ACTION'),
4075+
).style;
4076+
expect(
4077+
actionButtonStyle?.overlayColor?.resolve(<MaterialState>{MaterialState.hovered}),
4078+
theme.colorScheme.inversePrimary.withOpacity(0.08),
4079+
);
4080+
});
40484081
}
40494082

40504083
/// Start test for "SnackBar dismiss test".

0 commit comments

Comments
 (0)