1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| //入口位置 Future<void> main() async { runApp(Home()); } class Home extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold(body: Center(child: AnimationDemo(),),), ); } } //自定义动画 class AnimationDemo extends StatefulWidget { @override State<StatefulWidget> createState() => _AnimationDemo(); }
class _AnimationDemo extends State<AnimationDemo> with SingleTickerProviderStateMixin { AnimationController _animationController; Animation _animation;
@override void initState() { super.initState(); _animationController = AnimationController(duration: Duration(seconds: 2), vsync: this); _animation = Tween(begin: .5, end: .1).animate(_animationController); _animationController.forward(); //开始动画 }
@override Widget build(BuildContext context) { return ScaleTransition( scale: _animation, child: Container(height: 200, width: 200, color: Colors.red,), ); }
@override void dispose() { _animationController.dispose(); super.dispose(); } }
|