diff --git a/packages/flutter/lib/src/widgets/tap_region.dart b/packages/flutter/lib/src/widgets/tap_region.dart index 721b8ee480..f4bdf8d89e 100644 --- a/packages/flutter/lib/src/widgets/tap_region.dart +++ b/packages/flutter/lib/src/widgets/tap_region.dart @@ -234,7 +234,7 @@ class RenderTapRegionSurface extends RenderProxyBoxWithHitTestBehavior implement return true; }(), 'A RenderTapRegion was registered when it was disabled.'); - if (event is! PointerDownEvent || event.buttons != kPrimaryButton) { + if (event is! PointerDownEvent) { return; } diff --git a/packages/flutter/test/widgets/tap_region_test.dart b/packages/flutter/test/widgets/tap_region_test.dart index 4f31a9e845..c53983b778 100644 --- a/packages/flutter/test/widgets/tap_region_test.dart +++ b/packages/flutter/test/widgets/tap_region_test.dart @@ -480,4 +480,578 @@ void main() { ])); tappedOutside.clear(); }); + + testWidgetsWithLeakTracking('TapRegionSurface detects outside right click', (WidgetTester tester) async { + final Set tappedOutside = {}; + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Column( + children: [ + const Text('Outside Surface'), + TapRegionSurface( + child: Row( + children: [ + const Text('Outside'), + TapRegion( + onTapOutside: (PointerEvent event) { + tappedOutside.add('No Group'); + }, + child: const Text('No Group'), + ), + TapRegion( + groupId: 1, + onTapOutside: (PointerEvent event) { + tappedOutside.add('Group 1 A'); + }, + child: const Text('Group 1 A'), + ), + TapRegion( + groupId: 1, + onTapOutside: (PointerEvent event) { + tappedOutside.add('Group 1 B'); + }, + child: const Text('Group 1 B'), + ), + ], + ), + ), + ], + ), + ), + ); + + await tester.pump(); + + Future click(Finder finder) async { + final TestGesture gesture = await tester.startGesture( + tester.getCenter(finder), + kind: PointerDeviceKind.mouse, + buttons: kSecondaryButton, + ); + await gesture.up(); + await gesture.removePointer(); + } + + expect(tappedOutside, isEmpty); + + await click(find.text('No Group')); + expect( + tappedOutside, + unorderedEquals({ + 'Group 1 A', + 'Group 1 B', + })); + tappedOutside.clear(); + + await click(find.text('Group 1 A')); + expect( + tappedOutside, + equals({ + 'No Group', + })); + tappedOutside.clear(); + + await click(find.text('Group 1 B')); + expect( + tappedOutside, + equals({ + 'No Group', + })); + tappedOutside.clear(); + + await click(find.text('Outside')); + expect( + tappedOutside, + unorderedEquals({ + 'No Group', + 'Group 1 A', + 'Group 1 B', + })); + tappedOutside.clear(); + + await click(find.text('Outside Surface')); + expect(tappedOutside, isEmpty); + }); + + testWidgetsWithLeakTracking('TapRegionSurface detects outside middle click', (WidgetTester tester) async { + final Set tappedOutside = {}; + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Column( + children: [ + const Text('Outside Surface'), + TapRegionSurface( + child: Row( + children: [ + const Text('Outside'), + TapRegion( + onTapOutside: (PointerEvent event) { + tappedOutside.add('No Group'); + }, + child: const Text('No Group'), + ), + TapRegion( + groupId: 1, + onTapOutside: (PointerEvent event) { + tappedOutside.add('Group 1 A'); + }, + child: const Text('Group 1 A'), + ), + TapRegion( + groupId: 1, + onTapOutside: (PointerEvent event) { + tappedOutside.add('Group 1 B'); + }, + child: const Text('Group 1 B'), + ), + ], + ), + ), + ], + ), + ), + ); + + await tester.pump(); + + Future click(Finder finder) async { + final TestGesture gesture = await tester.startGesture( + tester.getCenter(finder), + kind: PointerDeviceKind.mouse, + buttons: kTertiaryButton, + ); + await gesture.up(); + await gesture.removePointer(); + } + + expect(tappedOutside, isEmpty); + + await click(find.text('No Group')); + expect( + tappedOutside, + unorderedEquals({ + 'Group 1 A', + 'Group 1 B', + })); + tappedOutside.clear(); + + await click(find.text('Group 1 A')); + expect( + tappedOutside, + equals({ + 'No Group', + })); + tappedOutside.clear(); + + await click(find.text('Group 1 B')); + expect( + tappedOutside, + equals({ + 'No Group', + })); + tappedOutside.clear(); + + await click(find.text('Outside')); + expect( + tappedOutside, + unorderedEquals({ + 'No Group', + 'Group 1 A', + 'Group 1 B', + })); + tappedOutside.clear(); + + await click(find.text('Outside Surface')); + expect(tappedOutside, isEmpty); + }); + + testWidgetsWithLeakTracking('TapRegionSurface consumes outside right click when asked', (WidgetTester tester) async { + final Set tappedOutside = {}; + int propagatedTaps = 0; + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Column( + children: [ + const Text('Outside Surface'), + TapRegionSurface( + child: Row( + children: [ + GestureDetector( + onTap: () { + propagatedTaps += 1; + }, + child: const Text('Outside'), + ), + TapRegion( + consumeOutsideTaps: true, + onTapOutside: (PointerEvent event) { + tappedOutside.add('No Group'); + }, + child: const Text('No Group'), + ), + TapRegion( + groupId: 1, + onTapOutside: (PointerEvent event) { + tappedOutside.add('Group 1 A'); + }, + child: const Text('Group 1 A'), + ), + TapRegion( + groupId: 1, + consumeOutsideTaps: true, + onTapOutside: (PointerEvent event) { + tappedOutside.add('Group 1 B'); + }, + child: const Text('Group 1 B'), + ), + ], + ), + ), + ], + ), + ), + ); + + await tester.pump(); + + Future click(Finder finder) async { + final TestGesture gesture = await tester.startGesture( + tester.getCenter(finder), + kind: PointerDeviceKind.mouse, + buttons: kSecondaryButton + ); + await gesture.up(); + await gesture.removePointer(); + } + + expect(tappedOutside, isEmpty); + expect(propagatedTaps, equals(0)); + + await click(find.text('No Group')); + expect( + tappedOutside, + unorderedEquals({ + 'Group 1 A', + 'Group 1 B', + })); + expect(propagatedTaps, equals(0)); + tappedOutside.clear(); + + await click(find.text('Group 1 A')); + expect( + tappedOutside, + equals({ + 'No Group', + })); + expect(propagatedTaps, equals(0)); + tappedOutside.clear(); + + await click(find.text('Group 1 B')); + expect( + tappedOutside, + equals({ + 'No Group', + })); + expect(propagatedTaps, equals(0)); + tappedOutside.clear(); + + await click(find.text('Outside')); + expect( + tappedOutside, + unorderedEquals({ + 'No Group', + 'Group 1 A', + 'Group 1 B', + })); + expect(propagatedTaps, equals(0)); + tappedOutside.clear(); + + await click(find.text('Outside Surface')); + expect(tappedOutside, isEmpty); + }); + + testWidgetsWithLeakTracking('TapRegionSurface consumes outside middle click when asked', (WidgetTester tester) async { + final Set tappedOutside = {}; + int propagatedTaps = 0; + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Column( + children: [ + const Text('Outside Surface'), + TapRegionSurface( + child: Row( + children: [ + GestureDetector( + onTap: () { + propagatedTaps += 1; + }, + child: const Text('Outside'), + ), + TapRegion( + consumeOutsideTaps: true, + onTapOutside: (PointerEvent event) { + tappedOutside.add('No Group'); + }, + child: const Text('No Group'), + ), + TapRegion( + groupId: 1, + onTapOutside: (PointerEvent event) { + tappedOutside.add('Group 1 A'); + }, + child: const Text('Group 1 A'), + ), + TapRegion( + groupId: 1, + consumeOutsideTaps: true, + onTapOutside: (PointerEvent event) { + tappedOutside.add('Group 1 B'); + }, + child: const Text('Group 1 B'), + ), + ], + ), + ), + ], + ), + ), + ); + + await tester.pump(); + + Future click(Finder finder) async { + final TestGesture gesture = await tester.startGesture( + tester.getCenter(finder), + kind: PointerDeviceKind.mouse, + buttons: kTertiaryButton, + ); + await gesture.up(); + await gesture.removePointer(); + } + + expect(tappedOutside, isEmpty); + expect(propagatedTaps, equals(0)); + + await click(find.text('No Group')); + expect( + tappedOutside, + unorderedEquals({ + 'Group 1 A', + 'Group 1 B', + })); + expect(propagatedTaps, equals(0)); + tappedOutside.clear(); + + await click(find.text('Group 1 A')); + expect( + tappedOutside, + equals({ + 'No Group', + })); + expect(propagatedTaps, equals(0)); + tappedOutside.clear(); + + await click(find.text('Group 1 B')); + expect( + tappedOutside, + equals({ + 'No Group', + })); + expect(propagatedTaps, equals(0)); + tappedOutside.clear(); + + await click(find.text('Outside')); + expect( + tappedOutside, + unorderedEquals({ + 'No Group', + 'Group 1 A', + 'Group 1 B', + })); + expect(propagatedTaps, equals(0)); + tappedOutside.clear(); + + await click(find.text('Outside Surface')); + expect(tappedOutside, isEmpty); + }); + + testWidgetsWithLeakTracking('TapRegionSurface detects inside right click', (WidgetTester tester) async { + final Set tappedInside = {}; + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Column( + children: [ + const Text('Outside Surface'), + TapRegionSurface( + child: Row( + children: [ + const Text('Outside'), + TapRegion( + onTapInside: (PointerEvent event) { + tappedInside.add('No Group'); + }, + child: const Text('No Group'), + ), + TapRegion( + groupId: 1, + onTapInside: (PointerEvent event) { + tappedInside.add('Group 1 A'); + }, + child: const Text('Group 1 A'), + ), + TapRegion( + groupId: 1, + onTapInside: (PointerEvent event) { + tappedInside.add('Group 1 B'); + }, + child: const Text('Group 1 B'), + ), + ], + ), + ), + ], + ), + ), + ); + + await tester.pump(); + + Future click(Finder finder) async { + final TestGesture gesture = await tester.startGesture( + tester.getCenter(finder), + kind: PointerDeviceKind.mouse, + buttons: kSecondaryButton, + ); + await gesture.up(); + await gesture.removePointer(); + } + + expect(tappedInside, isEmpty); + + await click(find.text('No Group')); + expect( + tappedInside, + unorderedEquals({ + 'No Group', + })); + tappedInside.clear(); + + await click(find.text('Group 1 A')); + expect( + tappedInside, + equals({ + 'Group 1 A', + 'Group 1 B', + })); + tappedInside.clear(); + + await click(find.text('Group 1 B')); + expect( + tappedInside, + equals({ + 'Group 1 A', + 'Group 1 B', + })); + tappedInside.clear(); + + await click(find.text('Outside')); + expect(tappedInside, isEmpty); + tappedInside.clear(); + + await click(find.text('Outside Surface')); + expect(tappedInside, isEmpty); + }); + + testWidgetsWithLeakTracking('TapRegionSurface detects inside middle click', (WidgetTester tester) async { + final Set tappedInside = {}; + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Column( + children: [ + const Text('Outside Surface'), + TapRegionSurface( + child: Row( + children: [ + const Text('Outside'), + TapRegion( + onTapInside: (PointerEvent event) { + tappedInside.add('No Group'); + }, + child: const Text('No Group'), + ), + TapRegion( + groupId: 1, + onTapInside: (PointerEvent event) { + tappedInside.add('Group 1 A'); + }, + child: const Text('Group 1 A'), + ), + TapRegion( + groupId: 1, + onTapInside: (PointerEvent event) { + tappedInside.add('Group 1 B'); + }, + child: const Text('Group 1 B'), + ), + ], + ), + ), + ], + ), + ), + ); + + await tester.pump(); + + Future click(Finder finder) async { + final TestGesture gesture = await tester.startGesture( + tester.getCenter(finder), + kind: PointerDeviceKind.mouse, + buttons: kTertiaryButton, + ); + await gesture.up(); + await gesture.removePointer(); + } + + expect(tappedInside, isEmpty); + + await click(find.text('No Group')); + expect( + tappedInside, + unorderedEquals({ + 'No Group', + })); + tappedInside.clear(); + + await click(find.text('Group 1 A')); + expect( + tappedInside, + equals({ + 'Group 1 A', + 'Group 1 B', + })); + tappedInside.clear(); + + await click(find.text('Group 1 B')); + expect( + tappedInside, + equals({ + 'Group 1 A', + 'Group 1 B', + })); + tappedInside.clear(); + + await click(find.text('Outside')); + expect(tappedInside, isEmpty); + tappedInside.clear(); + + await click(find.text('Outside Surface')); + expect(tappedInside, isEmpty); + }); }