import 'package:flutter/material.dart';
class TestPage extends StatefulWidget {
_TestPageState createState() => new _TestPageState();
}
class _TestPageState extends State<TestPage> with TickerProviderStateMixin {
AnimationController controller;
Animation<double> curve;
Animation topAnimation;
double _top = 0.0;
int count = 0;
@override
void initState() {
super.initState();
}
void startAnimation(begin, end) {
controller = AnimationController(
duration: const Duration(milliseconds: 600), vsync: this);
curve = CurvedAnimation(parent: controller,curve: Curves.easeOut);
topAnimation = Tween(begin: begin, end: end).animate(curve)
..addListener((){
// print(topAnimation.value);
// 一定要触发渲染不然最后就闪一闪到了终点
setState(() {
_top = topAnimation.value;
});
})
..addStatusListener((status) {
if(status == AnimationStatus.completed) {
// 结束时操作
}
});
controller.forward();
}
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
Positioned(
top: _top,
child: FlutterLogo(),
),
Positioned(
bottom: 0,
child: Container(
width: 100,
height: 100,
child: RaisedButton(
child: Text('点我'),
onPressed: (){
count++;
setState(() {
count++;
});
startAnimation(_top, count*100.1);
},
),
),
)
],
)
);
}
dispose() {
controller.dispose();
super.dispose();
}
}
【flutter动画坑】一个组件多次不同的动画
最新推荐文章于 2025-03-29 22:57:07 发布