From 68fa653e59677b615fa29738b32e046b2c0f9a98 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Mon, 11 Nov 2024 21:48:32 +0200 Subject: [PATCH] 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
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 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: () {}, )), ), ); } } ```
### Before https://github.com/user-attachments/assets/f8974af6-5af3-4d7d-9dbe-65bf170d5fa3 ### After https://github.com/user-attachments/assets/89a5315c-c7bd-4456-8594-e2092e43952d --- packages/flutter/lib/src/material/chip.dart | 1 + packages/flutter/test/material/chip_test.dart | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/packages/flutter/lib/src/material/chip.dart b/packages/flutter/lib/src/material/chip.dart index 2392098ed6..628b223667 100644 --- a/packages/flutter/lib/src/material/chip.dart +++ b/packages/flutter/lib/src/material/chip.dart @@ -1407,6 +1407,7 @@ class _RawChipState extends State 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([selectController, enableController]), diff --git a/packages/flutter/test/material/chip_test.dart b/packages/flutter/test/material/chip_test.dart index 029b8c2221..25d0b11a17 100644 --- a/packages/flutter/test/material/chip_test.dart +++ b/packages/flutter/test/material/chip_test.dart @@ -6039,6 +6039,49 @@ void main() { expect(tester.widget(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 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 {