animate method

Future<void> animate({
  1. required T target,
  2. Duration duration = kAnimation,
  3. Curve? curve,
})

Animate the current value (read via getter) toward target over duration. Returns a future that completes when the animation finishes — or when a subsequent animate supersedes it.

Implementation

Future<void> animate({
  required T target,
  Duration duration = kAnimation,
  Curve? curve,
}) {
  final currentValue = getter();
  if (currentValue == target) return Future<void>.value();

  cancel();

  final dynamic start = currentValue;
  final dynamic difference = (target as dynamic) - start;
  final completer = _completer = Completer<void>();
  final c = curve ?? this.curve ?? Curves.linear;

  _ticker = Ticker((elapsed) {
    if (disposed) {
      cancel();
      return;
    }
    if (elapsed >= duration) {
      setter(target);
      cancel();
      return;
    }
    final progress = elapsed.inMicroseconds / duration.inMicroseconds;
    final curved = c.transform(progress.clamp(0.0, 1.0));
    setter((start + difference * curved) as T);
  })..start();

  return completer.future;
}