Why is Animation Tween always running from 0.0 to 1.0?

Issue

I want to make an animation in flutter using

  late AnimationController controller1;
  late Animation<double> animation1;

  @override
  void initState() {
   super.initState();

   controller1 = AnimationController(
      duration: const Duration(milliseconds: 700),
      vsync: this,
    );
    animation1 = Tween(begin: 50.0, end: 0.0).animate(controller1)
      ..addListener(() {
        setState(() {});
      });

    controller1.forward();
}

No matter which values I put in as "begin" and "end" – the

controller1.value

always goes from 0.0 to 1.0. What is my mistake here?

Thanks!

Solution

I think you want to use the animation1.value, not the controller1.value and you can verify it using the following code 🙂 :

 @override
 void initState() {
  super.initState();
  controller =
      AnimationController(duration: const Duration(seconds: 2), vsync: this);

  animation = Tween<double>(begin: 50.0, end: 0.0).animate(controller)
    ..addListener(() {
      // #enddocregion addListener
      setState(() {
        print("Animation value: ${animation.value}");

        //TODO: you can uncomment this print to see the value of the controller
        //print("Controller value: ${controller.value}");
      });
    });
  controller.forward();
}

Console logs

Check out the official tutorial for more info 🙂

Animations tutorial

By the way, since controller.value goes from 0.0 to 1.0, you can consider it as a sort of completion percentage 🙂

Answered By – Mirko Raimo

Answer Checked By – Mildred Charles (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *