Skip to content

Instantly share code, notes, and snippets.

@urusaich
Created November 21, 2024 07:54
Show Gist options
  • Save urusaich/974ed240e45ad6b38798947d05802340 to your computer and use it in GitHub Desktop.
Save urusaich/974ed240e45ad6b38798947d05802340 to your computer and use it in GitHub Desktop.
class AnimatedValueBuilder<T> extends StatefulWidget {
const AnimatedValueBuilder({
super.key,
required this.duration,
required this.tween,
required this.progress,
required this.builder,
this.child,
}) : assert(progress >= 0 && progress <= 1);
final Duration duration;
final Tween<T> tween;
final double progress;
final ValueWidgetBuilder<T> builder;
final Widget? child;
@override
State<AnimatedValueBuilder<T>> createState() => _AnimatedValueBuilderState();
}
class _AnimatedValueBuilderState<T> extends State<AnimatedValueBuilder<T>>
with SingleTickerProviderStateMixin {
late final AnimationController _controller = AnimationController(
value: widget.progress,
duration: widget.duration,
vsync: this,
);
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
void didUpdateWidget(covariant AnimatedValueBuilder<T> oldWidget) {
super.didUpdateWidget(oldWidget);
_controller.duration = widget.duration;
if (widget.progress != oldWidget.progress) {
_controller.animateTo(widget.progress);
}
}
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _controller,
builder: (context, child) =>
widget.builder(context, widget.tween.evaluate(_controller), child),
child: widget.child,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment