From 952e73583f5944afd4b4ba4d3e7a55af02f9871a Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Thu, 3 Sep 2015 12:10:28 -0700 Subject: [PATCH] Fix minor problems in _ScrollGestureRecognizer, Dismissable Alternating scroll gestures would sometimes be ignored because _ScrollGestureRecognizer didn't always reset its _state when the pointer[s] went up. A Dismissable dismiss triggered by a drag and then a fling could cause the next attempt to drag-dimiss to fail. Fixed the definition of lerpColor(). --- packages/flutter/lib/gestures/scroll.dart | 1 + .../flutter/lib/src/widgets/dismissable.dart | 11 +++--- .../lib/src/widgets/gesture_detector.dart | 3 +- .../test/widget/gesture_detector_test.dart | 37 ++++++++++++++++++- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/packages/flutter/lib/gestures/scroll.dart b/packages/flutter/lib/gestures/scroll.dart index 9f09815c3f..e7845458c1 100644 --- a/packages/flutter/lib/gestures/scroll.dart +++ b/packages/flutter/lib/gestures/scroll.dart @@ -78,6 +78,7 @@ abstract class _ScrollGestureRecognizer extends GestureRecogn void didStopTrackingLastPointer() { if (_state == ScrollState.possible) { resolve(GestureDisposition.rejected); + _state = ScrollState.ready; return; } bool wasAccepted = (_state == ScrollState.accepted); diff --git a/packages/flutter/lib/src/widgets/dismissable.dart b/packages/flutter/lib/src/widgets/dismissable.dart index 321fc1c9f8..fa511a710d 100644 --- a/packages/flutter/lib/src/widgets/dismissable.dart +++ b/packages/flutter/lib/src/widgets/dismissable.dart @@ -18,7 +18,7 @@ final Interval _kCardDismissResizeInterval = new Interval(0.4, 1.0); const double _kMinFlingVelocity = 700.0; const double _kMinFlingVelocityDelta = 400.0; const double _kFlingVelocityScale = 1.0 / 300.0; -const double _kDismissCardThreshold = 0.6; +const double _kDismissCardThreshold = 0.4; typedef void ResizedCallback(); typedef void DismissedCallback(); @@ -140,12 +140,11 @@ class Dismissable extends StatefulComponent { return EventDisposition.ignored; _dragUnderway = false; - if (_isHorizontalFlingGesture(event)) { + if (_fadePerformance.isCompleted) { // drag then fling + _startResizePerformance(); + } else if (_isHorizontalFlingGesture(event)) { _dragX = event.velocityX.sign; - if (_fadePerformance.isCompleted) - _startResizePerformance(); - else - _fadePerformance.fling(velocity: event.velocityX.abs() * _kFlingVelocityScale); + _fadePerformance.fling(velocity: event.velocityX.abs() * _kFlingVelocityScale); } else { _fadePerformance.reverse(); } diff --git a/packages/flutter/lib/src/widgets/gesture_detector.dart b/packages/flutter/lib/src/widgets/gesture_detector.dart index 847e0b2f13..7b8a28abf8 100644 --- a/packages/flutter/lib/src/widgets/gesture_detector.dart +++ b/packages/flutter/lib/src/widgets/gesture_detector.dart @@ -187,8 +187,7 @@ class GestureDetector extends StatefulComponent { } GestureRecognizer _ensureDisposed(GestureRecognizer recognizer) { - if (recognizer != null) - recognizer.dispose(); + recognizer?.dispose(); return null; } diff --git a/packages/unit/test/widget/gesture_detector_test.dart b/packages/unit/test/widget/gesture_detector_test.dart index 084c1d8d27..f30798549a 100644 --- a/packages/unit/test/widget/gesture_detector_test.dart +++ b/packages/unit/test/widget/gesture_detector_test.dart @@ -41,13 +41,13 @@ void main() { expect(didEndScroll, isFalse); Point secondLocation = new Point(10.0, 9.0); - tester.dispatchEvent(pointer.move(secondLocation), secondLocation); + tester.dispatchEvent(pointer.move(secondLocation), firstLocation); expect(didStartScroll, isFalse); expect(updatedScrollDelta, 1.0); updatedScrollDelta = null; expect(didEndScroll, isFalse); - tester.dispatchEvent(pointer.up(), secondLocation); + tester.dispatchEvent(pointer.up(), firstLocation); expect(didStartScroll, isFalse); expect(updatedScrollDelta, isNull); expect(didEndScroll, isTrue); @@ -55,4 +55,37 @@ void main() { tester.pumpFrame(() => new Container()); }); + + test('Match two scroll gestures in succession', () { + WidgetTester tester = new WidgetTester(); + TestPointer pointer = new TestPointer(7); + + int gestureCount = 0; + double dragDistance = 0.0; + + Point downLocation = new Point(10.0, 10.0); + Point upLocation = new Point(10.0, 20.0); + + Widget builder() { + return new GestureDetector( + onVerticalScrollUpdate: (double delta) { dragDistance += delta; }, + onVerticalScrollEnd: () { gestureCount += 1; }, + onHorizontalScrollUpdate: (_) { fail("gesture should not match"); }, + onHorizontalScrollEnd: () { fail("gesture should not match"); }, + child: new Container() + ); + } + tester.pumpFrame(builder); + + tester.dispatchEvent(pointer.down(downLocation), downLocation); + tester.dispatchEvent(pointer.move(upLocation), downLocation); + tester.dispatchEvent(pointer.up(), downLocation); + + tester.dispatchEvent(pointer.down(downLocation), downLocation); + tester.dispatchEvent(pointer.move(upLocation), downLocation); + tester.dispatchEvent(pointer.up(), downLocation); + + expect(gestureCount, 2); + expect(dragDistance, -20.0); + }); }