From 15cb1f84d72643d6f8c1111cec5b4cad962ec61b Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Mon, 17 Apr 2023 23:41:55 +0300 Subject: [PATCH] Fix Chip highlight color isn't drawn on top of the background color (#124673) --- packages/flutter/lib/src/material/chip.dart | 3 +- packages/flutter/test/material/chip_test.dart | 51 ++++++++++++++++++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/material/chip.dart b/packages/flutter/lib/src/material/chip.dart index cf342eebcbc..3eeac224f8a 100644 --- a/packages/flutter/lib/src/material/chip.dart +++ b/packages/flutter/lib/src/material/chip.dart @@ -14,6 +14,7 @@ import 'colors.dart'; import 'constants.dart'; import 'debug.dart'; import 'icons.dart'; +import 'ink_decoration.dart'; import 'ink_well.dart'; import 'material.dart'; import 'material_localizations.dart'; @@ -1210,7 +1211,7 @@ class _RawChipState extends State with MaterialStateMixin, TickerProvid child: AnimatedBuilder( animation: Listenable.merge([selectController, enableController]), builder: (BuildContext context, Widget? child) { - return Container( + return Ink( decoration: ShapeDecoration( shape: resolvedShape, color: _getBackgroundColor(theme, chipTheme, chipDefaults), diff --git a/packages/flutter/test/material/chip_test.dart b/packages/flutter/test/material/chip_test.dart index b1b9abfa564..46aa1ecf39d 100644 --- a/packages/flutter/test/material/chip_test.dart +++ b/packages/flutter/test/material/chip_test.dart @@ -57,7 +57,7 @@ dynamic getRenderChip(WidgetTester tester) { if (!tester.any(findRenderChipElement())) { return null; } - final Element element = tester.element(findRenderChipElement()); + final Element element = tester.element(findRenderChipElement().first); return element.renderObject; } @@ -3284,6 +3284,55 @@ void main() { expect(tester.takeException(), isNull); }); + + testWidgets('Chip background color and shape are drawn on Ink', (WidgetTester tester) async { + const Color backgroundColor = Color(0xff00ff00); + const OutlinedBorder shape = ContinuousRectangleBorder(); + + await tester.pumpWidget(wrapForChip( + child: const RawChip( + label: Text('text'), + backgroundColor: backgroundColor, + shape: shape, + ), + )); + + final Ink ink = tester.widget(find.descendant( + of: find.byType(RawChip), + matching: find.byType(Ink), + )); + final ShapeDecoration decoration = ink.decoration! as ShapeDecoration; + expect(decoration.color, backgroundColor); + expect(decoration.shape, shape); + }); + + testWidgets('Chip highlight color is drawn on top of the backgroundColor', (WidgetTester tester) async { + final FocusNode focusNode = FocusNode(debugLabel: 'RawChip'); + tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional; + const Color backgroundColor = Color(0xff00ff00); + + await tester.pumpWidget(wrapForChip( + child: RawChip( + label: const Text('text'), + backgroundColor: backgroundColor, + autofocus: true, + focusNode: focusNode, + onPressed: () {}, + ), + )); + + await tester.pumpAndSettle(); + + expect(focusNode.hasPrimaryFocus, isTrue); + expect( + find.byType(Material).last, + paints + // Background color is drawn first. + ..rrect(color: backgroundColor) + // Highlight color is drawn on top of the background color. + ..rect(color: const Color(0x1f000000)), + ); + }); } class _MaterialStateOutlinedBorder extends StadiumBorder implements MaterialStateOutlinedBorder {