Add callbacks for detecting tap up events to TapRegion (#156110)

Adds `onTapUpOutside` and `onTapUpInside` callbacks to `TapRegion`, and `onTapUpOutside` to `EditableText` text fields. This allows users to detect `PointerUpEvent`s, which in turn can be used to differentiate between a tap and pan / scroll gesture (for example by comparing the distance between the down and up events).

Fixes #140271 and potentially  #138190
This commit is contained in:
Hannes Hultergård 2024-10-21 18:59:16 +02:00 committed by GitHub
parent 4ff8c9507b
commit 8621694fdf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 340 additions and 52 deletions

View File

@ -273,6 +273,7 @@ class CupertinoTextField extends StatefulWidget {
this.onEditingComplete,
this.onSubmitted,
this.onTapOutside,
this.onTapUpOutside,
this.inputFormatters,
this.enabled = true,
this.cursorWidth = 2.0,
@ -402,6 +403,7 @@ class CupertinoTextField extends StatefulWidget {
this.onEditingComplete,
this.onSubmitted,
this.onTapOutside,
this.onTapUpOutside,
this.inputFormatters,
this.enabled = true,
this.cursorWidth = 2.0,
@ -671,6 +673,9 @@ class CupertinoTextField extends StatefulWidget {
/// {@macro flutter.widgets.editableText.onTapOutside}
final TapRegionCallback? onTapOutside;
/// {@macro flutter.widgets.editableText.onTapUpOutside}
final TapRegionCallback? onTapUpOutside;
/// {@macro flutter.widgets.editableText.inputFormatters}
final List<TextInputFormatter>? inputFormatters;

View File

@ -292,6 +292,7 @@ class TextField extends StatefulWidget {
this.onTap,
this.onTapAlwaysCalled = false,
this.onTapOutside,
this.onTapUpOutside,
this.mouseCursor,
this.buildCounter,
this.scrollController,
@ -707,6 +708,9 @@ class TextField extends StatefulWidget {
/// * [TapRegion] for how the region group is determined.
final TapRegionCallback? onTapOutside;
/// {@macro flutter.widgets.editableText.onTapUpOutside}
final TapRegionUpCallback? onTapUpOutside;
/// The cursor for a mouse pointer when it enters or is hovering over the
/// widget.
///
@ -1485,6 +1489,7 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements
groupId: widget.groupId,
onSelectionHandleTapped: _handleSelectionHandleTapped,
onTapOutside: widget.onTapOutside,
onTapUpOutside: widget.onTapUpOutside,
inputFormatters: formatters,
rendererIgnoresPointer: true,
mouseCursor: MouseCursor.defer, // TextField will handle the cursor

View File

@ -145,6 +145,7 @@ class TextFormField extends FormField<String> {
GestureTapCallback? onTap,
bool onTapAlwaysCalled = false,
TapRegionCallback? onTapOutside,
TapRegionUpCallback? onTapUpOutside,
VoidCallback? onEditingComplete,
ValueChanged<String>? onFieldSubmitted,
super.onSaved,
@ -245,6 +246,7 @@ class TextFormField extends FormField<String> {
onTap: onTap,
onTapAlwaysCalled: onTapAlwaysCalled,
onTapOutside: onTapOutside,
onTapUpOutside: onTapUpOutside,
onEditingComplete: onEditingComplete,
onSubmitted: onFieldSubmitted,
inputFormatters: inputFormatters,

View File

@ -850,6 +850,7 @@ class EditableText extends StatefulWidget {
this.onSelectionHandleTapped,
this.groupId = EditableText,
this.onTapOutside,
this.onTapUpOutside,
List<TextInputFormatter>? inputFormatters,
this.mouseCursor,
this.rendererIgnoresPointer = false,
@ -1492,8 +1493,8 @@ class EditableText extends StatefulWidget {
final Object groupId;
/// {@template flutter.widgets.editableText.onTapOutside}
/// Called for each tap that occurs outside of the[TextFieldTapRegion] group
/// when the text field is focused.
/// Called for each tap down that occurs outside of the [TextFieldTapRegion]
/// group when the text field is focused.
///
/// If this is null, [EditableTextTapOutsideIntent] will be invoked. In the
/// default implementation, [FocusNode.unfocus] will be called on the
@ -1530,10 +1531,27 @@ class EditableText extends StatefulWidget {
/// See also:
///
/// * [TapRegion] for how the region group is determined.
/// * [onTapUpOutside] which is called for each tap up.
/// * [EditableTextTapOutsideIntent] for the intent that is invoked if
/// this is null.
final TapRegionCallback? onTapOutside;
/// {@template flutter.widgets.editableText.onTapUpOutside}
/// Called for each tap up that occurs outside of the [TextFieldTapRegion]
/// group when the text field is focused.
///
/// The [PointerUpEvent] passed to the function is the event that caused the
/// notification. It is possible that the event may occur outside of the
/// immediate bounding box defined by the text field, although it will be
/// within the bounding box of a [TextFieldTapRegion] member.
/// {@endtemplate}
///
/// See also:
///
/// * [TapRegion] for how the region group is determined.
/// * [onTapOutside], which is called for each tap down.
final TapRegionUpCallback? onTapUpOutside;
/// {@template flutter.widgets.editableText.inputFormatters}
/// Optional input validation and formatting overrides.
///
@ -5172,6 +5190,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
return TextFieldTapRegion(
groupId: widget.groupId,
onTapOutside: _hasFocus ? widget.onTapOutside ?? (PointerDownEvent event) => _defaultOnTapOutside(context, event) : null,
onTapUpOutside: widget.onTapUpOutside,
debugLabel: kReleaseMode ? null : 'EditableText',
child: MouseRegion(
cursor: widget.mouseCursor ?? SystemMouseCursors.text,

View File

@ -33,12 +33,24 @@ bool _tapRegionDebug(String message, [Iterable<String>? details]) {
return true;
}
/// The type of callback that [TapRegion.onTapOutside] and
/// [TapRegion.onTapInside] take.
/// Signature for a callback called for a [PointerDownEvent] relative to a [TapRegion].
///
/// The event is the pointer event that caused the callback to be called.
/// See also:
///
/// * [TapRegion.onTapOutside], which is of this type.
/// * [TapRegion.onTapInside], which is of this type.
/// * [TapRegionUpCallback], which is similar but for [PointerUpEvent]s.
typedef TapRegionCallback = void Function(PointerDownEvent event);
/// Signature for a callback called for a [PointerUpEvent] relative to a [TapRegion].
///
/// See also:
///
/// * [TapRegion.onTapUpOutside], which is of this type.
/// * [TapRegion.onTapUpInside], which is of this type.
/// * [TapRegionCallback], which is similar but for [PointerDownEvent]s.
typedef TapRegionUpCallback = void Function(PointerUpEvent event);
/// An interface for registering and unregistering a [RenderTapRegion]
/// (typically created with a [TapRegion] widget) with a
/// [RenderTapRegionSurface] (typically created with a [TapRegionSurface]
@ -89,21 +101,23 @@ abstract class TapRegionRegistry {
/// by assigning a [TapRegion.groupId], where all the regions with the same
/// groupId act as if they were all one region.
///
/// When a tap outside of a registered region or region group is detected, its
/// [TapRegion.onTapOutside] callback is called. If the tap is outside one
/// member of a group, but inside another, no notification is made.
/// When a tap down or tap up outside of a registered region or region group is
/// detected, its [TapRegion.onTapOutside] or [TapRegion.onTapUpOutside]
/// callback is called, respectively. If the tap is outside one member of a
/// group, but inside another, no notification is made.
///
/// When a tap inside of a registered region or region group is detected, its
/// [TapRegion.onTapInside] callback is called. If the tap is inside one member
/// of a group, all members are notified.
/// When a tap down or tap up inside of a registered region or region group is
/// detected, its [TapRegion.onTapInside] or [TapRegion.onTapUpInside]
/// callback is called, respectively. If the tap is inside one member of a
/// group, all members are notified.
///
/// The [TapRegionSurface] should be defined at the highest level needed to
/// encompass the entire area where taps should be monitored. This is typically
/// around the entire app. If the entire app isn't covered, then taps outside of
/// the [TapRegionSurface] will be ignored and no [TapRegion.onTapOutside] calls
/// will be made for those events. The [WidgetsApp], [MaterialApp] and
/// [CupertinoApp] automatically include a [TapRegionSurface] around their
/// entire app.
/// the [TapRegionSurface] will be ignored and no [TapRegion.onTapOutside] or
/// [TapRegion.onTapUpOutside] calls will be made for those events. The
/// [WidgetsApp], [MaterialApp] and [CupertinoApp] automatically include a
/// [TapRegionSurface] around their entire app.
///
/// [TapRegionSurface] does not participate in the [gesture
/// disambiguation](https://flutter.dev/to/gesture-disambiguation)
@ -152,21 +166,24 @@ class TapRegionSurface extends SingleChildRenderObjectWidget {
/// group by assigning a [RenderTapRegion.groupId], where all the regions with
/// the same groupId act as if they were all one region.
///
/// When a tap outside of a registered region or region group is detected, its
/// [TapRegion.onTapOutside] callback is called. If the tap is outside one
/// member of a group, but inside another, no notification is made.
/// When a tap down or tap up outside of a registered region or region group is
/// detected, its [TapRegion.onTapOutside] or [TapRegion.onTapUpOutside]
/// callback is called, respectively. If the tap is outside one member of a
/// group, but inside another, no notification is made.
///
/// When a tap inside of a registered region or region group is detected, its
/// [TapRegion.onTapInside] callback is called. If the tap is inside one member
/// of a group, all members are notified.
/// When a tap down or tap up inside of a registered region or region group is
/// detected, its [TapRegion.onTapInside] or [TapRegion.onTapUpInside]
/// callback is called, respectively. If the tap is inside one member of a
/// group, all members are notified.
///
/// The [RenderTapRegionSurface] should be defined at the highest level needed
/// to encompass the entire area where taps should be monitored. This is
/// typically around the entire app. If the entire app isn't covered, then taps
/// outside of the [RenderTapRegionSurface] will be ignored and no
/// [RenderTapRegion.onTapOutside] calls will be made for those events. The
/// [WidgetsApp], [MaterialApp] and [CupertinoApp] automatically include a
/// [RenderTapRegionSurface] around the entire app.
/// [RenderTapRegion.onTapOutside] or [RenderTapRegion.onTapUpOutside] calls
/// will be made for those events. The [WidgetsApp], [MaterialApp] and
/// [CupertinoApp] automatically include a [RenderTapRegionSurface] around the
/// entire app.
///
/// [RenderTapRegionSurface] does not participate in the [gesture
/// disambiguation](https://flutter.dev/to/gesture-disambiguation)
@ -242,7 +259,7 @@ class RenderTapRegionSurface extends RenderProxyBoxWithHitTestBehavior implement
return true;
}(), 'A RenderTapRegion was registered when it was disabled.');
if (event is! PointerDownEvent) {
if (event is! PointerDownEvent && event is! PointerUpEvent) {
return;
}
@ -258,8 +275,8 @@ class RenderTapRegionSurface extends RenderProxyBoxWithHitTestBehavior implement
return;
}
// A child was hit, so we need to call onTapOutside for those regions or
// groups of regions that were not hit.
// A child was hit, so we need to call onTapOutside / onTapUpOutside for
// those regions or groups of regions that were not hit.
final Set<RenderTapRegion> hitRegions =
_getRegionsHit(_registeredRegions, result.path).cast<RenderTapRegion>().toSet();
assert(_tapRegionDebug('Tap event hit ${hitRegions.length} descendants.'));
@ -275,22 +292,33 @@ class RenderTapRegionSurface extends RenderProxyBoxWithHitTestBehavior implement
bool consumeOutsideTaps = false;
for (final RenderTapRegion region in outsideRegions) {
assert(_tapRegionDebug('Calling onTapOutside for $region'));
if (event is PointerDownEvent) {
assert(_tapRegionDebug('Calling onTapOutside for $region'));
region.onTapOutside?.call(event);
} else if (event is PointerUpEvent) {
assert(_tapRegionDebug('Calling onTapUpOutside for $region'));
region.onTapUpOutside?.call(event);
}
if (region.consumeOutsideTaps) {
assert(_tapRegionDebug('Stopping tap propagation for $region (and all of ${region.groupId})'));
consumeOutsideTaps = true;
}
region.onTapOutside?.call(event);
}
for (final RenderTapRegion region in insideRegions) {
assert(_tapRegionDebug('Calling onTapInside for $region'));
region.onTapInside?.call(event);
if (event is PointerDownEvent) {
assert(_tapRegionDebug('Calling onTapInside for $region'));
region.onTapInside?.call(event);
} else if (event is PointerUpEvent) {
assert(_tapRegionDebug('Calling onTapUpInside for $region'));
region.onTapUpInside?.call(event);
}
}
// If any of the "outside" regions have consumeOutsideTaps set, then stop
// the propagation of the event through the gesture recognizer by adding it
// to the recognizer and immediately resolving it.
if (consumeOutsideTaps) {
if (consumeOutsideTaps && event is PointerDownEvent) {
GestureBinding.instance.gestureArena.add(event.pointer, _DummyTapRecognizer()).resolve(GestureDisposition.accepted);
}
}
@ -332,7 +360,7 @@ class _DummyTapRecognizer extends GestureArenaMember {
/// If there is no [TapRegionSurface] ancestor, [TapRegion] will do nothing.
///
/// [TapRegion] is aware of the [Route]s in the [Navigator], so that [onTapOutside]
/// isn't called after the user navigates to a different page.
/// or [onTapUpOutside] isn't called after the user navigates to a different page.
class TapRegion extends SingleChildRenderObjectWidget {
/// Creates a const [TapRegion].
///
@ -344,6 +372,8 @@ class TapRegion extends SingleChildRenderObjectWidget {
this.behavior = HitTestBehavior.deferToChild,
this.onTapOutside,
this.onTapInside,
this.onTapUpOutside,
this.onTapUpInside,
this.groupId,
this.consumeOutsideTaps = false,
String? debugLabel,
@ -360,37 +390,71 @@ class TapRegion extends SingleChildRenderObjectWidget {
/// See [HitTestBehavior] for the allowed values and their meanings.
final HitTestBehavior behavior;
/// A callback to be invoked when a tap is detected outside of this
/// A callback to be invoked when a tap down is detected outside of this
/// [TapRegion] and any other region with the same [groupId], if any.
///
/// The [PointerDownEvent] passed to the function is the event that caused the
/// notification. If this region is part of a group (i.e. [groupId] is set),
/// then it's possible that the event may be outside of this immediate region,
/// although it will be within the region of one of the group members.
///
/// See also:
/// * [onTapUpOutside], which is called when a tap up is detected outside
/// of this region.
final TapRegionCallback? onTapOutside;
/// A callback to be invoked when a tap is detected inside of this
/// A callback to be invoked when a tap down is detected inside of this
/// [TapRegion], or any other tap region with the same [groupId], if any.
///
/// The [PointerDownEvent] passed to the function is the event that caused the
/// notification. If this region is part of a group (i.e. [groupId] is set),
/// then it's possible that the event may be outside of this immediate region,
/// although it will be within the region of one of the group members.
///
/// See also:
/// * [onTapUpInside], which is called when a tap up is detected inside
/// of this region.
final TapRegionCallback? onTapInside;
/// A callback to be invoked when a tap up is detected outside of this
/// [TapRegion] and any other region with the same [groupId], if any.
///
/// The [PointerUpEvent] passed to the function is the event that caused the
/// notification. If this region is part of a group (i.e. [groupId] is set),
/// then it's possible that the event may be outside of this immediate region,
/// although it will be within the region of one of the group members.
///
/// See also:
/// * [onTapOutside], which is called when a tap down is detected outside
/// of this region.
final TapRegionUpCallback? onTapUpOutside;
/// A callback to be invoked when a tap up is detected inside of this
/// [TapRegion], or any other tap region with the same [groupId], if any.
///
/// The [PointerUpEvent] passed to the function is the event that caused the
/// notification. If this region is part of a group (i.e. [groupId] is set),
/// then it's possible that the event may be outside of this immediate region,
/// although it will be within the region of one of the group members.
///
/// See also:
/// * [onTapInside], which is called when a tap down is detected inside
/// of this region.
final TapRegionUpCallback? onTapUpInside;
/// An optional group ID that groups [TapRegion]s together so that they
/// operate as one region. If any member of a group is hit by a particular
/// tap, then the [onTapOutside] will not be called for any members of the
/// group. If any member of the group is hit, then all members will have their
/// [onTapInside] called.
/// tap, then the [onTapOutside] / [onTapUpOutside] will not be called for
/// any members of the group. If any member of the group is hit, then all
/// members will have their [onTapInside] / [onTapUpInside] called.
///
/// If the group id is null, then only this region is hit tested.
final Object? groupId;
/// If true, then the group that this region belongs to will stop the
/// propagation of the tap down event in the gesture arena.
/// propagation of all events in the gesture arena.
///
/// This is useful if you want to block the tap down from being given to a
/// This is useful if you want to block events from being given to a
/// [GestureDetector] when [onTapOutside] is called.
///
/// If other [TapRegion]s with the same [groupId] have [consumeOutsideTaps]
@ -416,6 +480,8 @@ class TapRegion extends SingleChildRenderObjectWidget {
behavior: behavior,
onTapOutside: isCurrent ? onTapOutside : null,
onTapInside: onTapInside,
onTapUpOutside: isCurrent ? onTapUpOutside : null,
onTapUpInside: onTapUpInside,
groupId: groupId,
debugLabel: debugLabel,
);
@ -431,7 +497,9 @@ class TapRegion extends SingleChildRenderObjectWidget {
..behavior = behavior
..groupId = groupId
..onTapOutside = isCurrent ? onTapOutside : null
..onTapInside = onTapInside;
..onTapInside = onTapInside
..onTapUpOutside = isCurrent ? onTapUpOutside : null
..onTapUpInside = onTapUpInside;
if (!kReleaseMode) {
renderObject.debugLabel = debugLabel;
}
@ -480,6 +548,8 @@ class RenderTapRegion extends RenderProxyBoxWithHitTestBehavior {
bool consumeOutsideTaps = false,
this.onTapOutside,
this.onTapInside,
this.onTapUpOutside,
this.onTapUpInside,
super.behavior = HitTestBehavior.deferToChild,
Object? groupId,
String? debugLabel,
@ -491,7 +561,7 @@ class RenderTapRegion extends RenderProxyBoxWithHitTestBehavior {
bool _isRegistered = false;
/// A callback to be invoked when a tap is detected outside of this
/// A callback to be invoked when a tap down is detected outside of this
/// [RenderTapRegion] and any other region with the same [groupId], if any.
///
/// The [PointerDownEvent] passed to the function is the event that caused the
@ -500,7 +570,7 @@ class RenderTapRegion extends RenderProxyBoxWithHitTestBehavior {
/// although it will be within the region of one of the group members.
TapRegionCallback? onTapOutside;
/// A callback to be invoked when a tap is detected inside of this
/// A callback to be invoked when a tap down is detected inside of this
/// [RenderTapRegion], or any other tap region with the same [groupId], if any.
///
/// The [PointerDownEvent] passed to the function is the event that caused the
@ -509,6 +579,24 @@ class RenderTapRegion extends RenderProxyBoxWithHitTestBehavior {
/// although it will be within the region of one of the group members.
TapRegionCallback? onTapInside;
/// A callback to be invoked when a tap up is detected outside of this
/// [RenderTapRegion] and any other region with the same [groupId], if any.
///
/// The [PointerUpEvent] passed to the function is the event that caused the
/// notification. If this region is part of a group (i.e. [groupId] is set),
/// then it's possible that the event may be outside of this immediate region,
/// although it will be within the region of one of the group members.
TapRegionUpCallback? onTapUpOutside;
/// A callback to be invoked when a tap up is detected inside of this
/// [RenderTapRegion], or any other tap region with the same [groupId], if any.
///
/// The [PointerUpEvent] passed to the function is the event that caused the
/// notification. If this region is part of a group (i.e. [groupId] is set),
/// then it's possible that the event may be outside of this immediate region,
/// although it will be within the region of one of the group members.
TapRegionUpCallback? onTapUpInside;
/// A label used in debug builds. Will be null in release builds.
String? debugLabel;
@ -522,8 +610,8 @@ class RenderTapRegion extends RenderProxyBoxWithHitTestBehavior {
}
}
/// Whether or not the tap down even that triggers a call to [onTapOutside]
/// will continue on to participate in the gesture arena.
/// Whether or not the tap event that triggers a call to [onTapOutside]
/// or [onTapUpOutside] will continue on to participate in the gesture arena.
///
/// If any [RenderTapRegion] in the same group has [consumeOutsideTaps] set to
/// true, then the tap down event will be consumed before other gesture
@ -539,9 +627,9 @@ class RenderTapRegion extends RenderProxyBoxWithHitTestBehavior {
/// An optional group ID that groups [RenderTapRegion]s together so that they
/// operate as one region. If any member of a group is hit by a particular
/// tap, then the [onTapOutside] will not be called for any members of the
/// group. If any member of the group is hit, then all members will have their
/// [onTapInside] called.
/// tap, then the [onTapOutside] / [onTapUpOutside] will not be called for
/// any members of the group. If any member of the group is hit, then all
/// members will have their [onTapInside] / [onTapUpInside] called.
///
/// If the group id is null, then only this region is hit tested.
Object? get groupId => _groupId;
@ -647,6 +735,8 @@ class TextFieldTapRegion extends TapRegion {
super.enabled,
super.onTapOutside,
super.onTapInside,
super.onTapUpOutside,
super.onTapUpInside,
super.consumeOutsideTaps,
super.debugLabel,
super.groupId = EditableText,

View File

@ -9,7 +9,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('TapRegionSurface detects outside taps', (WidgetTester tester) async {
testWidgets('TapRegionSurface detects outside tap down events', (WidgetTester tester) async {
final Set<String> tappedOutside = <String>{};
await tester.pumpWidget(
Directionality(
@ -56,7 +56,8 @@ void main() {
tester.getCenter(finder),
kind: PointerDeviceKind.mouse,
);
await gesture.up();
// We intentionally don't call up() here because we're testing the down event.
await gesture.cancel();
await gesture.removePointer();
}
@ -101,6 +102,91 @@ void main() {
expect(tappedOutside, isEmpty);
});
testWidgets('TapRegionSurface detects outside tap up events', (WidgetTester tester) async {
final Set<String> tappedOutside = <String>{};
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: TapRegionSurface(
child: Row(
children: <Widget>[
const Text('Outside'),
TapRegion(
onTapUpOutside: (PointerEvent event) {
tappedOutside.add('No Group');
},
child: const Text('No Group'),
),
TapRegion(
groupId: 1,
onTapUpOutside: (PointerEvent event) {
tappedOutside.add('Group 1 A');
},
child: const Text('Group 1 A'),
),
TapRegion(
groupId: 1,
onTapUpOutside: (PointerEvent event) {
tappedOutside.add('Group 1 B');
},
child: const Text('Group 1 B'),
),
],
),
),
),
);
await tester.pump();
Future<void> click(Finder finder) async {
final TestGesture gesture = await tester.startGesture(
tester.getCenter(finder),
kind: PointerDeviceKind.mouse,
);
expect(tappedOutside, isEmpty); // No callbacks should been called before up event.
await gesture.up();
await gesture.removePointer();
}
expect(tappedOutside, isEmpty);
await click(find.text('No Group'));
expect(
tappedOutside,
unorderedEquals(<String>{
'Group 1 A',
'Group 1 B',
}));
tappedOutside.clear();
await click(find.text('Group 1 A'));
expect(
tappedOutside,
equals(<String>{
'No Group',
}));
tappedOutside.clear();
await click(find.text('Group 1 B'));
expect(
tappedOutside,
equals(<String>{
'No Group',
}));
tappedOutside.clear();
await click(find.text('Outside'));
expect(
tappedOutside,
unorderedEquals(<String>{
'No Group',
'Group 1 A',
'Group 1 B',
}));
tappedOutside.clear();
});
testWidgets('TapRegionSurface consumes outside taps when asked', (WidgetTester tester) async {
final Set<String> tappedOutside = <String>{};
int propagatedTaps = 0;
@ -206,7 +292,7 @@ void main() {
expect(tappedOutside, isEmpty);
});
testWidgets('TapRegionSurface detects inside taps', (WidgetTester tester) async {
testWidgets('TapRegionSurface detects inside tap down events', (WidgetTester tester) async {
final Set<String> tappedInside = <String>{};
await tester.pumpWidget(
Directionality(
@ -253,7 +339,8 @@ void main() {
tester.getCenter(finder),
kind: PointerDeviceKind.mouse,
);
await gesture.up();
// We intentionally don't call up() here because we're testing the down event.
await gesture.cancel();
await gesture.removePointer();
}
@ -293,6 +380,86 @@ void main() {
expect(tappedInside, isEmpty);
});
testWidgets('TapRegionSurface detects inside tap up events', (WidgetTester tester) async {
final Set<String> tappedInside = <String>{};
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: TapRegionSurface(
child: Row(
children: <Widget>[
const Text('Outside'),
TapRegion(
onTapUpInside: (PointerEvent event) {
tappedInside.add('No Group');
},
child: const Text('No Group'),
),
TapRegion(
groupId: 1,
onTapUpInside: (PointerEvent event) {
tappedInside.add('Group 1 A');
},
child: const Text('Group 1 A'),
),
TapRegion(
groupId: 1,
onTapUpInside: (PointerEvent event) {
tappedInside.add('Group 1 B');
},
child: const Text('Group 1 B'),
),
],
),
),
),
);
await tester.pump();
Future<void> click(Finder finder) async {
final TestGesture gesture = await tester.startGesture(
tester.getCenter(finder),
kind: PointerDeviceKind.mouse,
);
expect(tappedInside, isEmpty); // No callbacks should been called before up event.
await gesture.up();
await gesture.removePointer();
}
expect(tappedInside, isEmpty);
await click(find.text('No Group'));
expect(
tappedInside,
unorderedEquals(<String>{
'No Group',
}));
tappedInside.clear();
await click(find.text('Group 1 A'));
expect(
tappedInside,
equals(<String>{
'Group 1 A',
'Group 1 B',
}));
tappedInside.clear();
await click(find.text('Group 1 B'));
expect(
tappedInside,
equals(<String>{
'Group 1 A',
'Group 1 B',
}));
tappedInside.clear();
await click(find.text('Outside'));
expect(tappedInside, isEmpty);
tappedInside.clear();
});
testWidgets('TapRegionSurface detects inside taps correctly with behavior', (WidgetTester tester) async {
final Set<String> tappedInside = <String>{};
const ValueKey<String> noGroupKey = ValueKey<String>('No Group');