Skip to content

Instantly share code, notes, and snippets.

@stargazing-dino
Created July 25, 2019 22:18
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save stargazing-dino/b7d43cd9ad25d614f52aba2c74a597fd to your computer and use it in GitHub Desktop.
Save stargazing-dino/b7d43cd9ad25d614f52aba2c74a597fd 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),
);
}
}
}
@krapjost
Copy link

thank you!

@WizardingStudios
Copy link

I have an exception when I use doubles:

The exception is here:

_doubleCount = visitor(

Exception:

Exception has occurred. _CastError (type 'Null' is not a subtype of type 'double' in type cast)

No problems with integers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment