Skip to content

Instantly share code, notes, and snippets.

@Heilum
Created November 1, 2024 03:11
Show Gist options
  • Save Heilum/6dce78a0c7776196a3902ec5187f2306 to your computer and use it in GitHub Desktop.
Save Heilum/6dce78a0c7776196a3902ec5187f2306 to your computer and use it in GitHub Desktop.
AnimatedCount
import 'package:flutter/material.dart';
class AnimatedCount extends ImplicitlyAnimatedWidget {
const AnimatedCount({
super.key,
required this.count,
super.duration = const Duration(milliseconds: 600),
super.curve = Curves.fastOutSlowIn,
required this.formatter,
this.style,
});
final double count;
final String Function(double) formatter;
final TextStyle? style;
@override
ImplicitlyAnimatedWidgetState<ImplicitlyAnimatedWidget> createState() {
return _AnimatedCountState();
}
}
class _AnimatedCountState extends AnimatedWidgetBaseState<AnimatedCount> {
late Tween<double> _doubleCount;
@override
void initState() {
_doubleCount = Tween<double>(begin: widget.count, end: widget.count);
super.initState();
}
@override
Widget build(BuildContext context) {
return Text(widget.formatter(_doubleCount.evaluate(animation)),
style: widget.style);
}
@override
void forEachTween(TweenVisitor<dynamic> visitor) {
_doubleCount = visitor(
_doubleCount,
widget.count,
(dynamic value) => Tween<double>(begin: value),
) as Tween<double>;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment