Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: animate item removal in ListView

I am building a ListView from a Stream. I need to animate deletions and insertions to that list, but have no idea how.

I have seen this sample by Flutter but it is not related to streams in any way: https://flutter.io/catalog/samples/animated-list/

Any help greatly appreciated :)

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);
          });
    });
like image 650
gatti Avatar asked Aug 18 '18 14:08

gatti


People also ask

How do you use animated list in flutter?

AnimatedList is a built-in widget in Flutter, used to implement a list view that animates its items when they are inserted or removed. This helps the user feel less sudden and more pleasant about the change of the list. In this article, you will learn how to use AnimatedList through a couple of complete examples.


1 Answers

Full example here.

As a general answer for an AnimatedList, you can do the following:

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);
like image 54
Suragch Avatar answered Oct 05 '22 17:10

Suragch