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

<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"  />
This commit is contained in:
Taha Tesser
2024-05-24 01:42:52 +03:00
committed by GitHub
parent 881e29f94a
commit ec69f001fc
2 changed files with 38 additions and 8 deletions

View File

@@ -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<SnackBarAction> {
}
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<SnackBar> {
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),

View File

@@ -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<TextButton>(
find.widgetWithText(TextButton, 'ACTION'),
).style;
expect(
actionButtonStyle?.overlayColor?.resolve(<MaterialState>{MaterialState.hovered}),
theme.colorScheme.inversePrimary.withOpacity(0.08),
);
});
}
/// Start test for "SnackBar dismiss test".