【自学Flutter】 交错动画的使用
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
AnimationController _controller;
Animation<double> _animation;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(seconds: 3),
vsync: this,
)..repeat(reverse: true);
_animation = Tween<double>(begin: 0, end: 300).animate(_controller)
..addListener(() => setState(() {}));
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('交错动画示例')),
body: Center(
child: Container(
margin: EdgeInsets.symmetric(vertical: 10),
height: _animation.value,
width: _animation.value,
child: FlutterLogo(),
),
),
);
}
}
这段代码创建了一个简单的交错动画示例,其中一个Flutter徽标会在一个3秒钟的动画周期内来回交错放大缩小。代码使用了AnimationController
和Tween
来定义动画曲线,并通过addListener
在动画每一帧更新时重绘UI。
评论已关闭