如何在Flutter中一次性设置SlideTransition
的动画?
这段代码将SlideTransition
和AnimationController
动画化,但会无限重复。如何播放并停止一次?
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(appBar: AppBar(title: Text('Flutter Code Sample')),
body: Center(child: MyStatefulWidget()),),);}
}
class MyStatefulWidget extends StatefulWidget {
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget>
with SingleTickerProviderStateMixin {
late final AnimationController _controller = AnimationController(
duration: Duration(milliseconds: 100),vsync: this,)..repeat(reverse: true);
late final Animation<Offset> _offsetAnimation = Tween<Offset>(
begin: Offset.zero,end: Offset(1.0, 0.0),).animate(CurvedAnimation(parent: _controller,curve: Curves.linear,));
@override
void dispose() {
_controller.dispose();
_controller.value = 0.5;
super.dispose();
}
@override
Widget build(BuildContext context) {
return SlideTransition(position: _offsetAnimation,transformHitTests: false,child: Padding(padding: EdgeInsets.all(0.0),child: Icon(Icons.add),
),);}
}
1条答案
按热度按时间wbgh16ku1#
只需删除您在控制器上调用方法: