-
-
Save praisegeek/3a09ef8cb77847d72beb8f93c0adcba9 to your computer and use it in GitHub Desktop.
Animated Count
This file contains 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), | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment