removeItem method

void removeItem(
  1. int index,
  2. AnimatedRemovedItemBuilder builder, {
  3. Duration duration = _kDuration,
})
inherited

Remove the item at index and start an animation that will be passed to builder when the item is visible.

Items are removed immediately. After an item has been removed, its index will no longer be passed to the subclass' SliverAnimatedGrid.itemBuilder or SliverAnimatedList.itemBuilder. However the item will still appear for duration, and during that time builder must construct its widget as needed.

This method's semantics are the same as Dart's List.remove method: it decreases the length of items by one and shifts all items at or before index towards the beginning of the list of items.

Implementation

void removeItem(int index, AnimatedRemovedItemBuilder builder, { Duration duration = _kDuration }) {
  assert(index >= 0);

  final int itemIndex = _indexToItemIndex(index);
  assert(itemIndex >= 0 && itemIndex < _itemsCount);
  assert(_activeItemAt(_outgoingItems, itemIndex) == null);

  final _ActiveItem? incomingItem = _removeActiveItemAt(_incomingItems, itemIndex);
  final AnimationController controller =
      incomingItem?.controller ?? AnimationController(duration: duration, value: 1.0, vsync: this);
  final _ActiveItem outgoingItem = _ActiveItem.outgoing(controller, itemIndex, builder);
  setState(() {
    _outgoingItems
      ..add(outgoingItem)
      ..sort();
  });

  controller.reverse().then<void>((void value) {
    _removeActiveItemAt(_outgoingItems, outgoingItem.itemIndex)!.controller!.dispose();

    // Decrement the incoming and outgoing item indices to account
    // for the removal.
    for (final _ActiveItem item in _incomingItems) {
      if (item.itemIndex > outgoingItem.itemIndex) {
        item.itemIndex -= 1;
      }
    }
    for (final _ActiveItem item in _outgoingItems) {
      if (item.itemIndex > outgoingItem.itemIndex) {
        item.itemIndex -= 1;
      }
    }

    setState(() => _itemsCount -= 1);
  });
}