Sait Banazili
Sait Banazili

Reputation: 1353

Are there generic types of AnimationController? If not, why?

Flutter newbie here. I am using Tween<Offset> with an AnimationController. And I'm trying to use animateTo() method with a target of type Offset. Is this possible?

Document says:

animateTo(double target, { Duration duration, Curve curve: Curves.linear }) → TickerFuture

Drives the animation from its current value to target

AnimationController:

_controller = AnimationController(
  duration: const Duration(milliseconds: 500),
  vsync: this,
);

AnimateTo():

_controller.animateTo(Offset(0,0))

The above line is not correct:

The argument type 'Offset' can't be assigned to the parameter type 'double'.dart(argument_type_not_assignable)

Are there generic types of AnimationController? If not, why? I know I can use Tween<Offset>(begin:.., end:..).animate(_controller). But looks like animateTo() and similar AnimationController methods are just for a target of type double. This looks confusing.

Upvotes: 1

Views: 299

Answers (2)

R&#233;mi Rousselet
R&#233;mi Rousselet

Reputation: 277077

No. AnimationController only handle double.

The double used by AnimationController have a different meaning from the double used by Tween.

AnimationController is only about the animation progress, while Tween may output one value after many layers of transformations.

As such, not only it is very difficult to convert a transformed value back to the progress it represents, it also suffers from limitations:

Curves. If an AnimationController should handle any tweenable object, then it would implicitly support curves too.

The thing is, we can have such curve:

enter image description here

But this raises an issue. As we can see in the previous gif, after transformed by a curve, we can have multiple time the same value emitted.

In that case, what would be the desired behavior for an animateTo(X) if a controller can emit X multiple times?

This wouldn't make sense. As such, AnimationController works with nothing but a linear floating value. If we want more complicated behaviors, we have to use different objects.

Upvotes: 1

Mertus
Mertus

Reputation: 1195

What you need is this,

var _controller = AnimationController(vsync: this, duration: Duration(seconds: 2));

var _animation = Tween(begin: Offset(0,0), end: Offset(5,5));

_controller.animateTo(0.5);

The Tween class is an Animatable which produces interpolated values for your animation. the animateTo method gets a value between 0 and 1 (although you can define these bounds yourself in the constructor of AnimationController default values are 0 and 1). For example if you give 0.5 to animateTo method you will animate right to the half way between your offset values.

Upvotes: 1

Related Questions