dart 如何一次性设置SlideTransition动画?

shyt4zoc  于 2023-03-27  发布在  其他
关注(0)|答案(1)|浏览(120)

如何在Flutter中一次性设置SlideTransition的动画?
这段代码将SlideTransitionAnimationController动画化,但会无限重复。如何播放并停止一次?

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),
      ),);}
}
wbgh16ku

wbgh16ku1#

只需删除您在控制器上调用方法:

..repeat(reverse: true)

相关问题