簡體   English   中英

如何旋轉 Flutter 中的小部件?

[英]How to rotate a widget in Flutter?

有沒有辦法為小部件的旋轉設置動畫? 我嘗試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,
    );
  },
)

截屏:

在此處輸入圖像描述


完整代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM