animateTo method

  1. @override
TickerFuture animateTo(
  1. double target, {
  2. double? from,
})

Runs an animation in which the value transitions to match the target.

If from is non-null, it will be set as the value before the animation starts.

If the target is greater than the current value, the status will show as AnimationStatus.forward while the animation is running and AnimationStatus.completed when it ends; likewise, if the target is less than the current value, the status is reported as AnimationStatus.reverse while the animation runs and AnimationStatus.dismissed when it's over.

If maintainSpeed is false, the animation runs for the full duration (or reverseDuration, if applicable). Otherwise, the animation lasts for a fraction of the specified duration, based on the difference between the target and the current value.

Implementation

@override
TickerFuture animateTo(double target, {double? from}) {
  assert(_ticker != null, 'Cannot animate after the ToggleAnimation is disposed of.');
  assert(
    0.0 <= target && target <= 1.0,
    'The target value (${target.toStringAsFixed(2)}) must be in the range [0.0, 1.0].',
  );
  assert(
    from == null || 0.0 <= from && from <= 1.0,
    'The "from" value (${from.toStringAsFixed(2)}) must be in the range [0.0, 1.0].',
  );

  _ticker!.stop(canceled: true);
  _target = target;
  if (from != null) {
    _value = _from = from;
  } else {
    _from = _value;
  }
  if (maintainSpeed) {
    _targetProgress = (_target - _from).abs();
  }

  if (target == _value) {
    return TickerFuture.complete();
  }
  if (duration == Duration.zero) {
    _value = target;
    notifyListeners();
    _statusUpdate();
    return TickerFuture.complete();
  }
  final TickerFuture tickerFuture = _ticker!.start();
  _statusUpdate();
  return _currentAnimation = tickerFuture;
}