getOnePainterWidget static method
Widget
getOnePainterWidget({
- required Size size,
- required CustomPainter painter(
- double progress
- bool isRepeat = false,
- bool isRepeatWithReverse = false,
- Duration duration = const Duration(milliseconds: 200),
- Animation<
double> ? onAnimation(- 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));
},
);
},
);
}