Skip to content

Instantly share code, notes, and snippets.

@praisegeek
Forked from stargazing-dino/animated_count.dart
Created June 2, 2020 19:17
Show Gist options
  • Save praisegeek/3a09ef8cb77847d72beb8f93c0adcba9 to your computer and use it in GitHub Desktop.
Save praisegeek/3a09ef8cb77847d72beb8f93c0adcba9 to your computer and use it in GitHub Desktop.
Animated Count
import 'package:flutter/material.dart';
class AnimatedCount extends ImplicitlyAnimatedWidget {
AnimatedCount({
Key key,
@required this.count,
@required Duration duration,
Curve curve = Curves.linear,
}) : super(duration: duration, curve: curve, key: key);
final num count;
@override
ImplicitlyAnimatedWidgetState<ImplicitlyAnimatedWidget> createState() {
return _AnimatedCountState();
}
}
class _AnimatedCountState extends AnimatedWidgetBaseState<AnimatedCount> {
IntTween _intCount;
Tween<double> _doubleCount;
@override
Widget build(BuildContext context) {
return widget.count is int
? Text(_intCount.evaluate(animation).toString())
: Text(_doubleCount.evaluate(animation).toStringAsFixed(1));
}
@override
void forEachTween(TweenVisitor visitor) {
if (widget.count is int) {
_intCount = visitor(
_intCount,
widget.count,
(dynamic value) => IntTween(begin: value),
);
} else {
_doubleCount = visitor(
_doubleCount,
widget.count,
(dynamic value) => Tween<double>(begin: value),
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment