gpt4 book ai didi

flutter - 补间动画 flutter

转载 作者:行者123 更新时间:2023-12-03 04:35:05 25 4
gpt4 key购买 nike

我想在单击按钮时播放动画。第一次按下时,小部件旋转 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'),
),
),
),

我要旋转的小部件

            child: CustomPaint (
painter: SmileyPainter(),
child: RotationTransition(
turns: Tween(begin: beginValue, end: endValue,).animate(animController),
child: CustomPaint (
painter: Smile(),
),
),
)

动画 Controller

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

最佳答案

如果您想要实现的只是旋转一个小部件,我建议您避免使用 Controller 。这不仅会简化您的代码,还会为您省去处理代码的麻烦。

我开始意识到使用 TweenAnimationBuilder 小部件几乎可以避免任何 Controller 。

这是一个如何让它适用于您的案例的示例:

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,
),
),
),
);

关于flutter - 补间动画 flutter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64305420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com