diff --git a/packages/flutter/lib/src/widgets/gesture_detector.dart b/packages/flutter/lib/src/widgets/gesture_detector.dart index 6ffb2a1b13..1285eb8189 100644 --- a/packages/flutter/lib/src/widgets/gesture_detector.dart +++ b/packages/flutter/lib/src/widgets/gesture_detector.dart @@ -702,15 +702,14 @@ class GestureDetector extends StatelessWidget { Widget build(BuildContext context) { final Map gestures = {}; - 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(debugOwner: this), @@ -741,21 +740,8 @@ class GestureDetector extends StatelessWidget { onLongPressUp != null || onLongPressStart != null || onLongPressMoveUpdate != null || - onLongPressEnd != null) { - gestures[LongPressGestureRecognizer] = GestureRecognizerFactoryWithHandlers( - () => 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); diff --git a/packages/flutter/test/widgets/gesture_detector_test.dart b/packages/flutter/test/widgets/gesture_detector_test.dart index bc8af0e747..1cc8d99086 100644 --- a/packages/flutter/test/widgets/gesture_detector_test.dart +++ b/packages/flutter/test/widgets/gesture_detector_test.dart @@ -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 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;