Skip to content

Commit

Permalink
Merge pull request #164 from awhitford/rtl-colorize
Browse files Browse the repository at this point in the history
Added RTL support for Colorize animation. Resolves #109
  • Loading branch information
aagarwal1012 committed Dec 6, 2020
2 parents c3cbb6c + 0c68d71 commit df9c63a
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions lib/src/colorize.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ class ColorizeAnimatedText extends AnimatedText {
/// The [List] should contain at least two values of [Color] in it.
final List<Color> colors;

/// Specifies the [TextDirection] for animation direction.
///
/// By default it is set to [TextDirection.ltr]
final TextDirection textDirection;

ColorizeAnimatedText(
String text, {
TextAlign textAlign = TextAlign.start,
@required TextStyle textStyle,
this.speed = const Duration(milliseconds: 200),
@required this.colors,
this.textDirection = TextDirection.ltr,
}) : assert(null != speed),
assert(null != colors && colors.length > 1),
super(
Expand All @@ -32,6 +38,8 @@ class ColorizeAnimatedText extends AnimatedText {
);

Animation<double> _colorShifter, _fadeIn, _fadeOut;
// Copy of colors that may be reversed when RTL.
List<Color> _colors;

@override
void initAnimation(AnimationController controller) {
Expand All @@ -54,18 +62,33 @@ class ColorizeAnimatedText extends AnimatedText {
),
);

_colorShifter =
Tween<double>(begin: 0.0, end: colors.length * tuning).animate(
final colorShift = colors.length * tuning;
final colorTween = textDirection == TextDirection.ltr
? Tween<double>(
begin: 0.0,
end: colorShift,
)
: Tween<double>(
begin: colorShift,
end: 0.0,
);
_colorShifter = colorTween.animate(
CurvedAnimation(
parent: controller,
curve: const Interval(0.0, 1.0, curve: Curves.easeIn),
),
);

// With RTL, colors need to be reversed to compensate for colorTween
// counting down instead of up.
_colors = textDirection == TextDirection.ltr
? colors
: colors.reversed.toList(growable: false);
}

@override
Widget completeText() {
final linearGradient = LinearGradient(colors: colors).createShader(
final linearGradient = LinearGradient(colors: _colors).createShader(
Rect.fromLTWH(0.0, 0.0, _colorShifter.value, 0.0),
);
return Text(
Expand Down Expand Up @@ -94,6 +117,7 @@ class ColorizeAnimatedTextKit extends AnimatedTextKit {
Key key,
@required List<String> text,
TextAlign textAlign = TextAlign.start,
TextDirection textDirection = TextDirection.ltr,
TextStyle textStyle,
List<Color> colors,
Duration speed = const Duration(milliseconds: 200),
Expand All @@ -109,8 +133,8 @@ class ColorizeAnimatedTextKit extends AnimatedTextKit {
bool stopPauseOnTap = false,
}) : super(
key: key,
animatedTexts:
_animatedTexts(text, textAlign, textStyle, speed, colors),
animatedTexts: _animatedTexts(
text, textAlign, textStyle, speed, colors, textDirection),
pause: pause,
displayFullTextOnTap: displayFullTextOnTap,
stopPauseOnTap: stopPauseOnTap,
Expand All @@ -129,6 +153,7 @@ class ColorizeAnimatedTextKit extends AnimatedTextKit {
TextStyle textStyle,
Duration speed,
List<Color> colors,
TextDirection textDirection,
) =>
text
.map((_) => ColorizeAnimatedText(
Expand All @@ -137,6 +162,7 @@ class ColorizeAnimatedTextKit extends AnimatedTextKit {
textStyle: textStyle,
speed: speed,
colors: colors,
textDirection: textDirection,
))
.toList();
}

0 comments on commit df9c63a

Please sign in to comment.