Skip to content

Commit

Permalink
Merge pull request #133 from awhitford/redundant_texts
Browse files Browse the repository at this point in the history
Removed redundant _texts variables.
  • Loading branch information
aagarwal1012 committed Oct 23, 2020
2 parents fce7109 + dc6ed02 commit 4028ab0
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 191 deletions.
115 changes: 65 additions & 50 deletions lib/src/colorize.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ColorizeAnimatedTextKit extends StatefulWidget {

/// Define the [Duration] of the pause between texts
///
/// By default it is set to 500 milliseconds.
/// By default it is set to 1000 milliseconds.
final Duration pause;

/// Adds the onTap [VoidCallback] to the animated widget.
Expand Down Expand Up @@ -62,22 +62,31 @@ class ColorizeAnimatedTextKit extends StatefulWidget {
/// The [List] should contain at least two values of [Color] in it.
final List<Color> colors;

const ColorizeAnimatedTextKit(
{Key key,
@required this.text,
this.textStyle,
@required this.colors,
this.speed,
this.pause,
this.onTap,
this.onNext,
this.onFinished,
this.alignment = AlignmentDirectional.topStart,
this.textAlign = TextAlign.start,
this.totalRepeatCount = 3,
this.repeatForever = false,
this.isRepeatingAnimation = true})
: super(key: key);
const ColorizeAnimatedTextKit({
Key key,
@required this.text,
this.textStyle,
@required this.colors,
this.speed = const Duration(milliseconds: 200),
this.pause = const Duration(milliseconds: 1000),
this.onTap,
this.onNext,
this.onFinished,
this.alignment = AlignmentDirectional.topStart,
this.textAlign = TextAlign.start,
this.totalRepeatCount = 3,
this.repeatForever = false,
this.isRepeatingAnimation = true,
}) : assert(null != text),
assert(null != colors && colors.length > 1),
assert(null != speed),
assert(null != pause),
assert(null != alignment),
assert(null != textAlign),
assert(null != totalRepeatCount),
assert(null != repeatForever),
assert(null != isRepeatingAnimation),
super(key: key);

@override
_ColorizeTextState createState() => _ColorizeTextState();
Expand All @@ -86,17 +95,15 @@ class ColorizeAnimatedTextKit extends StatefulWidget {
class _ColorizeTextState extends State<ColorizeAnimatedTextKit>
with TickerProviderStateMixin {
AnimationController _controller;
Timer _timer;

Animation _colorShifter, _fadeIn, _fadeOut;
Animation<double> _colorShifter, _fadeIn, _fadeOut;
double _tuning;

Duration _speed;
Duration _pause;

List<Map> _texts = [];

int _index;

final _textCharacters = <Characters>[];

bool _isCurrentlyPausing = false;

int _currentRepeatCount;
Expand All @@ -105,22 +112,21 @@ class _ColorizeTextState extends State<ColorizeAnimatedTextKit>
void initState() {
super.initState();

_speed = widget.speed ?? const Duration(milliseconds: 200);
_pause = widget.pause ?? const Duration(milliseconds: 1000);

_index = -1;

_currentRepeatCount = 0;

widget.text.forEach((text) {
_texts.add({'text': text, 'speed': _speed, 'pause': _pause});
_textCharacters.add(text.characters);
});

_currentRepeatCount = 0;

_nextAnimation();
}

@override
void dispose() {
_timer?.cancel();
_controller?.stop();
_controller?.dispose();
super.dispose();
}
Expand All @@ -131,25 +137,25 @@ class _ColorizeTextState extends State<ColorizeAnimatedTextKit>
onTap: widget.onTap,
child: _isCurrentlyPausing || !_controller.isAnimating
? Text(
_texts[_index]['text'],
widget.text[_index],
style: widget.textStyle,
textAlign: widget.textAlign,
)
: AnimatedBuilder(
animation: _controller,
builder: (BuildContext context, Widget child) {
Shader linearGradient = LinearGradient(colors: widget.colors)
.createShader(
Rect.fromLTWH(0.0, 0.0, _colorShifter.value, 0.0));
final linearGradient =
LinearGradient(colors: widget.colors).createShader(
Rect.fromLTWH(0.0, 0.0, _colorShifter.value, 0.0),
);
return Opacity(
opacity:
_fadeIn.value != 1.0 ? _fadeIn.value : _fadeOut.value,
child: Text(
_texts[_index]['text'],
style: widget.textStyle != null
? widget.textStyle.merge(TextStyle(
foreground: Paint()..shader = linearGradient))
: widget.textStyle,
widget.text[_index],
style: widget.textStyle?.merge(
TextStyle(foreground: Paint()..shader = linearGradient),
),
textAlign: widget.textAlign,
),
);
Expand All @@ -159,7 +165,7 @@ class _ColorizeTextState extends State<ColorizeAnimatedTextKit>
}

void _nextAnimation() {
final bool isLast = _index == widget.text.length - 1;
final isLast = _index == widget.text.length - 1;

_isCurrentlyPausing = false;

Expand All @@ -186,38 +192,47 @@ class _ColorizeTextState extends State<ColorizeAnimatedTextKit>

if (mounted) setState(() {});

final textLen = _textCharacters[_index].length;
_controller = AnimationController(
duration: _texts[_index]['speed'] * _texts[_index]['text'].length,
duration: widget.speed * textLen,
vsync: this,
);

_tuning = (300.0 * widget.colors.length) *
(widget.textStyle.fontSize / 24.0) *
0.75 *
(_texts[_index]['text'].length / 15.0);
(textLen / 15.0);

_fadeIn = Tween<double>(begin: 0.0, end: 1.0).animate(CurvedAnimation(
_fadeIn = Tween<double>(begin: 0.0, end: 1.0).animate(
CurvedAnimation(
parent: _controller,
curve: const Interval(0.0, 0.1, curve: Curves.easeOut)));
curve: const Interval(0.0, 0.1, curve: Curves.easeOut),
),
);

_fadeOut = Tween<double>(begin: 1.0, end: 1.0).animate(CurvedAnimation(
_fadeOut = Tween<double>(begin: 1.0, end: 1.0).animate(
CurvedAnimation(
parent: _controller,
curve: const Interval(0.9, 1.0, curve: Curves.easeIn)));
curve: const Interval(0.9, 1.0, curve: Curves.easeIn),
),
);

_colorShifter =
Tween<double>(begin: 0.0, end: widget.colors.length * _tuning).animate(
CurvedAnimation(
parent: _controller,
curve: const Interval(0.0, 1.0, curve: Curves.easeIn)))
..addStatusListener(_animationEndCallback);
CurvedAnimation(
parent: _controller,
curve: const Interval(0.0, 1.0, curve: Curves.easeIn),
),
)..addStatusListener(_animationEndCallback);

_controller?.forward();
}

void _animationEndCallback(state) {
if (state == AnimationStatus.completed) {
_isCurrentlyPausing = true;
Timer(_texts[_index]['pause'], _nextAnimation);
assert(null == _timer || !_timer.isActive);
_timer = Timer(widget.pause, _nextAnimation);
}
}
}
33 changes: 14 additions & 19 deletions lib/src/fade.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,10 @@ class FadeAnimatedTextKit extends StatefulWidget {

class _FadeTextState extends State<FadeAnimatedTextKit>
with SingleTickerProviderStateMixin {
Animation _fadeIn, _fadeOut;
Animation<double> _fadeIn, _fadeOut;

AnimationController _controller;

final _texts = <Map<String, dynamic>>[];

int _index;

bool _isCurrentlyPausing = false;
Expand All @@ -132,13 +130,6 @@ class _FadeTextState extends State<FadeAnimatedTextKit>

_currentRepeatCount = 0;

widget.text.forEach((text) {
_texts.add({
'text': text,
'pause': widget.pause,
});
});

_initAnimation();
_nextAnimation();
}
Expand All @@ -154,7 +145,7 @@ class _FadeTextState extends State<FadeAnimatedTextKit>
@override
Widget build(BuildContext context) {
final textWidget = Text(
_texts[_index]['text'],
widget.text[_index],
style: widget.textStyle,
textAlign: widget.textAlign,
);
Expand Down Expand Up @@ -198,7 +189,7 @@ class _FadeTextState extends State<FadeAnimatedTextKit>
}

void _nextAnimation() {
final bool isLast = _index == widget.text.length - 1;
final isLast = _index == widget.text.length - 1;

_isCurrentlyPausing = false;

Expand Down Expand Up @@ -229,7 +220,7 @@ class _FadeTextState extends State<FadeAnimatedTextKit>
}

void _setPause() {
final bool isLast = _index == widget.text.length - 1;
final isLast = _index == widget.text.length - 1;

_isCurrentlyPausing = true;
if (mounted) setState(() {});
Expand All @@ -242,7 +233,7 @@ class _FadeTextState extends State<FadeAnimatedTextKit>
if (state == AnimationStatus.completed) {
_isCurrentlyPausing = true;
assert(null == _timer || !_timer.isActive);
_timer = Timer(_texts[_index]['pause'], _nextAnimation);
_timer = Timer(widget.pause, _nextAnimation);
}
}

Expand All @@ -254,16 +245,20 @@ class _FadeTextState extends State<FadeAnimatedTextKit>
_nextAnimation();
}
} else {
final int pause = _texts[_index]['pause'].inMilliseconds;
final int left = widget.duration.inMilliseconds;

_controller?.stop();

_setPause();

assert(null == _timer || !_timer.isActive);
_timer =
Timer(Duration(milliseconds: max(pause, left)), _nextAnimation);
_timer = Timer(
Duration(
milliseconds: max(
widget.pause.inMilliseconds,
widget.duration.inMilliseconds,
),
),
_nextAnimation,
);
}
}

Expand Down
22 changes: 7 additions & 15 deletions lib/src/rotate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,8 @@ class _RotatingTextState extends State<RotateAnimatedTextKit>

double _transitionHeight;

Animation _fadeIn, _fadeOut, _slideIn, _slideOut;

final _texts = <Map<String, dynamic>>[];
Animation<double> _fadeIn, _fadeOut;
Animation<Alignment> _slideIn, _slideOut;

int _index;

Expand All @@ -135,13 +134,6 @@ class _RotatingTextState extends State<RotateAnimatedTextKit>

_currentRepeatCount = 0;

widget.text.forEach((text) {
_texts.add({
'text': text,
'pause': widget.pause,
});
});

_initAnimation();
_nextAnimation();
}
Expand All @@ -157,7 +149,7 @@ class _RotatingTextState extends State<RotateAnimatedTextKit>
@override
Widget build(BuildContext context) {
final textWidget = Text(
_texts[_index]['text'],
widget.text[_index],
style: widget.textStyle,
textAlign: widget.textAlign,
);
Expand Down Expand Up @@ -246,7 +238,7 @@ class _RotatingTextState extends State<RotateAnimatedTextKit>
}

void _nextAnimation() {
final bool isLast = _index == widget.text.length - 1;
final isLast = _index == widget.text.length - 1;

_isCurrentlyPausing = false;

Expand Down Expand Up @@ -277,7 +269,7 @@ class _RotatingTextState extends State<RotateAnimatedTextKit>
}

void _setPause() {
final bool isLast = _index == widget.text.length - 1;
final isLast = _index == widget.text.length - 1;

_isCurrentlyPausing = true;
if (mounted) setState(() {});
Expand All @@ -289,7 +281,7 @@ class _RotatingTextState extends State<RotateAnimatedTextKit>
void _animationEndCallback(state) {
if (state == AnimationStatus.completed) {
assert(null == _timer || !_timer.isActive);
_timer = Timer(_texts[_index]['pause'], _nextAnimation);
_timer = Timer(widget.pause, _nextAnimation);
}
}

Expand All @@ -304,7 +296,7 @@ class _RotatingTextState extends State<RotateAnimatedTextKit>
_setPause();

assert(null == _timer || !_timer.isActive);
_timer = Timer(_texts[_index]['pause'], _nextAnimation);
_timer = Timer(widget.pause, _nextAnimation);
}
}

Expand Down
Loading

0 comments on commit 4028ab0

Please sign in to comment.