getVelocityEstimate method

  1. @override
VelocityEstimate getVelocityEstimate()
override

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.

Implementation

@override
VelocityEstimate getVelocityEstimate() {
  // Has user recently moved since last sample?
  if (_sinceLastSample.elapsedMilliseconds > VelocityTracker._assumePointerMoveStoppedMilliseconds) {
    return const VelocityEstimate(
      pixelsPerSecond: Offset.zero,
      confidence: 1.0,
      duration: Duration.zero,
      offset: Offset.zero,
    );
  }

  // The velocity estimated using this expression is an approximation of the
  // scroll velocity of a macOS scroll view at the moment the user touch was
  // released.
  final Offset estimatedVelocity = _previousVelocityAt(-2) * 0.15
                                 + _previousVelocityAt(-1) * 0.65
                                 + _previousVelocityAt(0) * 0.2;

  final _PointAtTime? newestSample = _touchSamples[_index];
  _PointAtTime? oldestNonNullSample;

  for (int i = 1; i <= IOSScrollViewFlingVelocityTracker._sampleSize; i += 1) {
    oldestNonNullSample = _touchSamples[(_index + i) % IOSScrollViewFlingVelocityTracker._sampleSize];
    if (oldestNonNullSample != null) {
      break;
    }
  }

  if (oldestNonNullSample == null || newestSample == null) {
    assert(false, 'There must be at least 1 point in _touchSamples: $_touchSamples');
    return const VelocityEstimate(
      pixelsPerSecond: Offset.zero,
      confidence: 0.0,
      duration: Duration.zero,
      offset: Offset.zero,
    );
  } else {
    return VelocityEstimate(
      pixelsPerSecond: estimatedVelocity,
      confidence: 1.0,
      duration: newestSample.time - oldestNonNullSample.time,
      offset: newestSample.point - oldestNonNullSample.point,
    );
  }
}