Search code examples
flutteranimationflutter-getx

How to create animation controller on demand


Is it possible to write a function to create an animation controller? I have dynamic data, if my data return 3, I will need to create 3 animation controller.

Edit: Thank you pskink for the List.generate idea. If I wanted to trigger manually using the below function (List<AnimationController> animationList)

createAnimationController(int sec, String id) {
animationList
    .add(AnimationController(vsync: this, duration: Duration(seconds: sec))
      ..addListener(() {
        print('hello');
        update([id]);
      }));
return animationList.last; }

I notice that my listener does not fire, not sure why.


Solution

  • Answer from pskink

    Working code that return newly generate AnimationController:

        List<AnimationController> animationList;
        createAnimationController(int sec, String id) {
        animationList
            .add(AnimationController(vsync: this, duration: Duration(seconds: sec))..forward()
              ..addListener(() {
                update([id]);
              }));
        return animationList.last; 
    }