Use case
I kind of expected Tween<Color> to work out of the box and was taken by surprise when I had to use special Classes for a bunch of types.
I'm creating an application where the user can create animations and I want basically anything to be animatable, so the application has to programmatically generate a bunch of Animations and Tweens for a bunch of different types of user created properties.
Example of some code that this has caused me to write
// Inside a Foo<T> class for my application with lots of user defined animations
// ...
Animatable<T> get tween {
Tween _tween;
if(beginValue is Color) {
_tween = ColorTween(
begin: beginValue as Color,
end: endValue as Color
);
} else if(beginValue is TextStyle) {
_tween = TextStyleTween(
begin: beginValue as TextStyle,
end: endValue as TextStyle
);
// ... and so on
} else {
_tween = Tween<T>(
begin: beginValue,
end: endValue
);
}
return _tween.chain(CurveTween(curve: curve));
}
Proposal
Is there perhaps not a way to make the lerp function take objects like Color, Size and so on into account? Or maybe have the Tween<T> class be abstract and then implement all Tween<double>, (Tween<num>), Tween<'Add-Subtract-AndMultiplicableType'> separatly?
The problem lies in that objects like Color and TextStyle can't simply be subtracted and multiplied by t in the default Tween<T>'s lerp() function which is why all these separate classes have been created.
// Implementation of Tween in tween.dart
class Tween<T extends dynamic> extends Animatable<T> {
// ...
@protected
T lerp(double t) {
assert(begin != null);
assert(end != null);
return begin + (end - begin) * t as T;
}
}
Different approaches are of course welcome since I'm not familiar with how to implement these kind of things in such a big project like Flutter xP Maybe it would be possible to have a Tween<T extends dynamic which 'has support for subtraction, addition and multiplication'> so that only Tween<Color> and these special cases have to be implemented "manually".
Use case
I kind of expected
Tween<Color>to work out of the box and was taken by surprise when I had to use special Classes for a bunch of types.I'm creating an application where the user can create animations and I want basically anything to be animatable, so the application has to programmatically generate a bunch of Animations and Tweens for a bunch of different types of user created properties.
Example of some code that this has caused me to write
Proposal
Is there perhaps not a way to make the lerp function take objects like Color, Size and so on into account? Or maybe have the
Tween<T>class be abstract and then implement allTween<double>, (Tween<num>),Tween<'Add-Subtract-AndMultiplicableType'>separatly?The problem lies in that objects like Color and TextStyle can't simply be subtracted and multiplied by t in the default
Tween<T>'slerp()function which is why all these separate classes have been created.Different approaches are of course welcome since I'm not familiar with how to implement these kind of things in such a big project like Flutter xP Maybe it would be possible to have a
Tween<T extends dynamic which 'has support for subtraction, addition and multiplication'>so that onlyTween<Color>and these special cases have to be implemented "manually".