Fix Chip draws InkWell.hoverColor is drawn on top of the provided background color with hovered state (#158454)

Fixes [Setting hoverColor on ThemeData breaks Chip hover functionality](https://github.com/flutter/flutter/issues/157622)

### Description

The bug is caused by `InkWell.hoverColor`. When `ThemeData.hoverColor` is provided, its drawn on top. To fix this, we can override `InkWell.hoverColor` when Chip state color is provided.

### 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 MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        hoverColor: const Color(0xFFFF0000),
      ),
      home: Scaffold(
        body: Center(
            child: RawChip(
          color: WidgetStateProperty.resolveWith((states) {
            if (states.contains(WidgetState.hovered)) {
              return const Color(0xFF00fF00);
            }

            return const Color(0xFF0000FF);
          }),
          label: const Text('RawChip'),
          onPressed: () {},
        )),
      ),
    );
  }
}
```

</details>

### Before

https://github.com/user-attachments/assets/f8974af6-5af3-4d7d-9dbe-65bf170d5fa3

### After

https://github.com/user-attachments/assets/89a5315c-c7bd-4456-8594-e2092e43952d
This commit is contained in:
Taha Tesser
2024-11-11 21:48:32 +02:00
committed by GitHub
parent c36ad608da
commit 68fa653e59
2 changed files with 44 additions and 0 deletions

View File

@@ -1407,6 +1407,7 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid
onTapDown: canTap ? _handleTapDown : null,
onTapCancel: canTap ? _handleTapCancel : null,
onHover: canTap ? updateMaterialState(MaterialState.hovered) : null,
hoverColor: (widget.color ?? chipTheme.color) == null ? null : Colors.transparent,
customBorder: resolvedShape,
child: AnimatedBuilder(
animation: Listenable.merge(<Listenable>[selectController, enableController]),

View File

@@ -6039,6 +6039,49 @@ void main() {
expect(tester.widget<RawChip>(find.byType(RawChip)).chipAnimationStyle, chipAnimationStyle);
});
// Regression test for https://github.com/flutter/flutter/issues/157622.
testWidgets('Chip does not glitch on hover when providing ThemeData.hoverColor', (WidgetTester tester) async {
const Color themeDataHoverColor = Color(0xffff0000);
const Color hoverColor = Color(0xff00ff00);
const Color backgroundColor = Color(0xff0000ff);
await tester.pumpWidget(wrapForChip(
theme: ThemeData(hoverColor: themeDataHoverColor),
child: Center(
child: RawChip(
color: WidgetStateProperty.resolveWith((Set<WidgetState> states) {
if (states.contains(WidgetState.hovered)) {
return hoverColor;
}
return backgroundColor;
}),
label: const Text('Chip'),
onPressed: () {},
),
),
));
expect(getMaterialBox(tester), paints..rrect(color: backgroundColor));
// Hover over the chip.
final Offset center = tester.getCenter(find.byType(RawChip));
final TestGesture gesture = await tester.createGesture(
kind: PointerDeviceKind.mouse,
);
await gesture.addPointer();
await gesture.moveTo(center);
addTearDown(gesture.removePointer);
await tester.pumpAndSettle();
expect(
getMaterialBox(tester),
paints..rrect(color: hoverColor)..rect(color: Colors.transparent),
);
expect(
getMaterialBox(tester),
isNot(paints..rrect(color: hoverColor)..rect(color: themeDataHoverColor)),
);
});
}
class _MaterialStateOutlinedBorder extends StadiumBorder implements MaterialStateOutlinedBorder {