Flutter:设置 AnimatedList 动画持续时间

Jak*_*sMD 3 flutter flutter-animation

有没有办法在 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));
        });
Run Code Online (Sandbox Code Playgroud)

Jos*_*eve 8

所以......很短的研究之后,我意识到,你才可以设置时间,当你要插入到列表或从列表中删除,这是通过创建一个实现GlobalKeyAnimatedListState ...

我写了一个用于插入的示例代码

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));
        },
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

输出:

在此处输入图片说明