withRepeatAnimation<T> function

AnimationController withRepeatAnimation<T>({
  1. required TickerProvider vsync,
  2. required Tween<T> tween,
  3. required AnimationUpdateCallBack<T?> callBack,
  4. Duration duration = const Duration(seconds: 1),
  5. double initialValue = 0.0,
  6. Curve curve = Curves.linear,
  7. double? lowerBound,
  8. double? upperBound,
  9. bool isRepeatReversed = false,
  10. Duration? repeatPeriod,
  11. Animation? customAnimation,
})

To perform repeat animation in a simpler way

Implementation

AnimationController withRepeatAnimation<T>(
    {required TickerProvider vsync,
    required Tween<T> tween,
    required AnimationUpdateCallBack<T?> callBack,
    Duration duration = const Duration(seconds: 1),
    double initialValue = 0.0,
    Curve curve = Curves.linear,
    double? lowerBound,
    double? upperBound,
    bool isRepeatReversed = false,
    Duration? repeatPeriod,
    Animation? customAnimation}) {
  final AnimationController controller = AnimationController(
      vsync: vsync, duration: duration, value: initialValue);
  final curveAnimation = CurvedAnimation(parent: controller, curve: curve);
  final Animation animation = customAnimation ?? tween.animate(curveAnimation);
  animation.addListener(() {
    callBack.call(animation.value, controller.value);
  });

  controller
      .repeat(
          min: lowerBound,
          max: upperBound,
          period: repeatPeriod,
          reverse: isRepeatReversed)
      .whenCompleteOrCancel(() {
    controller.dispose();
  });

  return controller;
}