首页 > 解决方案 > TweenSequence 忽略 TweenSequence 项

问题描述

嗨,我想在我的 Flutter 应用程序中不断地淡入淡出图像。为此,我使用 TweenSequence:

AnimationController animationController;
  Animation<double> opacityAnimation;
  double opacity = 0.5;

  @override
  void initState() {
    super.initState();
    animationController =
        AnimationController(vsync: this, duration: Duration(seconds: 3));
    opacityAnimation = TweenSequence(
      [
        TweenSequenceItem<double>(
            tween: Tween<double>(begin: 0.5, end: 1.0).chain(
              CurveTween(curve: Curves.bounceIn),
            ),
            weight: 50),
         TweenSequenceItem<double>(
             tween: Tween<double>(begin: 1.0, end: 0.5).chain(
               CurveTween(curve: Curves.bounceIn),
             ),
             weight: 50),
      ],
    ).animate(animationController);
    animationController.addListener(
      () {
        setState(() {
          opacity = animationController.value;
          print(opacity);
        });
      },
    );
    animationController.repeat();
  }

  @override
  void dispose() {
    animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Stack(
        children: [
          Image.asset("assets/images/buildup_0.png"),
          Opacity(
            child: Image.asset("assets/images/Logo_K.png"),
            opacity: opacity,
          ),
        ],
      ),
    );
  }
}

但是,动画只是忽略了我给它的 Tweens 并在 0 和 1 之间进行动画处理,然后跳回到 0 并重复。由于打印语句,我可以在控制台中看到这一点。我在这里遗漏了一些明显的东西吗?

标签: flutterdartanimationflutter-animation

解决方案


我刚刚找到了解决方案:我需要将其设置为 opacityAnimation.value 而不是将 opacity 设置为 animationController.value


推荐阅读