gpt4 book ai didi

flutter - 如何在Flutter中旋转小部件?

转载 作者:行者123 更新时间:2023-12-03 02:55:19 31 4
gpt4 key购买 nike

有什么方法可以动画化小部件的旋转吗?我尝试了RotatedBox,但没有成功。

最佳答案

使用AnimatedBuilder

AnimatedBuilder(
animation: _animation, // pass AnimationController to it
child: YourContainer(),
builder: (_, child) {
return Transform.rotate(
angle: _animation.value * play_around_with_values,
child: child,
);
},
)

屏幕截图:

enter image description here

完整代码:
class _MainPageState extends State<MainPage> with SingleTickerProviderStateMixin {
AnimationController _controller;

@override
void initState() {
super.initState();

_controller = AnimationController(vsync: this, duration: Duration(seconds: 2))..repeat();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: AnimatedBuilder(
animation: _controller,
builder: (_, child) {
return Transform.rotate(
angle: _controller.value * 2 * math.pi,
child: child,
);
},
child: FlutterLogo(size: 200),
),
),
);
}
}

关于flutter - 如何在Flutter中旋转小部件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61656764/

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