【问题标题】:How to rotate a widget in Flutter?如何在 Flutter 中旋转小部件?
【发布时间】:2020-08-22 16:31:44
【问题描述】:

有什么方法可以为小部件的旋转设置动画?我试过RotatedBox,但没用。

【问题讨论】:

    标签: flutter dart flutter-animation


    【解决方案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,
        );
      },
    )
    

    截图:


    完整代码:

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

    【讨论】:

      猜你喜欢
      • 2018-12-12
      • 1970-01-01
      • 2014-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-31
      • 2017-10-31
      • 1970-01-01
      相关资源
      最近更新 更多