Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to 'forward ' the flutter animation

I have this code which uses an AnimationController and Tween to rotate a container along Z-axis when Start/Stop Button is pressed. The problem is it forwards the animation only first time after that no matter how many times I press the button it does nothing and the container remains stationary .

curvesanimation.dart which has all the animation logic

import 'package:flutter/material.dart';
import 'dart:math' as math;


class CurvedAnimationExample extends StatefulWidget {
  CurvedAnimationExample({Key key}) : super(key: key);
  @override
  CurvedAnimationExampleState createState() => CurvedAnimationExampleState();
}

class CurvedAnimationExampleState extends State<CurvedAnimationExample>
    with SingleTickerProviderStateMixin {
  Animation<double> _animation;
  AnimationController _animationController;

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

    _animationController =
        AnimationController(vsync: this, duration: Duration(seconds: 5))
          ..addListener(() {
            setState(() {});
          });

    final Animation curve =
        CurvedAnimation(parent: _animationController, curve: Curves.easeIn);

    _animation = Tween<double>(begin: 0, end: math.pi * 2).animate(curve);
  }

  void startStopAnimation() {                      
    print(_animationController.isAnimating.toString());
    if (_animationController.isAnimating)
      _animationController.stop();
    else {
      _animationController.forward();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Center(
        child: Column(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        Transform(
          alignment: Alignment.center,
          transform: Matrix4.identity()..rotateZ(_animation.value),
          child: Container(
            color: Colors.pinkAccent,
            width: 200,
            height: 200,
          ),
        ),
        RaisedButton(
          child: const Text("Start/Stop Animation"),
          elevation: 15,
          color: Colors.blueGrey,
          onPressed: () => startStopAnimation(),
        )
      ],
    ));
  }

  @override
  void dispose() {
    _animationController.dispose();
    print("Curved Animation Example : dispose is called");
    super.dispose();
  }
}

main.dart file

import 'package:flutter/material.dart';
import 'curvesanimations.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomeScaffold(
        title: "Curved Animation Example",
      ),
    );
  }
}

class MyHomeScaffold extends StatelessWidget {
  final String title;
  MyHomeScaffold({this.title});
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        appBar: AppBar(
          title: Text(title),
        ),
        body: CurvedAnimationExample(),
      ),
    );
  }
}

like image 221
Anurag Tripathi Avatar asked Dec 19 '25 10:12

Anurag Tripathi


1 Answers

This is because your animation has already reached its endpoint. You can't keep going forward once the end point is reached. An easy fix is to check if the animation is at the endpoint already and reset it to the beginning if it is:

void startStopAnimation() {                      
    print(_animationController.isAnimating.toString());
    if (_animationController.isAnimating)
      _animationController.stop();
    else {
      print("forward");
      if(_animationController.isCompleted) {//Check if animation is at endpoint
        _animationController.value = 0;
      }
      _animationController.forward();
    }
  }
like image 108
Christopher Moore Avatar answered Dec 21 '25 00:12

Christopher Moore