diff --git a/packages/flutter/lib/src/gestures/multidrag.dart b/packages/flutter/lib/src/gestures/multidrag.dart index 831ab8c83a..d40b81e673 100644 --- a/packages/flutter/lib/src/gestures/multidrag.dart +++ b/packages/flutter/lib/src/gestures/multidrag.dart @@ -123,7 +123,7 @@ abstract class MultiDragPointerState { assert(_arenaEntry != null); if (_client != null) { assert(pendingDelta == null); - final DragEndDetails details = new DragEndDetails(velocity: _velocityTracker.getVelocity() ?? Velocity.zero); + final DragEndDetails details = new DragEndDetails(velocity: _velocityTracker.getVelocity()); final Drag client = _client; _client = null; // Call client last to avoid reentrancy. diff --git a/packages/flutter/lib/src/gestures/scale.dart b/packages/flutter/lib/src/gestures/scale.dart index 2410f960ce..2c459a0647 100644 --- a/packages/flutter/lib/src/gestures/scale.dart +++ b/packages/flutter/lib/src/gestures/scale.dart @@ -193,7 +193,7 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer { assert(tracker != null); Velocity velocity = tracker.getVelocity(); - if (velocity != null && _isFlingGesture(velocity)) { + if (_isFlingGesture(velocity)) { final Offset pixelsPerSecond = velocity.pixelsPerSecond; if (pixelsPerSecond.distanceSquared > kMaxFlingVelocity * kMaxFlingVelocity) velocity = new Velocity(pixelsPerSecond: (pixelsPerSecond / pixelsPerSecond.distance) * kMaxFlingVelocity); diff --git a/packages/flutter/lib/src/gestures/velocity_tracker.dart b/packages/flutter/lib/src/gestures/velocity_tracker.dart index 169ea41732..bc3d4063f8 100644 --- a/packages/flutter/lib/src/gestures/velocity_tracker.dart +++ b/packages/flutter/lib/src/gestures/velocity_tracker.dart @@ -125,18 +125,16 @@ class _PointAtTime { String toString() => '_PointAtTime($point at $time)'; } -/// Computes a pointer's velocity based on data from PointerMove events. +/// Computes a pointer's velocity based on data from [PointerMove] events. /// -/// The input data is provided by calling addPosition(). Adding data -/// is cheap. +/// The input data is provided by calling [addPosition]. Adding data is cheap. /// -/// To obtain a velocity, call [getVelocity] or [getVelocityEstimate]. -/// This will compute the velocity based on the data added so far. Only -/// call this when you need to use the velocity, as it is comparatively -/// expensive. +/// To obtain a velocity, call [getVelocity] or [getVelocityEstimate]. This will +/// compute the velocity based on the data added so far. Only call these when +/// you need to use the velocity, as they are comparatively expensive. /// -/// The quality of the velocity estimation will be better if more data -/// points have been received. +/// The quality of the velocity estimation will be better if more data points +/// have been received. class VelocityTracker { static const int _kAssumePointerMoveStoppedMilliseconds = 40; static const int _kHistorySize = 20; @@ -157,6 +155,10 @@ class VelocityTracker { /// Returns an estimate of the velocity of the object being tracked by the /// tracker given the current information available to the tracker. + /// + /// Information is added using [addPosition]. + /// + /// Returns null if there is no data on which to base an estimate. VelocityEstimate getVelocityEstimate() { final List x = []; final List y = []; @@ -228,12 +230,12 @@ class VelocityTracker { /// /// This can be expensive. Only call this when you need the velocity. /// - /// getVelocity() will return null if no estimate is available or if - /// the velocity is zero. + /// Returns [Velocity.zero] if there is no data from which to compute an + /// estimate or if the estimated velocity is zero. Velocity getVelocity() { final VelocityEstimate estimate = getVelocityEstimate(); if (estimate == null || estimate.pixelsPerSecond == Offset.zero) - return null; + return Velocity.zero; return new Velocity(pixelsPerSecond: estimate.pixelsPerSecond); } } diff --git a/packages/flutter/test/gestures/velocity_tracker_test.dart b/packages/flutter/test/gestures/velocity_tracker_test.dart index 42b0e4496c..ecc5513c4b 100644 --- a/packages/flutter/test/gestures/velocity_tracker_test.dart +++ b/packages/flutter/test/gestures/velocity_tracker_test.dart @@ -71,4 +71,9 @@ void main() { } } }); + + test('No data velocity estimation', () { + final VelocityTracker tracker = new VelocityTracker(); + expect(tracker.getVelocity(), Velocity.zero); + }); }