AnimatedList is a stateful widget, and its corresponding state type is AnimatedListState.
The structure of AnimatedList is as follows:
There are two methods in the state to add and remove elements in the list:
Note that if the list is bound to a data source list, you cannot directly achieve the effect of updating the list by removing or adding elements. You also need to execute the following methods:
When adding or removing, the specified animation will be displayed.
When using insertItem and removeItem, you usually need to use a globalKey, but the globalKey needs to specify the generic type in order to call the methods to add and remove list elements:
To add an element, you only need to:
Removing an element is more complicated because you need to display the animation when the element leaves and then delete the corresponding entry in the data source:
Usually, we need to create a
_buildItemmethod to dynamically create child elements. When executingremoveItem, first create an identical child element in the callback, fade it out using animation, and finally delete the data in the data array.