首页 > 解决方案 > 在构建 AnimatedList 时,“insertItem”是如何工作的?

问题描述

特别是,我不明白该insertItem函数如何知道下面摘录中要添加的项目_animatedList,因为它的唯一参数是索引位置。

这里发生了什么?

  final GlobalKey<AnimatedListState> listKey;
  final dynamic removedItemBuilder;
  final List<E> _items;

  AnimatedListState get _animatedList => listKey.currentState;

  void insert(int index, E item) {
    _items.insert(index, item);
    _animatedList.insertItem(index);
  }

以上摘自以下来源:https ://api.flutter.dev/flutter/widgets/AnimatedList-class.html

标签: flutteranimation

解决方案


两者都insertItem只是removeItem提示在AnimatedList设置动画index。例如在上面的代码中,如果您删除该行

_animatedList.insertItem(index);

您的项目将被添加到_items列表中,但列表中没有任何动画(没有 UI 更改),即动画对添加到列表中的项目一无所知,它只知道在哪个index动画


推荐阅读