如何在Flutter中旋转一个小部件?

问题描述 投票:1回答:1

有什么方法可以让widget旋转起来?我试过 RotatedBox 但它没有工作。

flutter dart flutter-animation
1个回答
1
投票

使用 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),
        ),
      ),
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.