Flutter:设置AnimatedList动画持续时间

问题描述 投票:0回答:1

是否可以在AnimatedList中设置动画的持续时间?

  AnimatedList(
        key: _animList,
        initialItemCount: _myList.length,
        itemBuilder: (context, index, animation) {
          // duration = Duration(seconds: 1); <--- ????????
          return SlideTransition(
              position: animation.drive(
                  Tween(begin: Offset(1, 0), end: Offset(0, 0))
                      .chain(CurveTween(curve: Curves.bounceIn))),
              child: Container(color: Colors.red, height: 100, width: 100));
        });
flutter flutter-animation
1个回答
1
投票

所以...经过简短的研究,我意识到只有在要插入列表或从列表中删除时,才可以设置Duration,这是通过创建<< GlobalKey来实现的。 [AnimatedListState ...

我写了一个用于插入的示例代码class Pool extends StatelessWidget { final keys = GlobalKey<AnimatedListState>(); var list = List.generate(3, (i) => "Hello $i"); @override Widget build(BuildContext context) { return Scaffold( body: SafeArea( child: AnimatedList( key: keys, initialItemCount: list.length, itemBuilder: (context, index, animation) { return SlideTransition( position: animation.drive( Tween<Offset>(begin: Offset(1, 0), end: Offset(0, 0)) .chain(CurveTween(curve: Curves.ease))), child: ListTile( title: Text(list[index]), ), ); }, ), ), floatingActionButton: FloatingActionButton( onPressed: () { list.insert(0, "NothingYay"); keys.currentState.insertItem(0, duration: Duration(seconds: 2)); }, ), ); } }

输出:

enter image description here
© www.soinside.com 2019 - 2024. All rights reserved.