Add supportedDevices parameter to GestureDetector (#107312)
This commit is contained in:
parent
22f51c34c6
commit
7b07b85d91
@ -288,6 +288,7 @@ class GestureDetector extends StatelessWidget {
|
|||||||
this.behavior,
|
this.behavior,
|
||||||
this.excludeFromSemantics = false,
|
this.excludeFromSemantics = false,
|
||||||
this.dragStartBehavior = DragStartBehavior.start,
|
this.dragStartBehavior = DragStartBehavior.start,
|
||||||
|
this.supportedDevices,
|
||||||
}) : assert(excludeFromSemantics != null),
|
}) : assert(excludeFromSemantics != null),
|
||||||
assert(dragStartBehavior != null),
|
assert(dragStartBehavior != null),
|
||||||
assert(() {
|
assert(() {
|
||||||
@ -1004,6 +1005,11 @@ class GestureDetector extends StatelessWidget {
|
|||||||
/// * [DragGestureRecognizer.dragStartBehavior], which gives an example for the different behaviors.
|
/// * [DragGestureRecognizer.dragStartBehavior], which gives an example for the different behaviors.
|
||||||
final DragStartBehavior dragStartBehavior;
|
final DragStartBehavior dragStartBehavior;
|
||||||
|
|
||||||
|
/// The kind of devices that are allowed to be recognized.
|
||||||
|
///
|
||||||
|
/// If set to null, events from all device types will be recognized. Defaults to null.
|
||||||
|
final Set<PointerDeviceKind>? supportedDevices;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final Map<Type, GestureRecognizerFactory> gestures = <Type, GestureRecognizerFactory>{};
|
final Map<Type, GestureRecognizerFactory> gestures = <Type, GestureRecognizerFactory>{};
|
||||||
@ -1022,7 +1028,7 @@ class GestureDetector extends StatelessWidget {
|
|||||||
onTertiaryTapCancel != null
|
onTertiaryTapCancel != null
|
||||||
) {
|
) {
|
||||||
gestures[TapGestureRecognizer] = GestureRecognizerFactoryWithHandlers<TapGestureRecognizer>(
|
gestures[TapGestureRecognizer] = GestureRecognizerFactoryWithHandlers<TapGestureRecognizer>(
|
||||||
() => TapGestureRecognizer(debugOwner: this),
|
() => TapGestureRecognizer(debugOwner: this, supportedDevices: supportedDevices),
|
||||||
(TapGestureRecognizer instance) {
|
(TapGestureRecognizer instance) {
|
||||||
instance
|
instance
|
||||||
..onTapDown = onTapDown
|
..onTapDown = onTapDown
|
||||||
@ -1043,7 +1049,7 @@ class GestureDetector extends StatelessWidget {
|
|||||||
|
|
||||||
if (onDoubleTap != null) {
|
if (onDoubleTap != null) {
|
||||||
gestures[DoubleTapGestureRecognizer] = GestureRecognizerFactoryWithHandlers<DoubleTapGestureRecognizer>(
|
gestures[DoubleTapGestureRecognizer] = GestureRecognizerFactoryWithHandlers<DoubleTapGestureRecognizer>(
|
||||||
() => DoubleTapGestureRecognizer(debugOwner: this),
|
() => DoubleTapGestureRecognizer(debugOwner: this, supportedDevices: supportedDevices),
|
||||||
(DoubleTapGestureRecognizer instance) {
|
(DoubleTapGestureRecognizer instance) {
|
||||||
instance
|
instance
|
||||||
..onDoubleTapDown = onDoubleTapDown
|
..onDoubleTapDown = onDoubleTapDown
|
||||||
@ -1076,7 +1082,7 @@ class GestureDetector extends StatelessWidget {
|
|||||||
onTertiaryLongPressUp != null ||
|
onTertiaryLongPressUp != null ||
|
||||||
onTertiaryLongPressEnd != null) {
|
onTertiaryLongPressEnd != null) {
|
||||||
gestures[LongPressGestureRecognizer] = GestureRecognizerFactoryWithHandlers<LongPressGestureRecognizer>(
|
gestures[LongPressGestureRecognizer] = GestureRecognizerFactoryWithHandlers<LongPressGestureRecognizer>(
|
||||||
() => LongPressGestureRecognizer(debugOwner: this),
|
() => LongPressGestureRecognizer(debugOwner: this, supportedDevices: supportedDevices),
|
||||||
(LongPressGestureRecognizer instance) {
|
(LongPressGestureRecognizer instance) {
|
||||||
instance
|
instance
|
||||||
..onLongPressDown = onLongPressDown
|
..onLongPressDown = onLongPressDown
|
||||||
@ -1111,7 +1117,7 @@ class GestureDetector extends StatelessWidget {
|
|||||||
onVerticalDragEnd != null ||
|
onVerticalDragEnd != null ||
|
||||||
onVerticalDragCancel != null) {
|
onVerticalDragCancel != null) {
|
||||||
gestures[VerticalDragGestureRecognizer] = GestureRecognizerFactoryWithHandlers<VerticalDragGestureRecognizer>(
|
gestures[VerticalDragGestureRecognizer] = GestureRecognizerFactoryWithHandlers<VerticalDragGestureRecognizer>(
|
||||||
() => VerticalDragGestureRecognizer(debugOwner: this),
|
() => VerticalDragGestureRecognizer(debugOwner: this, supportedDevices: supportedDevices),
|
||||||
(VerticalDragGestureRecognizer instance) {
|
(VerticalDragGestureRecognizer instance) {
|
||||||
instance
|
instance
|
||||||
..onDown = onVerticalDragDown
|
..onDown = onVerticalDragDown
|
||||||
@ -1131,7 +1137,7 @@ class GestureDetector extends StatelessWidget {
|
|||||||
onHorizontalDragEnd != null ||
|
onHorizontalDragEnd != null ||
|
||||||
onHorizontalDragCancel != null) {
|
onHorizontalDragCancel != null) {
|
||||||
gestures[HorizontalDragGestureRecognizer] = GestureRecognizerFactoryWithHandlers<HorizontalDragGestureRecognizer>(
|
gestures[HorizontalDragGestureRecognizer] = GestureRecognizerFactoryWithHandlers<HorizontalDragGestureRecognizer>(
|
||||||
() => HorizontalDragGestureRecognizer(debugOwner: this),
|
() => HorizontalDragGestureRecognizer(debugOwner: this, supportedDevices: supportedDevices),
|
||||||
(HorizontalDragGestureRecognizer instance) {
|
(HorizontalDragGestureRecognizer instance) {
|
||||||
instance
|
instance
|
||||||
..onDown = onHorizontalDragDown
|
..onDown = onHorizontalDragDown
|
||||||
@ -1151,7 +1157,7 @@ class GestureDetector extends StatelessWidget {
|
|||||||
onPanEnd != null ||
|
onPanEnd != null ||
|
||||||
onPanCancel != null) {
|
onPanCancel != null) {
|
||||||
gestures[PanGestureRecognizer] = GestureRecognizerFactoryWithHandlers<PanGestureRecognizer>(
|
gestures[PanGestureRecognizer] = GestureRecognizerFactoryWithHandlers<PanGestureRecognizer>(
|
||||||
() => PanGestureRecognizer(debugOwner: this),
|
() => PanGestureRecognizer(debugOwner: this, supportedDevices: supportedDevices),
|
||||||
(PanGestureRecognizer instance) {
|
(PanGestureRecognizer instance) {
|
||||||
instance
|
instance
|
||||||
..onDown = onPanDown
|
..onDown = onPanDown
|
||||||
@ -1167,7 +1173,7 @@ class GestureDetector extends StatelessWidget {
|
|||||||
|
|
||||||
if (onScaleStart != null || onScaleUpdate != null || onScaleEnd != null) {
|
if (onScaleStart != null || onScaleUpdate != null || onScaleEnd != null) {
|
||||||
gestures[ScaleGestureRecognizer] = GestureRecognizerFactoryWithHandlers<ScaleGestureRecognizer>(
|
gestures[ScaleGestureRecognizer] = GestureRecognizerFactoryWithHandlers<ScaleGestureRecognizer>(
|
||||||
() => ScaleGestureRecognizer(debugOwner: this),
|
() => ScaleGestureRecognizer(debugOwner: this, supportedDevices: supportedDevices),
|
||||||
(ScaleGestureRecognizer instance) {
|
(ScaleGestureRecognizer instance) {
|
||||||
instance
|
instance
|
||||||
..onStart = onScaleStart
|
..onStart = onScaleStart
|
||||||
@ -1184,7 +1190,7 @@ class GestureDetector extends StatelessWidget {
|
|||||||
onForcePressUpdate != null ||
|
onForcePressUpdate != null ||
|
||||||
onForcePressEnd != null) {
|
onForcePressEnd != null) {
|
||||||
gestures[ForcePressGestureRecognizer] = GestureRecognizerFactoryWithHandlers<ForcePressGestureRecognizer>(
|
gestures[ForcePressGestureRecognizer] = GestureRecognizerFactoryWithHandlers<ForcePressGestureRecognizer>(
|
||||||
() => ForcePressGestureRecognizer(debugOwner: this),
|
() => ForcePressGestureRecognizer(debugOwner: this, supportedDevices: supportedDevices),
|
||||||
(ForcePressGestureRecognizer instance) {
|
(ForcePressGestureRecognizer instance) {
|
||||||
instance
|
instance
|
||||||
..onStart = onForcePressStart
|
..onStart = onForcePressStart
|
||||||
|
@ -875,6 +875,53 @@ void main() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWidgets('supportedDevices is respected', (WidgetTester tester) async {
|
||||||
|
bool didStartPan = false;
|
||||||
|
Offset? panDelta;
|
||||||
|
bool didEndPan = false;
|
||||||
|
|
||||||
|
await tester.pumpWidget(
|
||||||
|
GestureDetector(
|
||||||
|
onPanStart: (DragStartDetails details) {
|
||||||
|
didStartPan = true;
|
||||||
|
},
|
||||||
|
onPanUpdate: (DragUpdateDetails details) {
|
||||||
|
panDelta = (panDelta ?? Offset.zero) + details.delta;
|
||||||
|
},
|
||||||
|
onPanEnd: (DragEndDetails details) {
|
||||||
|
didEndPan = true;
|
||||||
|
},
|
||||||
|
supportedDevices: const <PointerDeviceKind>{PointerDeviceKind.mouse},
|
||||||
|
child: Container(
|
||||||
|
color: const Color(0xFF00FF00),
|
||||||
|
)
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(didStartPan, isFalse);
|
||||||
|
expect(panDelta, isNull);
|
||||||
|
expect(didEndPan, isFalse);
|
||||||
|
|
||||||
|
await tester.dragFrom(const Offset(10.0, 10.0), const Offset(20.0, 30.0), kind: PointerDeviceKind.mouse);
|
||||||
|
|
||||||
|
// Matching device should allow gesture.
|
||||||
|
expect(didStartPan, isTrue);
|
||||||
|
expect(panDelta!.dx, 20.0);
|
||||||
|
expect(panDelta!.dy, 30.0);
|
||||||
|
expect(didEndPan, isTrue);
|
||||||
|
|
||||||
|
didStartPan = false;
|
||||||
|
panDelta = null;
|
||||||
|
didEndPan = false;
|
||||||
|
|
||||||
|
await tester.dragFrom(const Offset(10.0, 10.0), const Offset(20.0, 30.0), kind: PointerDeviceKind.stylus);
|
||||||
|
|
||||||
|
// Non-matching device should not lead to any callbacks.
|
||||||
|
expect(didStartPan, isFalse);
|
||||||
|
expect(panDelta, isNull);
|
||||||
|
expect(didEndPan, isFalse);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
class _EmptySemanticsGestureDelegate extends SemanticsGestureDelegate {
|
class _EmptySemanticsGestureDelegate extends SemanticsGestureDelegate {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user