Flutter: установить продолжительность анимации AnimatedList - PullRequest
0 голосов
/ 21 марта 2020

Есть ли способ установить продолжительность анимации в 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));
        });

1 Ответ

2 голосов
/ 21 марта 2020

Итак ... после короткого исследования я понял, что вы можете установить Длительность только тогда, когда вы хотите вставить в список или удалить из списка, и это достигается путем создания 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

...