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:
creativecreatorormaybenot 2020-08-05 22:22:41 +00:00 committed by GitHub
parent cd0beabcd4
commit b631013a87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 25 deletions

View file

@ -702,15 +702,14 @@ class GestureDetector extends StatelessWidget {
Widget build(BuildContext context) {
final Map<Type, GestureRecognizerFactory> gestures = <Type, GestureRecognizerFactory>{};
if (
onTapDown != null ||
onTapUp != null ||
onTap != null ||
onTapCancel != null ||
onSecondaryTap != null ||
onSecondaryTapDown != null ||
onSecondaryTapUp != null ||
onSecondaryTapCancel != null
if (onTapDown != null ||
onTapUp != null ||
onTap != null ||
onTapCancel != null ||
onSecondaryTap != null ||
onSecondaryTapDown != null ||
onSecondaryTapUp != null ||
onSecondaryTapCancel != null
) {
gestures[TapGestureRecognizer] = GestureRecognizerFactoryWithHandlers<TapGestureRecognizer>(
() => TapGestureRecognizer(debugOwner: this),
@ -741,21 +740,8 @@ class GestureDetector extends StatelessWidget {
onLongPressUp != null ||
onLongPressStart != null ||
onLongPressMoveUpdate != null ||
onLongPressEnd != null) {
gestures[LongPressGestureRecognizer] = GestureRecognizerFactoryWithHandlers<LongPressGestureRecognizer>(
() => LongPressGestureRecognizer(debugOwner: this),
(LongPressGestureRecognizer instance) {
instance
..onLongPress = onLongPress
..onLongPressStart = onLongPressStart
..onLongPressMoveUpdate = onLongPressMoveUpdate
..onLongPressEnd =onLongPressEnd
..onLongPressUp = onLongPressUp;
},
);
}
if (onSecondaryLongPress != null ||
onLongPressEnd != null ||
onSecondaryLongPress != null ||
onSecondaryLongPressUp != null ||
onSecondaryLongPressStart != null ||
onSecondaryLongPressMoveUpdate != null ||
@ -764,10 +750,15 @@ class GestureDetector extends StatelessWidget {
() => LongPressGestureRecognizer(debugOwner: this),
(LongPressGestureRecognizer instance) {
instance
..onLongPress = onLongPress
..onLongPressStart = onLongPressStart
..onLongPressMoveUpdate = onLongPressMoveUpdate
..onLongPressEnd = onLongPressEnd
..onLongPressUp = onLongPressUp
..onSecondaryLongPress = onSecondaryLongPress
..onSecondaryLongPressStart = onSecondaryLongPressStart
..onSecondaryLongPressMoveUpdate = onSecondaryLongPressMoveUpdate
..onSecondaryLongPressEnd =onSecondaryLongPressEnd
..onSecondaryLongPressEnd = onSecondaryLongPressEnd
..onSecondaryLongPressUp = onSecondaryLongPressUp;
},
);
@ -865,6 +856,7 @@ class GestureDetector extends StatelessWidget {
child: child,
);
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);

View file

@ -381,6 +381,42 @@ void main() {
}, 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 {
int forcePressStart = 0;
int forcePressPeaked = 0;