Search code examples
flutterdartanimationflutter-animation

Flutter play animation once programmatically


I would like to trigger a animate programmatically but can not make it work. This is what I tried for my basic scale-animation:

Text(
    _getItemTitle(option),
    style: _wishState == option
        ? AppTextStyles.avenirNextH5SemiBold
        : AppTextStyles.avenirNextH5Regular,
  )
      .animate(
        onPlay: (controller) => _animationController.repeat(reverse: true),
      )
      .scale(
        end: const Offset(1.2, 1.2),
      ),

The animation should play once, including a reverse-animation. I tried adding a controller but how don't know how I can make that work. I would like to trigger it with a Provider I already setup.


Solution

  • You could try this instead:

    onPlay: (controller) => _animationController.loop(count:2, reverse: true),
    

    Response to question in comment:

    Use the target property in the animate() method to react to state changes, such as from your provider. See the docs: https://pub.dev/packages/flutter_animate#reacting-to-state-changes