From 9f98f63585362ca0fab9c339b2878932dcd0c9e1 Mon Sep 17 00:00:00 2001 From: Bruno Leroux Date: Sat, 8 Apr 2023 01:02:14 +0200 Subject: [PATCH] Fix InkWell ripple visible on right click when not expected (#124386) Right clicking an InkWell with no secondary tap handlers will no longer cause a ripple. --- .../flutter/lib/src/material/ink_well.dart | 29 ++++++++++++------- .../flutter/test/material/ink_well_test.dart | 26 +++++++++++++++++ 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/packages/flutter/lib/src/material/ink_well.dart b/packages/flutter/lib/src/material/ink_well.dart index c5147df6d8..78d8425887 100644 --- a/packages/flutter/lib/src/material/ink_well.dart +++ b/packages/flutter/lib/src/material/ink_well.dart @@ -1214,17 +1214,26 @@ class _InkResponseState extends State<_InkResponseStateWidget> } bool isWidgetEnabled(_InkResponseStateWidget widget) { + return _primaryButtonEnabled(widget) || _secondaryButtonEnabled(widget); + } + + bool _primaryButtonEnabled(_InkResponseStateWidget widget) { return widget.onTap != null || widget.onDoubleTap != null || widget.onLongPress != null || widget.onTapUp != null - || widget.onTapDown != null - || widget.onSecondaryTap != null + || widget.onTapDown != null; + } + + bool _secondaryButtonEnabled(_InkResponseStateWidget widget) { + return widget.onSecondaryTap != null || widget.onSecondaryTapUp != null || widget.onSecondaryTapDown != null; } bool get enabled => isWidgetEnabled(widget); + bool get _primaryEnabled => _primaryButtonEnabled(widget); + bool get _secondaryEnabled => _secondaryButtonEnabled(widget); void handleMouseEnter(PointerEnterEvent event) { _hovering = true; @@ -1305,16 +1314,16 @@ class _InkResponseState extends State<_InkResponseStateWidget> onTap: widget.excludeFromSemantics || widget.onTap == null ? null : simulateTap, onLongPress: widget.excludeFromSemantics || widget.onLongPress == null ? null : simulateLongPress, child: GestureDetector( - onTapDown: enabled ? handleTapDown : null, - onTapUp: enabled ? handleTapUp : null, - onTap: enabled ? handleTap : null, - onTapCancel: enabled ? handleTapCancel : null, + onTapDown: _primaryEnabled ? handleTapDown : null, + onTapUp: _primaryEnabled ? handleTapUp : null, + onTap: _primaryEnabled ? handleTap : null, + onTapCancel: _primaryEnabled ? handleTapCancel : null, onDoubleTap: widget.onDoubleTap != null ? handleDoubleTap : null, onLongPress: widget.onLongPress != null ? handleLongPress : null, - onSecondaryTapDown: enabled ? handleSecondaryTapDown : null, - onSecondaryTapUp: enabled ? handleSecondaryTapUp: null, - onSecondaryTap: enabled ? handleSecondaryTap : null, - onSecondaryTapCancel: enabled ? handleSecondaryTapCancel : null, + onSecondaryTapDown: _secondaryEnabled ? handleSecondaryTapDown : null, + onSecondaryTapUp: _secondaryEnabled ? handleSecondaryTapUp: null, + onSecondaryTap: _secondaryEnabled ? handleSecondaryTap : null, + onSecondaryTapCancel: _secondaryEnabled ? handleSecondaryTapCancel : null, behavior: HitTestBehavior.opaque, excludeFromSemantics: true, child: widget.child, diff --git a/packages/flutter/test/material/ink_well_test.dart b/packages/flutter/test/material/ink_well_test.dart index 7b3992182a..583d8be5d0 100644 --- a/packages/flutter/test/material/ink_well_test.dart +++ b/packages/flutter/test/material/ink_well_test.dart @@ -2137,4 +2137,30 @@ testWidgets('InkResponse radius can be updated', (WidgetTester tester) async { expect(log, equals(['secondary-tap-down', 'secondary-tap-cancel'])); }); + + // Regression test for https://github.com/flutter/flutter/issues/124328. + testWidgets('InkWell secondary tap should not draw a splash when no secondary callbacks are defined', (WidgetTester tester) async { + await tester.pumpWidget(Directionality( + textDirection: TextDirection.ltr, + child: Material( + child: Center( + child: InkWell( + onTap: () {}, + ), + ), + ), + )); + + final TestGesture gesture = await tester.startGesture( + tester.getRect(find.byType(InkWell)).center, + buttons: kSecondaryButton, + ); + await tester.pump(const Duration(milliseconds: 200)); + + // No splash should be painted. + final RenderObject inkFeatures = tester.allRenderObjects.firstWhere((RenderObject object) => object.runtimeType.toString() == '_RenderInkFeatures'); + expect(inkFeatures, paintsExactlyCountTimes(#drawCircle, 0)); + + await gesture.up(); + }); }