Unverified Commit b631013a authored by creativecreatorormaybenot's avatar creativecreatorormaybenot Committed by GitHub

Fix GestureDetector long press callback handling (#62897)

* Allow GestureDetector to use long press and secondary long press at the same time.
parent cd0beabc
...@@ -702,8 +702,7 @@ class GestureDetector extends StatelessWidget { ...@@ -702,8 +702,7 @@ 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 ||
...@@ -741,21 +740,8 @@ class GestureDetector extends StatelessWidget { ...@@ -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 { ...@@ -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 { ...@@ -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() { ...@@ -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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment