Fix GestureDetector long press callback handling (#62897)
* Allow GestureDetector to use long press and secondary long press at the same time.
This commit is contained in:
parent
cd0beabcd4
commit
b631013a87
@ -702,15 +702,14 @@ class GestureDetector extends StatelessWidget {
|
|||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final Map<Type, GestureRecognizerFactory> gestures = <Type, GestureRecognizerFactory>{};
|
final Map<Type, GestureRecognizerFactory> gestures = <Type, GestureRecognizerFactory>{};
|
||||||
|
|
||||||
if (
|
if (onTapDown != null ||
|
||||||
onTapDown != null ||
|
onTapUp != null ||
|
||||||
onTapUp != null ||
|
onTap != null ||
|
||||||
onTap != null ||
|
onTapCancel != null ||
|
||||||
onTapCancel != null ||
|
onSecondaryTap != null ||
|
||||||
onSecondaryTap != null ||
|
onSecondaryTapDown != null ||
|
||||||
onSecondaryTapDown != null ||
|
onSecondaryTapUp != null ||
|
||||||
onSecondaryTapUp != null ||
|
onSecondaryTapCancel != null
|
||||||
onSecondaryTapCancel != null
|
|
||||||
) {
|
) {
|
||||||
gestures[TapGestureRecognizer] = GestureRecognizerFactoryWithHandlers<TapGestureRecognizer>(
|
gestures[TapGestureRecognizer] = GestureRecognizerFactoryWithHandlers<TapGestureRecognizer>(
|
||||||
() => TapGestureRecognizer(debugOwner: this),
|
() => TapGestureRecognizer(debugOwner: this),
|
||||||
@ -741,21 +740,8 @@ class GestureDetector extends StatelessWidget {
|
|||||||
onLongPressUp != null ||
|
onLongPressUp != null ||
|
||||||
onLongPressStart != null ||
|
onLongPressStart != null ||
|
||||||
onLongPressMoveUpdate != null ||
|
onLongPressMoveUpdate != null ||
|
||||||
onLongPressEnd != null) {
|
onLongPressEnd != null ||
|
||||||
gestures[LongPressGestureRecognizer] = GestureRecognizerFactoryWithHandlers<LongPressGestureRecognizer>(
|
onSecondaryLongPress != null ||
|
||||||
() => LongPressGestureRecognizer(debugOwner: this),
|
|
||||||
(LongPressGestureRecognizer instance) {
|
|
||||||
instance
|
|
||||||
..onLongPress = onLongPress
|
|
||||||
..onLongPressStart = onLongPressStart
|
|
||||||
..onLongPressMoveUpdate = onLongPressMoveUpdate
|
|
||||||
..onLongPressEnd =onLongPressEnd
|
|
||||||
..onLongPressUp = onLongPressUp;
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (onSecondaryLongPress != null ||
|
|
||||||
onSecondaryLongPressUp != null ||
|
onSecondaryLongPressUp != null ||
|
||||||
onSecondaryLongPressStart != null ||
|
onSecondaryLongPressStart != null ||
|
||||||
onSecondaryLongPressMoveUpdate != null ||
|
onSecondaryLongPressMoveUpdate != null ||
|
||||||
@ -764,10 +750,15 @@ class GestureDetector extends StatelessWidget {
|
|||||||
() => LongPressGestureRecognizer(debugOwner: this),
|
() => LongPressGestureRecognizer(debugOwner: this),
|
||||||
(LongPressGestureRecognizer instance) {
|
(LongPressGestureRecognizer instance) {
|
||||||
instance
|
instance
|
||||||
|
..onLongPress = onLongPress
|
||||||
|
..onLongPressStart = onLongPressStart
|
||||||
|
..onLongPressMoveUpdate = onLongPressMoveUpdate
|
||||||
|
..onLongPressEnd = onLongPressEnd
|
||||||
|
..onLongPressUp = onLongPressUp
|
||||||
..onSecondaryLongPress = onSecondaryLongPress
|
..onSecondaryLongPress = onSecondaryLongPress
|
||||||
..onSecondaryLongPressStart = onSecondaryLongPressStart
|
..onSecondaryLongPressStart = onSecondaryLongPressStart
|
||||||
..onSecondaryLongPressMoveUpdate = onSecondaryLongPressMoveUpdate
|
..onSecondaryLongPressMoveUpdate = onSecondaryLongPressMoveUpdate
|
||||||
..onSecondaryLongPressEnd =onSecondaryLongPressEnd
|
..onSecondaryLongPressEnd = onSecondaryLongPressEnd
|
||||||
..onSecondaryLongPressUp = onSecondaryLongPressUp;
|
..onSecondaryLongPressUp = onSecondaryLongPressUp;
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@ -865,6 +856,7 @@ class GestureDetector extends StatelessWidget {
|
|||||||
child: child,
|
child: child,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
|
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
|
||||||
super.debugFillProperties(properties);
|
super.debugFillProperties(properties);
|
||||||
|
@ -381,6 +381,42 @@ void main() {
|
|||||||
}, variant: buttonVariant);
|
}, variant: buttonVariant);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWidgets('Primary and secondary long press callbacks should work together in GestureDetector', (WidgetTester tester) async {
|
||||||
|
bool primaryLongPress = false, secondaryLongPress = false;
|
||||||
|
|
||||||
|
await tester.pumpWidget(
|
||||||
|
Container(
|
||||||
|
alignment: Alignment.topLeft,
|
||||||
|
child: Container(
|
||||||
|
alignment: Alignment.center,
|
||||||
|
height: 100.0,
|
||||||
|
color: const Color(0xFF00FF00),
|
||||||
|
child: GestureDetector(
|
||||||
|
onLongPress: () {
|
||||||
|
primaryLongPress = true;
|
||||||
|
},
|
||||||
|
onSecondaryLongPress: () {
|
||||||
|
secondaryLongPress = true;
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
Future<void> longPress(Duration timeout, int buttons) async {
|
||||||
|
final TestGesture gesture = await tester.startGesture(const Offset(400.0, 50.0), buttons: buttons);
|
||||||
|
await tester.pump(timeout);
|
||||||
|
await gesture.up();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adding a second to make sure the time for long press has occurred.
|
||||||
|
await longPress(kLongPressTimeout + const Duration(seconds: 1), kPrimaryButton);
|
||||||
|
expect(primaryLongPress, isTrue);
|
||||||
|
|
||||||
|
await longPress(kLongPressTimeout + const Duration(seconds: 1), kSecondaryButton);
|
||||||
|
expect(secondaryLongPress, isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
testWidgets('Force Press Callback called after force press', (WidgetTester tester) async {
|
testWidgets('Force Press Callback called after force press', (WidgetTester tester) async {
|
||||||
int forcePressStart = 0;
|
int forcePressStart = 0;
|
||||||
int forcePressPeaked = 0;
|
int forcePressPeaked = 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user