Commit 24b40d87 authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Make getVelocity never return null (#9583)

Fixes https://github.com/flutter/flutter/issues/8425
parent ad1c497c
......@@ -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.
......
......@@ -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);
......
......@@ -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<double> x = <double>[];
final List<double> y = <double>[];
......@@ -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);
}
}
......@@ -71,4 +71,9 @@ void main() {
}
}
});
test('No data velocity estimation', () {
final VelocityTracker tracker = new VelocityTracker();
expect(tracker.getVelocity(), Velocity.zero);
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment