补间动画颤动

Sam*_*324 3 dart flutter

我想在单击按钮时播放动画。第一次按下时,小部件旋转 180 度,第二次按下时,又旋转 180 度(即返回到其原始位置)。我怎样才能做到这一点?

模拟手势检测按钮

                      Expanded(
                        child: GestureDetector(
                          onTap: () => setState(() {
                            if (tapValue == 0) {
                              tapValue++;
                              animController.forward();
                              beginValue = 0.0;
                              endValue = 0.5;
                            } else {
                              tapValue--;
                              animController.forward();
                            }
                          }),
                          child: Container(
                            child: Image.asset('assets/images/enableAsset.png'),
                          ),
                        ),
                      ),
Run Code Online (Sandbox Code Playgroud)

我想要旋转的小部件

            child: CustomPaint (
              painter: SmileyPainter(),
              child: RotationTransition(
                turns: Tween(begin: beginValue, end: endValue,).animate(animController),
                child: CustomPaint (
                  painter: Smile(),
                ),
              ),
            )
Run Code Online (Sandbox Code Playgroud)

动画控制器

  @override
  void initState() {
    animController = AnimationController(
      duration: const Duration(milliseconds: 400),
      vsync: this,
    );
    super.initState();
  }
Run Code Online (Sandbox Code Playgroud)

Lul*_*ntu 5

如果您想要实现的只是旋转小部件,我建议避免使用控制器。这不仅会简化您的代码,而且还会节省您处理代码的麻烦。

我开始意识到,使用小部件几乎可以避免任何控制器TweenAnimationBuilder

以下是如何使其适合您的案例的示例:

Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _rotationAngle += pi;
          print(_rotationAngle);
          setState(() {

          });
        },
      ),
      body: Center(
        child: TweenAnimationBuilder(
          duration: Duration(milliseconds: 300),
          tween: Tween<double>(begin: 0, end: _rotationAngle),
          builder: (BuildContext context, double value, Widget child) {
            return Transform.rotate(
              angle: value,
              child: child,
            );
          },
          child: Container(
            height: 500,
            width: 50,
            color: Colors.redAccent,
          ),
        ),
      ),
    );
Run Code Online (Sandbox Code Playgroud)