Created
November 1, 2024 03:11
-
-
Save Heilum/6dce78a0c7776196a3902ec5187f2306 to your computer and use it in GitHub Desktop.
AnimatedCount
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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