startDayNightCycleAnimation method

void startDayNightCycleAnimation({
  1. Duration cycleDuration = const Duration(minutes: 1),
  2. DayNightCycleDirection direction = DayNightCycleDirection.leftToRight,
})

Start the day/night cycle animation with custom speed and direction

Implementation

void startDayNightCycleAnimation({
  Duration cycleDuration = const Duration(minutes: 1),
  DayNightCycleDirection direction = DayNightCycleDirection.leftToRight,
}) {
  _dayNightCycleDirection = direction;

  // If controller exists and duration hasn't changed, just resume
  if (_dayNightCycleController != null &&
      _dayNightCycleDuration == cycleDuration &&
      !_dayNightCycleController!.isAnimating) {
    // Calculate where we should be based on current sun longitude
    final currentValue = _calculateAnimationValueFromSunLongitude();
    _dayNightCycleController!.value = currentValue.clamp(0.0, 1.0);
    _dayNightCycleController!.repeat();
    return;
  }

  // Otherwise, create new controller
  _dayNightCycleController?.dispose();
  _dayNightCycleDuration = cycleDuration;

  // Calculate starting value based on current sun position
  final startValue = _calculateAnimationValueFromSunLongitude();

  _dayNightCycleController = AnimationController(
    vsync: this,
    duration: cycleDuration,
    value: startValue.clamp(0.0, 1.0),
  )..addListener(() {
      if (mounted) {
        // Animate sun longitude based on direction
        widget.controller.sunLongitude =
            _calculateSunLongitudeFromAnimationValue(
          _dayNightCycleController!.value,
        );
        setState(() {});
      }
    });
  _dayNightCycleController!.repeat();
}