From 28ab296949ecb63bfc15f75982d8dba78c3ae8d6 Mon Sep 17 00:00:00 2001 From: Darshan Rander Date: Tue, 13 Oct 2020 17:03:47 +0530 Subject: [PATCH] TickerProvider -> Singletickerprovider (#124) * changes made to fade (#119) * changes made in rotate (#119) * changes made in scale (#119) * TickerProvider->SingleTickerProvider (fade)(#119) --- lib/src/fade.dart | 35 +++++++++++---------- lib/src/rotate.dart | 75 +++++++++++++++++++++++---------------------- lib/src/scale.dart | 64 ++++++++++++++++++++------------------ 3 files changed, 92 insertions(+), 82 deletions(-) diff --git a/lib/src/fade.dart b/lib/src/fade.dart index f47f023..5c77c83 100644 --- a/lib/src/fade.dart +++ b/lib/src/fade.dart @@ -99,7 +99,7 @@ class FadeAnimatedTextKit extends StatefulWidget { } class _FadeTextState extends State - with TickerProviderStateMixin { + with SingleTickerProviderStateMixin { Animation _fadeIn, _fadeOut; AnimationController _controller; @@ -135,6 +135,7 @@ class _FadeTextState extends State _texts.add({'text': text, 'pause': _pause}); }); + _initAnimation(); _nextAnimation(); } @@ -171,6 +172,22 @@ class _FadeTextState extends State ); } + void _initAnimation() { + _controller = AnimationController( + duration: _duration, + vsync: this, + ); + + _fadeIn = Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation( + parent: _controller, + curve: const Interval(0.0, 0.5, curve: Curves.linear))); + + _fadeOut = Tween(begin: 1.0, end: 0.0).animate(CurvedAnimation( + parent: _controller, + curve: const Interval(0.8, 1.0, curve: Curves.linear))) + ..addStatusListener(_animationEndCallback); + } + void _nextAnimation() { final bool isLast = _index == widget.text.length - 1; @@ -199,21 +216,7 @@ class _FadeTextState extends State if (mounted) setState(() {}); - _controller = AnimationController( - duration: _duration, - vsync: this, - ); - - _fadeIn = Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation( - parent: _controller, - curve: const Interval(0.0, 0.5, curve: Curves.linear))); - - _fadeOut = Tween(begin: 1.0, end: 0.0).animate(CurvedAnimation( - parent: _controller, - curve: const Interval(0.8, 1.0, curve: Curves.linear))) - ..addStatusListener(_animationEndCallback); - - _controller.forward(); + _controller.forward(from: 0.0); } void _setPause() { diff --git a/lib/src/rotate.dart b/lib/src/rotate.dart index 2f74b53..55578a8 100644 --- a/lib/src/rotate.dart +++ b/lib/src/rotate.dart @@ -98,7 +98,7 @@ class RotateAnimatedTextKit extends StatefulWidget { } class _RotatingTextState extends State - with TickerProviderStateMixin { + with SingleTickerProviderStateMixin { AnimationController _controller; double _transitionHeight; @@ -137,6 +137,7 @@ class _RotatingTextState extends State _texts.add({'text': text, 'pause': _pause}); }); + _initAnimation(); _nextAnimation(); } @@ -179,40 +180,7 @@ class _RotatingTextState extends State ); } - void _nextAnimation() { - final bool isLast = _index == widget.text.length - 1; - - _isCurrentlyPausing = false; - - // Handling onNext callback - if (_index > -1) { - widget.onNext?.call(_index, isLast); - } - - if (isLast) { - if (widget.isRepeatingAnimation && - (widget.repeatForever || - _currentRepeatCount != (widget.totalRepeatCount - 1))) { - _index = 0; - if (!widget.repeatForever) { - _currentRepeatCount++; - } - } else { - widget.onFinished?.call(); - return; - } - } else { - _index++; - } - - if (mounted) setState(() {}); - - if (widget.transitionHeight == null) { - _transitionHeight = widget.textStyle.fontSize * 10 / 3; - } else { - _transitionHeight = widget.transitionHeight; - } - + void _initAnimation() { _controller = AnimationController( duration: _duration, vsync: this, @@ -253,8 +221,43 @@ class _RotatingTextState extends State parent: _controller, curve: const Interval(0.7, 1.0, curve: Curves.easeIn))) ..addStatusListener(_animationEndCallback); + } + + void _nextAnimation() { + final bool isLast = _index == widget.text.length - 1; + + _isCurrentlyPausing = false; + + // Handling onNext callback + if (_index > -1) { + widget.onNext?.call(_index, isLast); + } + + if (isLast) { + if (widget.isRepeatingAnimation && + (widget.repeatForever || + _currentRepeatCount != (widget.totalRepeatCount - 1))) { + _index = 0; + if (!widget.repeatForever) { + _currentRepeatCount++; + } + } else { + widget.onFinished?.call(); + return; + } + } else { + _index++; + } + + if (mounted) setState(() {}); + + if (widget.transitionHeight == null) { + _transitionHeight = widget.textStyle.fontSize * 10 / 3; + } else { + _transitionHeight = widget.transitionHeight; + } - _controller.forward(); + _controller.forward(from: 0.0); } void _setPause() { diff --git a/lib/src/scale.dart b/lib/src/scale.dart index 19eb83a..4bd8984 100644 --- a/lib/src/scale.dart +++ b/lib/src/scale.dart @@ -105,7 +105,7 @@ class ScaleAnimatedTextKit extends StatefulWidget { } class _ScaleTextState extends State - with TickerProviderStateMixin { + with SingleTickerProviderStateMixin { AnimationController _controller; Animation _fadeIn, _fadeOut, _scaleIn, _scaleOut; @@ -141,6 +141,8 @@ class _ScaleTextState extends State _texts.add({'text': text, 'pause': _pause}); }); + // init controller and animations + _initAnimation(); // Start animation _nextAnimation(); } @@ -181,34 +183,7 @@ class _ScaleTextState extends State ); } - void _nextAnimation() { - final bool isLast = _index == widget.text.length - 1; - - _isCurrentlyPausing = false; - - // Handling onNext callback - if (_index > -1) { - widget.onNext?.call(_index, isLast); - } - - if (isLast) { - if (widget.isRepeatingAnimation && - (widget.repeatForever || - _currentRepeatCount != (widget.totalRepeatCount - 1))) { - _index = 0; - if (!widget.repeatForever) { - _currentRepeatCount++; - } - } else { - widget.onFinished?.call(); - return; - } - } else { - _index++; - } - - setState(() {}); - + void _initAnimation() { _controller = AnimationController( duration: _duration, vsync: this, @@ -239,8 +214,37 @@ class _ScaleTextState extends State curve: Curves.easeIn, ))) ..addStatusListener(_animationEndCallback); + } + + void _nextAnimation() { + final bool isLast = _index == widget.text.length - 1; + + _isCurrentlyPausing = false; + + // Handling onNext callback + if (_index > -1) { + widget.onNext?.call(_index, isLast); + } + + if (isLast) { + if (widget.isRepeatingAnimation && + (widget.repeatForever || + _currentRepeatCount != (widget.totalRepeatCount - 1))) { + _index = 0; + if (!widget.repeatForever) { + _currentRepeatCount++; + } + } else { + widget.onFinished?.call(); + return; + } + } else { + _index++; + } + + setState(() {}); - _controller.forward(); + _controller.forward(from: 0.0); } void _setPause() {