Skip to content

Commit

Permalink
Hide InkWell hover highlight when an hovered InkWell is disabled (#11…
Browse files Browse the repository at this point in the history
…8026)
  • Loading branch information
bleroux authored Jan 10, 2023
1 parent 466cb54 commit ad7322d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
21 changes: 14 additions & 7 deletions packages/flutter/lib/src/material/ink_well.dart
Original file line number Diff line number Diff line change
Expand Up @@ -842,21 +842,28 @@ class _InkResponseState extends State<_InkResponseStateWidget>
widget.radius != oldWidget.radius ||
widget.borderRadius != oldWidget.borderRadius ||
widget.highlightShape != oldWidget.highlightShape) {
final InkHighlight? hoverHighLight = _highlights[_HighlightType.hover];
if (hoverHighLight != null) {
hoverHighLight.dispose();
final InkHighlight? hoverHighlight = _highlights[_HighlightType.hover];
if (hoverHighlight != null) {
hoverHighlight.dispose();
updateHighlight(_HighlightType.hover, value: _hovering, callOnHover: false);
}
final InkHighlight? focusHighLight = _highlights[_HighlightType.focus];
if (focusHighLight != null) {
focusHighLight.dispose();
final InkHighlight? focusHighlight = _highlights[_HighlightType.focus];
if (focusHighlight != null) {
focusHighlight.dispose();
// Do not call updateFocusHighlights() here because it is called below
}
}
if (enabled != isWidgetEnabled(oldWidget)) {
statesController.update(MaterialState.disabled, !enabled);
if (!enabled) {
statesController.update(MaterialState.pressed, false);
// Remove the existing hover highlight immediately when enabled is false.
// Do not rely on updateHighlight or InkHighlight.deactivate to not break
// the expected lifecycle which is updating _hovering when the mouse exit.
// Manually updating _hovering here or calling InkHighlight.deactivate
// will lead to onHover not being called or call when it is not allowed.
final InkHighlight? hoverHighlight = _highlights[_HighlightType.hover];
hoverHighlight?.dispose();
}
// Don't call widget.onHover because many widgets, including the button
// widgets, apply setState to an ancestor context from onHover.
Expand Down Expand Up @@ -937,7 +944,7 @@ class _InkResponseState extends State<_InkResponseStateWidget>
_highlights[type] = InkHighlight(
controller: Material.of(context),
referenceBox: referenceBox,
color: resolvedOverlayColor,
color: enabled ? resolvedOverlayColor : resolvedOverlayColor.withAlpha(0),
shape: widget.highlightShape,
radius: widget.radius,
borderRadius: widget.borderRadius,
Expand Down
50 changes: 50 additions & 0 deletions packages/flutter/test/material/ink_well_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1722,6 +1722,56 @@ testWidgets('InkResponse radius can be updated', (WidgetTester tester) async {
expect(hover, false);
});

testWidgets('hovered ink well draws a transparent highlight when disabled', (WidgetTester tester) async {
Widget buildFrame({ required bool enabled }) {
return Material(
child: Directionality(
textDirection: TextDirection.ltr,
child: Center(
child: SizedBox(
width: 100,
height: 100,
child: InkWell(
onTap: enabled ? () { } : null,
onHover: (bool value) { },
hoverColor: const Color(0xff00ff00),
),
),
),
),
);
}

await tester.pumpWidget(buildFrame(enabled: true));
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
await gesture.addPointer();

// Hover the enabled InkWell.
await gesture.moveTo(tester.getCenter(find.byType(InkWell)));
await tester.pumpAndSettle();
expect(
find.byType(Material),
paints
..rect(
color: const Color(0xff00ff00),
rect: const Rect.fromLTRB(350.0, 250.0, 450.0, 350.0),
)
);

// Disable the hovered InkWell.
await tester.pumpWidget(buildFrame(enabled: false));
await tester.pumpAndSettle();
expect(
find.byType(Material),
paints
..rect(
color: const Color(0x0000ff00),
rect: const Rect.fromLTRB(350.0, 250.0, 450.0, 350.0),
)
);
});


testWidgets('Changing InkWell.enabled should not trigger TextButton setState()', (WidgetTester tester) async {
Widget buildFrame({ required bool enabled }) {
return Material(
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/test/material/list_tile_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ void main() {
paints
..rect()
..rect(
color: Colors.orange[500],
color: Colors.orange[500]!.withAlpha(0),
rect: const Rect.fromLTRB(350.0, 250.0, 450.0, 350.0),
)
..rect(
Expand Down

0 comments on commit ad7322d

Please sign in to comment.