Flutter:如何检测动画是否已暂停/停止

yiytaume  于 6个月前  发布在  Flutter
关注(0)|答案(3)|浏览(117)

这可能听起来很简单,但我指的是在调用stop()之后检查它。例如:

if (timerControllerList[index].isAnimating) {
   await timerControllerList[index].stop();
}

if (timerControllerList[index].status == AnimationStatus.forward) {
  // it goes in  :(
}

字符串
我有两个定时器动画(这就是为什么我有一个List),我可以在达到10秒之前独立暂停,我需要检测暂停其中任何一个时,另一个也停止了。在这种情况下,它们都有一个状态forward
初始化:

void initTimer(int index) {
// called from initState()

    late AnimationController timerController;
    late Animation<int> timerAnimation;
    int timerCounter = 10;

    timerController = AnimationController(
      vsync: this,
      duration: Duration(seconds: timerCounter),
    );

    timerAnimation =
        StepTween(begin: 0, end: timerCounter * 1000).animate(timerController)
          ..addStatusListener((status) {
            if (status == AnimationStatus.completed) {
              stopTimer(index);
            }
          });

    timerControllerList.add(timerController);
    timerAnimationList.add(timerAnimation);
  }

kadbb459

kadbb4591#

这就是我如何在启动画面上检查动画的状态。

_animation = Tween<double>(begin: 0, end: 1).animate(_controller)
      ..addStatusListener(
        (status) {
          if (status == AnimationStatus.completed) {
            _controller.reverse().then(
                  (value) => Navigator.pushAndRemoveUntil(
                      context,
                      MaterialPageRoute(
                        builder: (context) => const FirebaseAuthCheck(),
                      ),
                      (route) => false),
                );
          }
        },
      );

字符串

c6ubokkw

c6ubokkw2#

你可以使用监听器来帮助你。这将检查它是否被停止:

_animation.addStatusListener((status) {
    if (status == AnimationStatus.dismissed) {
        // stopped
    }

字符串
或者检查它是否被暂停,类似这样:

_controller.addListener(() {
if (_controller.isAnimating == false) {
    // paused
}


甚至是:

_animation.addStatusListener((status) {
    if (status == AnimationStatus.forward || 
        status == AnimationStatus.reverse) {
        // running
    } else {
        // paused
    }
});

xiozqbni

xiozqbni3#

我的问题来自于另一个CMAC动画:

void stopTimer(int index) async {
    if (countDownController.isAnimating || !playingGame) return;

    // stopping timer
    if (timerControllerList[index].isAnimating) {
      timerControllerList[index].stop();
    }

    // timer finished
    blackBGControllerList[index].reset();
    await finalMsgControllerList[index].forward();
    await calculateFinalMsg(index);
    if (players == 1) {
      playAgainButtonController.forward();
      playingGame = false;
      saveBestScore(index);
    } else {
      bool someoneCounting =
          timerControllerList.any((controller) => controller.isAnimating);
      if (someoneCounting) return;
      playingGame = false;
      findWinner();
    }
  }

字符串
timerControllerList[index].stop();中,我停止了第一个动画,但await finalMsgControllerList[index].forward()正在启动另一个动画。
因此,如果我在完成第二个动画之前点击,它就会产生问题。不知道为什么,因为停止/检查之前正在运行,但删除await解决了我的问题。

相关问题