Flutter 中的 RotationTransition 小部件:全面指南
    		       		warning:
    		            这篇文章距离上次修改已过444天,其中的内容可能已经有所变动。
    		        
        		                
                RotationTransition 是 Flutter 中用于创建旋转过渡动画的小部件。以下是如何使用 RotationTransition 创建一个简单旋转动画的示例代码:
import 'package:flutter/material.dart';
 
class RotationExample extends StatefulWidget {
  @override
  _RotationExampleState createState() => _RotationExampleState();
}
 
class _RotationExampleState extends State<RotationExample> with TickerProviderStateMixin {
  AnimationController _controller;
  CurvedAnimation _curvedAnimation;
 
  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(seconds: 2),
      vsync: this,
    )..repeat(); // 使动画无限重复
    _curvedAnimation = CurvedAnimation(
      parent: _controller,
      curve: Curves.easeInOut,
    );
  }
 
  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
 
  @override
  Widget build(BuildContext context) {
    return Center(
      child: RotationTransition(
        turns: _curvedAnimation,
        child: Icon(Icons.refresh),
      ),
    );
  }
}
 
void main() {
  runApp(MaterialApp(home: RotationExample()));
}这段代码创建了一个无限重复的旋转动画,其中 RotationTransition 小部件以圆形的运动轨迹旋转其子部件(一个 Icon)。AnimationController 控制旋转的周期,CurvedAnimation 使旋转速度变化,从而产生一个平滑的旋转动画效果。
评论已关闭