Created
June 4, 2019 18:14
-
-
Save JorgeCastilloPrz/e3eb891a1ef49f7a9b7082a6faea5dbf to your computer and use it in GitHub Desktop.
Stateful widget used to retain animations to calculate values for painting the arc.
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
class _FabLoadingWidget extends State<FabLoader> | |
with SingleTickerProviderStateMixin { | |
final Widget child; | |
final double strokeWidth; | |
AnimationController _controller; | |
_FabLoadingWidget({@required this.strokeWidth, @required this.child}); | |
@override | |
void initState() { | |
super.initState(); | |
_controller = AnimationController( | |
duration: const Duration(seconds: 5), | |
vsync: this, | |
); | |
_controller.repeat(); // we want it to repeat over and over (indeterminate) | |
} | |
@override | |
void didUpdateWidget(FabLoader oldWidget) { | |
super.didUpdateWidget(oldWidget); | |
// We want to start animation again if the widget is updated. | |
if (!_controller.isAnimating) { | |
_controller.repeat(); | |
} | |
} | |
@override | |
void dispose() { | |
_controller.dispose(); // avoid leaks | |
super.dispose(); | |
} | |
Widget _buildIndicator( | |
double headValue, double tailValue, int stepValue, double rotationValue) { | |
return new CustomPaint( | |
child: child, | |
foregroundPainter: new ArcPainter( | |
strokeWidth: strokeWidth, | |
backgroundColor: widget.backgroundColor, | |
color: widget.color, | |
headValue: headValue, | |
tailValue: tailValue, | |
stepValue: stepValue, | |
rotationValue: rotationValue), | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return AnimatedBuilder( | |
animation: _controller, | |
builder: (BuildContext context, Widget child) { | |
return _buildIndicator( | |
_kStrokeHeadTween.evaluate(_controller), | |
_kStrokeTailTween.evaluate(_controller), | |
_kStepTween.evaluate(_controller), | |
_kRotationTween.evaluate(_controller), | |
); | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment