fling method

TickerFuture fling({
  1. double velocity = 1.0,
  2. SpringDescription? springDescription,
  3. AnimationBehavior? animationBehavior,
})

Drives the animation with a spring (within lowerBound and upperBound) and initial velocity.

If velocity is positive, the animation will complete, otherwise it will dismiss. The velocity is specified in units per second. If the SemanticsBinding.disableAnimations flag is set, the velocity is somewhat arbitrarily multiplied by 200.

The springDescription parameter can be used to specify a custom SpringType.criticallyDamped or SpringType.overDamped spring with which to drive the animation. By default, a SpringType.criticallyDamped spring is used. See SpringDescription.withDampingRatio for how to create a suitable SpringDescription.

The resulting spring simulation cannot be of type SpringType.underDamped; such a spring would oscillate rather than fling.

Returns a TickerFuture that completes when the animation is complete.

The most recently returned TickerFuture, if any, is marked as having been canceled, meaning the future never completes and its TickerFuture.orCancel derivative future completes with a TickerCanceled error.

Implementation

TickerFuture fling({ double velocity = 1.0, SpringDescription? springDescription, AnimationBehavior? animationBehavior }) {
  springDescription ??= _kFlingSpringDescription;
  _direction = velocity < 0.0 ? _AnimationDirection.reverse : _AnimationDirection.forward;
  final double target = velocity < 0.0 ? lowerBound - _kFlingTolerance.distance
                                       : upperBound + _kFlingTolerance.distance;
  final AnimationBehavior behavior = animationBehavior ?? this.animationBehavior;
  final double scale = switch (behavior) {
    // This is arbitrary (it was chosen because it worked for the drawer widget).
    AnimationBehavior.normal when SemanticsBinding.instance.disableAnimations => 200.0,
    AnimationBehavior.normal || AnimationBehavior.preserve => 1.0,
  };
  final SpringSimulation simulation = SpringSimulation(springDescription, value, target, velocity * scale)
    ..tolerance = _kFlingTolerance;
  assert(
    simulation.type != SpringType.underDamped,
    'The specified spring simulation is of type SpringType.underDamped.\n'
    'An underdamped spring results in oscillation rather than a fling. '
    'Consider specifying a different springDescription, or use animateWith() '
    'with an explicit SpringSimulation if an underdamped spring is intentional.',
  );
  stop();
  return _startSimulation(simulation);
}