Skip to content

Commit 54a63f1

Browse files
committed
add ColorInterpolationTween
1 parent cd31ad0 commit 54a63f1

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

example/lib/parallel_animation_logo.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class AnimatedLogo1 extends AnimatedWidget {
44
// The Tweens are static because they don't change.
55
static final _opacityTween = new Tween<double>(begin: 0.1, end: 1.0);
66
static final _sizeTween = new InterpolationTween(inputRange: [0,0.2,1], outputRange: [0,250,300]);
7+
static final _colorTween = ColorInterpolation(inputRange: [0,0.2,1],outputRange: [Colors.white,Colors.green,Colors.red]);
78

89
AnimatedLogo1({Key key, Animation<double> animation})
910
: super(key: key, listenable: animation);
@@ -18,6 +19,7 @@ class AnimatedLogo1 extends AnimatedWidget {
1819
height: _sizeTween.evaluate(animation),
1920
width: _sizeTween.evaluate(animation),
2021
child: new FlutterLogo(),
22+
color: _colorTween.evaluate(animation),
2123
),
2224
),
2325
);

lib/animated_interpolation.dart

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,65 @@ class InterpolationTween extends Animatable {
3434
extrapolateRight: extrapolateRight))(t);
3535
}
3636

37+
class ColorInterpolationTween extends Animatable<Color> {
38+
ColorInterpolationTween({
39+
@required this.inputRange,
40+
@required this.outputRange,
41+
this.curve = const _Linear._(),
42+
this.extrapolate,
43+
this.extrapolateLeft = ExtrapolateType.extend,
44+
this.extrapolateRight = ExtrapolateType.extend,
45+
});
46+
47+
final List<double> inputRange;
48+
final List<Color> outputRange;
49+
final Curve curve;
50+
final ExtrapolateType extrapolate;
51+
final ExtrapolateType extrapolateLeft;
52+
final ExtrapolateType extrapolateRight;
53+
@override
54+
Color transform(double t) {
55+
// TODO: implement transform
56+
return Color.fromARGB(
57+
createInterpolation(InterpolationConfigType(
58+
inputRange: inputRange,
59+
outputRange: outputRange.map((color) => color.alpha.toDouble()).toList(),
60+
curve: curve,
61+
extrapolate: extrapolate,
62+
extrapolateLeft: extrapolateLeft,
63+
extrapolateRight: extrapolateRight))(t)
64+
.toInt()
65+
.clamp(0, 255),
66+
createInterpolation(InterpolationConfigType(
67+
inputRange: inputRange,
68+
outputRange: outputRange.map((color) => color.red.toDouble()).toList(),
69+
curve: curve,
70+
extrapolate: extrapolate,
71+
extrapolateLeft: extrapolateLeft,
72+
extrapolateRight: extrapolateRight))(t)
73+
.toInt()
74+
.clamp(0, 255),
75+
createInterpolation(InterpolationConfigType(
76+
inputRange: inputRange,
77+
outputRange: outputRange.map((color) => color.green.toDouble()).toList(),
78+
curve: curve,
79+
extrapolate: extrapolate,
80+
extrapolateLeft: extrapolateLeft,
81+
extrapolateRight: extrapolateRight))(t)
82+
.toInt()
83+
.clamp(0, 255),
84+
createInterpolation(InterpolationConfigType(
85+
inputRange: inputRange,
86+
outputRange: outputRange.map((color) => color.blue.toDouble()).toList(),
87+
curve: curve,
88+
extrapolate: extrapolate,
89+
extrapolateLeft: extrapolateLeft,
90+
extrapolateRight: extrapolateRight))(t)
91+
.toInt()
92+
.clamp(0, 255),
93+
);
94+
}
95+
}
3796

3897
enum ExtrapolateType {
3998
extend,

0 commit comments

Comments
 (0)