Created
July 25, 2019 22:18
-
-
Save stargazing-dino/b7d43cd9ad25d614f52aba2c74a597fd to your computer and use it in GitHub Desktop.
Animated Count
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 { | |
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), | |
); | |
} | |
} | |
} |
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
thank you!