Flutter:在ListView中动画移除项目

时间:2018-08-18 14:45:51

标签: listview dart flutter

我正在从Stream构建ListView。我需要为该列表中的删除和插入设置动画,但是不知道如何。

我看过Flutter的这个示例,但它与流没有任何关系:https://flutter.io/catalog/samples/animated-list/

任何帮助都非常感激:)

new StreamBuilder(

    stream: feed.stream, // this is a Stream<List<Product>>

    builder: (context, snapshot) {
      if (!snapshot.hasData)
        return const Text('Loading products');
      return new ListView.builder(
          itemCount: snapshot.data.length,
          itemBuilder: (context, index) {
            Product product = snapshot.data[index];
            return new ProductWidget(product);
          });
    });

1 个答案:

答案 0 :(得分:2)

这不是使用Streams,而是作为AnimatedList的一般答案,您可以执行以下操作:

enter image description here

// Remove "Pig" from the list
int removeIndex = 2;

// remove the item from the data list backing the AnimatedList
String removedItem = _data.removeAt(removeIndex);

// This builder is just so that the animation has something
// to work with before it disappears from view since the original
// has already been deleted.
AnimatedListRemovedItemBuilder builder = (context, animation) {
  // A method to build the Card widget.
  return _buildItem(removedItem, animation);
};

// notify the AnimatedList that the item was removed
_listKey.currentState.removeItem(removeIndex, builder);