Skip to content

Make Tween<Color>, Tween<TextStyle>, Tween<Size> and so on work instead of having to use ColorTween/TextStyleTween classes #71905

@EriKWDev

Description

@EriKWDev

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".

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projecta: animationAnimation APIsc: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Flutterframeworkflutter/packages/flutter repository. See also f: labels.team-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions