diff --git a/packages/flutter/lib/src/gestures/long_press.dart b/packages/flutter/lib/src/gestures/long_press.dart index 20c47643b2..61de7a6d75 100644 --- a/packages/flutter/lib/src/gestures/long_press.dart +++ b/packages/flutter/lib/src/gestures/long_press.dart @@ -11,28 +11,48 @@ import 'recognizer.dart'; /// same location for a long period of time. typedef GestureLongPressCallback = void Function(); +/// Signature for when a pointer stops contacting the screen after a long press gesture was detected. +typedef GestureLongPressUpCallback = void Function(); + /// Recognizes when the user has pressed down at the same location for a long /// period of time. class LongPressGestureRecognizer extends PrimaryPointerGestureRecognizer { /// Creates a long-press gesture recognizer. /// /// Consider assigning the [onLongPress] callback after creating this object. - LongPressGestureRecognizer({ Object debugOwner }) : super(deadline: kLongPressTimeout, debugOwner: debugOwner); + LongPressGestureRecognizer({ Object debugOwner }) + : super(deadline: kLongPressTimeout, debugOwner: debugOwner); - /// Called when a long-press is recognized. + bool _longPressAccepted = false; + + /// Called when a long press gesture has been recognized. GestureLongPressCallback onLongPress; + /// Called when the pointer stops contacting the screen after the long-press gesture has been recognized. + GestureLongPressUpCallback onLongPressUp; + @override void didExceedDeadline() { resolve(GestureDisposition.accepted); - if (onLongPress != null) + _longPressAccepted = true; + if (onLongPress != null) { invokeCallback('onLongPress', onLongPress); + } } @override void handlePrimaryPointer(PointerEvent event) { - if (event is PointerUpEvent) - resolve(GestureDisposition.rejected); + if (event is PointerUpEvent) { + if (_longPressAccepted == true && onLongPressUp != null) { + _longPressAccepted = false; + invokeCallback('onLongPressUp', onLongPressUp); + } else { + resolve(GestureDisposition.rejected); + } + } else if (event is PointerDownEvent || event is PointerCancelEvent) { + // the first touch, initialize the flag with false + _longPressAccepted = false; + } } @override diff --git a/packages/flutter/lib/src/widgets/gesture_detector.dart b/packages/flutter/lib/src/widgets/gesture_detector.dart index 8006a07c5f..ee9617aeb1 100644 --- a/packages/flutter/lib/src/widgets/gesture_detector.dart +++ b/packages/flutter/lib/src/widgets/gesture_detector.dart @@ -150,6 +150,7 @@ class GestureDetector extends StatelessWidget { this.onTapCancel, this.onDoubleTap, this.onLongPress, + this.onLongPressUp, this.onVerticalDragDown, this.onVerticalDragStart, this.onVerticalDragUpdate, @@ -242,6 +243,9 @@ class GestureDetector extends StatelessWidget { /// a long period of time. final GestureLongPressCallback onLongPress; + /// A pointer that has triggered a long-press has stopped contacting the screen. + final GestureLongPressUpCallback onLongPressUp; + /// A pointer has contacted the screen and might begin to move vertically. final GestureDragDownCallback onVerticalDragDown; @@ -348,12 +352,13 @@ class GestureDetector extends StatelessWidget { ); } - if (onLongPress != null) { + if (onLongPress != null || onLongPressUp !=null) { gestures[LongPressGestureRecognizer] = GestureRecognizerFactoryWithHandlers( () => LongPressGestureRecognizer(debugOwner: this), (LongPressGestureRecognizer instance) { instance - ..onLongPress = onLongPress; + ..onLongPress = onLongPress + ..onLongPressUp = onLongPressUp; }, ); } diff --git a/packages/flutter/test/gestures/long_press_test.dart b/packages/flutter/test/gestures/long_press_test.dart index bff22768f0..10c6c4eb22 100644 --- a/packages/flutter/test/gestures/long_press_test.dart +++ b/packages/flutter/test/gestures/long_press_test.dart @@ -137,4 +137,25 @@ void main() { drag.dispose(); }); + testGesture('Should recognize long press up', (GestureTester tester) { + final LongPressGestureRecognizer longPress = LongPressGestureRecognizer(); + + bool longPressUpRecognized = false; + longPress.onLongPressUp = () { + longPressUpRecognized = true; + }; + + longPress.addPointer(down); + tester.closeArena(5); + expect(longPressUpRecognized, isFalse); + tester.route(down); // kLongPressTimeout = 500; + expect(longPressUpRecognized, isFalse); + tester.async.elapse(const Duration(milliseconds: 300)); + expect(longPressUpRecognized, isFalse); + tester.async.elapse(const Duration(milliseconds: 700)); + tester.route(up); + expect(longPressUpRecognized, isTrue); + + longPress.dispose(); + }); } diff --git a/packages/flutter/test/widgets/gesture_detector_test.dart b/packages/flutter/test/widgets/gesture_detector_test.dart index 67a0652a15..ac2800f283 100644 --- a/packages/flutter/test/widgets/gesture_detector_test.dart +++ b/packages/flutter/test/widgets/gesture_detector_test.dart @@ -314,4 +314,33 @@ void main() { expect(tap, 0); expect(longPress, 1); }); + + testWidgets('Long Press Up Callback called after long press', (WidgetTester tester) async { + int longPressUp = 0; + + await tester.pumpWidget( + Container( + alignment: Alignment.topLeft, + child: Container( + alignment: Alignment.center, + height: 100.0, + color: const Color(0xFF00FF00), + child: GestureDetector( + onLongPressUp: () { + longPressUp += 1; + }, + ), + ), + ), + ); + + Future longPress(Duration timeout) async { + final TestGesture gesture = await tester.startGesture(const Offset(400.0, 50.0)); + await tester.pump(timeout); + await gesture.up(); + } + + await longPress(kLongPressTimeout + Duration(seconds: 1)); // To make sure the time for long press has occured + expect(longPressUp, 1); + }); }