Skip to content

Commit

Permalink
Merge pull request #131 from awhitford/defaults
Browse files Browse the repository at this point in the history
Refactored code to move default logic from the State class to the StatefulWidget
  • Loading branch information
awhitford authored Oct 23, 2020
2 parents d4c3346 + 568ce66 commit 26a81ef
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 214 deletions.
81 changes: 45 additions & 36 deletions lib/src/fade.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';

class FadeAnimatedTextKit extends StatefulWidget {
/// List of [String] that would be displayed subsequently in the animation.
Expand Down Expand Up @@ -75,24 +75,34 @@ class FadeAnimatedTextKit extends StatefulWidget {
/// By default it is set to false.
final bool stopPauseOnTap;

const FadeAnimatedTextKit(
{Key key,
@required this.text,
this.duration,
this.textStyle,
this.pause,
this.displayFullTextOnTap = false,
this.stopPauseOnTap = false,
this.onTap,
this.onNext,
this.onNextBeforePause,
this.onFinished,
this.totalRepeatCount = 3,
this.alignment = AlignmentDirectional.topStart,
this.textAlign = TextAlign.start,
this.repeatForever = false,
this.isRepeatingAnimation = true})
: super(key: key);
const FadeAnimatedTextKit({
Key key,
@required this.text,
this.duration = const Duration(milliseconds: 2000),
this.textStyle,
this.pause = const Duration(milliseconds: 500),
this.displayFullTextOnTap = false,
this.stopPauseOnTap = false,
this.onTap,
this.onNext,
this.onNextBeforePause,
this.onFinished,
this.totalRepeatCount = 3,
this.alignment = AlignmentDirectional.topStart,
this.textAlign = TextAlign.start,
this.repeatForever = false,
this.isRepeatingAnimation = true,
}) : assert(null != text),
assert(null != duration),
assert(null != pause),
assert(null != displayFullTextOnTap),
assert(null != stopPauseOnTap),
assert(null != totalRepeatCount),
assert(null != alignment),
assert(null != textAlign),
assert(null != repeatForever),
assert(null != isRepeatingAnimation),
super(key: key);

@override
_FadeTextState createState() => _FadeTextState();
Expand All @@ -103,11 +113,8 @@ class _FadeTextState extends State<FadeAnimatedTextKit>
Animation _fadeIn, _fadeOut;

AnimationController _controller;
List<Widget> textWidgetList = [];

Duration _pause;

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

int _index;

Expand All @@ -117,22 +124,19 @@ class _FadeTextState extends State<FadeAnimatedTextKit>

int _currentRepeatCount;

Duration _duration;

@override
void initState() {
super.initState();

_pause = widget.pause ?? const Duration(milliseconds: 500);

_index = -1;

_currentRepeatCount = 0;

_duration = widget.duration ?? const Duration(milliseconds: 2000);

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

_initAnimation();
Expand Down Expand Up @@ -174,18 +178,23 @@ class _FadeTextState extends State<FadeAnimatedTextKit>

void _initAnimation() {
_controller = AnimationController(
duration: _duration,
duration: widget.duration,
vsync: this,
);

_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.5, curve: Curves.linear)));
curve: const Interval(0.0, 0.5, curve: Curves.linear),
),
);

_fadeOut = Tween<double>(begin: 1.0, end: 0.0).animate(CurvedAnimation(
_fadeOut = Tween<double>(begin: 1.0, end: 0.0).animate(
CurvedAnimation(
parent: _controller,
curve: const Interval(0.8, 1.0, curve: Curves.linear)))
..addStatusListener(_animationEndCallback);
curve: const Interval(0.8, 1.0, curve: Curves.linear),
),
)..addStatusListener(_animationEndCallback);
}

void _nextAnimation() {
Expand Down
140 changes: 78 additions & 62 deletions lib/src/rotate.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/material.dart';

class RotateAnimatedTextKit extends StatefulWidget {
/// List of [String] that would be displayed subsequently in the animation.
Expand Down Expand Up @@ -74,24 +74,33 @@ class RotateAnimatedTextKit extends StatefulWidget {
/// By default it is set to false.
final bool displayFullTextOnTap;

const RotateAnimatedTextKit(
{Key key,
@required this.text,
this.textStyle,
this.transitionHeight,
this.pause,
this.onNext,
this.onNextBeforePause,
this.onFinished,
this.totalRepeatCount = 3,
this.duration,
this.onTap,
this.alignment = const Alignment(0.0, 0.0),
this.textAlign = TextAlign.start,
this.displayFullTextOnTap = false,
this.repeatForever = false,
this.isRepeatingAnimation = true})
: super(key: key);
const RotateAnimatedTextKit({
Key key,
@required this.text,
this.textStyle,
this.transitionHeight,
this.pause = const Duration(milliseconds: 500),
this.onNext,
this.onNextBeforePause,
this.onFinished,
this.totalRepeatCount = 3,
this.duration = const Duration(milliseconds: 2000),
this.onTap,
this.alignment = const Alignment(0.0, 0.0),
this.textAlign = TextAlign.start,
this.displayFullTextOnTap = false,
this.repeatForever = false,
this.isRepeatingAnimation = true,
}) : assert(null != text),
assert(null != pause),
assert(null != totalRepeatCount),
assert(null != duration),
assert(null != alignment),
assert(null != textAlign),
assert(null != displayFullTextOnTap),
assert(null != repeatForever),
assert(null != isRepeatingAnimation),
super(key: key);

@override
_RotatingTextState createState() => _RotatingTextState();
Expand All @@ -105,11 +114,7 @@ class _RotatingTextState extends State<RotateAnimatedTextKit>

Animation _fadeIn, _fadeOut, _slideIn, _slideOut;

List<Widget> textWidgetList = [];

Duration _pause;

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

int _index;

Expand All @@ -119,22 +124,22 @@ class _RotatingTextState extends State<RotateAnimatedTextKit>

int _currentRepeatCount;

Duration _duration;

@override
void initState() {
super.initState();

_pause = widget.pause ?? const Duration(milliseconds: 500);
_transitionHeight =
widget.transitionHeight ?? (widget.textStyle.fontSize * 10 / 3);

_index = -1;

_currentRepeatCount = 0;

_duration = widget.duration ?? const Duration(milliseconds: 2000);

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

_initAnimation();
Expand Down Expand Up @@ -169,10 +174,10 @@ class _RotatingTextState extends State<RotateAnimatedTextKit>
return AlignTransition(
alignment: _slideIn.value.y != 0.0 ? _slideIn : _slideOut,
child: Opacity(
opacity: _fadeIn.value != 1.0
? _fadeIn.value
: _fadeOut.value,
child: child),
opacity:
_fadeIn.value != 1.0 ? _fadeIn.value : _fadeOut.value,
child: child,
),
);
},
),
Expand All @@ -182,45 +187,62 @@ class _RotatingTextState extends State<RotateAnimatedTextKit>

void _initAnimation() {
_controller = AnimationController(
duration: _duration,
duration: widget.duration,
vsync: this,
);

if (_index == 0) {
_slideIn = AlignmentTween(
begin: Alignment(-1.0, -1.0).add(widget.alignment),
end: Alignment(-1.0, 0.0).add(widget.alignment))
.animate(CurvedAnimation(
parent: _controller,
curve: const Interval(0.0, 0.4, curve: Curves.linear)));
begin: const Alignment(-1.0, -1.0) + widget.alignment,
end: const Alignment(-1.0, 0.0) + widget.alignment,
).animate(
CurvedAnimation(
parent: _controller,
curve: const Interval(0.0, 0.4, curve: Curves.linear),
),
);

_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.4, curve: Curves.easeOut)));
curve: const Interval(0.0, 0.4, curve: Curves.easeOut),
),
);
} else {
_slideIn = AlignmentTween(
begin: Alignment(-1.0, -1.0).add(widget.alignment),
end: Alignment(-1.0, 0.0).add(widget.alignment))
.animate(CurvedAnimation(
parent: _controller,
curve: const Interval(0.0, 0.4, curve: Curves.linear)));
begin: const Alignment(-1.0, -1.0) + widget.alignment,
end: const Alignment(-1.0, 0.0) + widget.alignment,
).animate(
CurvedAnimation(
parent: _controller,
curve: const Interval(0.0, 0.4, curve: Curves.linear),
),
);

_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.4, curve: Curves.easeOut)));
curve: const Interval(0.0, 0.4, curve: Curves.easeOut),
),
);
}

_slideOut = AlignmentTween(
begin: Alignment(-1.0, 0.0).add(widget.alignment),
end: Alignment(-1.0, 1.0).add(widget.alignment),
).animate(CurvedAnimation(
begin: const Alignment(-1.0, 0.0) + widget.alignment,
end: const Alignment(-1.0, 1.0) + widget.alignment,
).animate(
CurvedAnimation(
parent: _controller,
curve: const Interval(0.7, 1.0, curve: Curves.linear)));
curve: const Interval(0.7, 1.0, curve: Curves.linear),
),
);

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

void _nextAnimation() {
Expand Down Expand Up @@ -251,12 +273,6 @@ class _RotatingTextState extends State<RotateAnimatedTextKit>

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

if (widget.transitionHeight == null) {
_transitionHeight = widget.textStyle.fontSize * 10 / 3;
} else {
_transitionHeight = widget.transitionHeight;
}

_controller.forward(from: 0.0);
}

Expand Down
Loading

0 comments on commit 26a81ef

Please sign in to comment.