getOnePainterWidget static method

Widget getOnePainterWidget({
  1. required Size size,
  2. required CustomPainter painter(
    1. double progress
    ),
  3. bool isRepeat = false,
  4. bool isRepeatWithReverse = false,
  5. Duration duration = const Duration(milliseconds: 200),
  6. Animation<double>? onAnimation(
    1. AnimationController controller
    )?,
})

Implementation

static Widget getOnePainterWidget({
  required Size size,
  required CustomPainter Function(double progress) painter,
  bool isRepeat = false,
  bool isRepeatWithReverse = false,
  Duration duration = const Duration(milliseconds: 200),
  Animation<double>? Function(AnimationController controller)? onAnimation,
}) {
  String controllerKey = 'Painter_${DateTime.now().millisecondsSinceEpoch}_${shortHash(UniqueKey())}';
  return BuilderWithTicker(
    init: (state) {
      BuilderWithTickerState vsync = state as BuilderWithTickerState;
      AnimationController controller = AnimationController(vsync: vsync, duration: duration);
      Broker.setIfAbsent<AnimationController>(controller, key: controllerKey);
      Future.delayed(const Duration(milliseconds: 200), () {
        AnimationController? controller = Broker.getIf<AnimationController>(key: controllerKey);
        isRepeat == true ? controller?.repeat(reverse: isRepeatWithReverse) : controller?.forward();
      });
    },
    dispose: (state) {
      AnimationController? controller = Broker.getIf<AnimationController>(key: controllerKey);
      controller?.stop();
      controller?.dispose();
    },
    builder: (state) {
      AnimationController? controller = Broker.getIf(key: controllerKey);
      assert(controller != null, 'AnimationController cannot be null, should init in initState');
      if (controller == null) {
        return CustomPaint(painter: painter(1.0));
      }
      Animation<double> animation = onAnimation?.call(controller) ?? controller;
      return AnimatedBuilder(
        animation: animation,
        builder: (context, snapshot) {
          return CustomPaint(size: size, painter: painter(animation.value));
        },
      );
    },
  );
}