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);
});
});
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.
Full example here.
As a general answer for an AnimatedList, you can do the following:
// 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With