processTick method

Cmd? processTick(
  1. DateTime now
)

Processes a frame tick. Call this from State.handleUpdate when an AnimationTickMsg with a matching controllerId is received.

Returns a Cmd to schedule the next tick if the animation is still running, or null if the animation has completed (or was stopped).

Implementation

Cmd? processTick(DateTime now) {
  if (!isAnimating) return null;

  // Record the start time on the very first tick.
  _startTime ??= now;
  final elapsed = now.difference(_startTime!);
  final totalDuration = _activeDuration;

  // Zero or null duration → jump to target immediately.
  if (totalDuration == null || totalDuration == Duration.zero) {
    _value = _targetValue;
    _completeAnimation();
    return null;
  }

  // Compute normalized time progress [0.0, 1.0].
  final t = (elapsed.inMicroseconds / totalDuration.inMicroseconds).clamp(
    0.0,
    1.0,
  );

  // Apply curve and interpolate.
  final curvedT = _curve.transform(t);
  _value = _startValue + (_targetValue - _startValue) * curvedT;

  if (t >= 1.0) {
    // Animation segment is complete.
    _value = _targetValue;

    if (_repeating) {
      return _handleRepeatCycle();
    }

    _completeAnimation();
    return null;
  }

  // Still animating — notify and schedule next tick.
  notifyListeners();
  return _scheduleTick();
}