我正在尝试使用动画列表小部件,我想要实现的是通过在此动画列表中按下我的磁贴小部件上的按钮,磁贴将从我的数据中删除并从动画列表中删除,但是当按下按钮时唯一最后一项中的最后一项将被删除,而不是给定索引处的一项
首先我为列表使用了一个全局键
final GlobalKey<AnimatedListState> listKey = GlobalKey<AnimatedListState>();
我的动画列表代码
AnimatedList(
initialItemCount: snapshot.data[0].length ,
key: listKey,
itemBuilder: (BuildContext context, int index, Animation animation) {
return SizeTransition(
sizeFactor: animation,
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ProductFullScreenView(
productInfo: snapshot.data[0].elementAt(index),
)));
},
child: ProductListCardVertical(
index: index,
),
));
});
在我的小部件中,我调用了这个函数
// function to remove the data from my database, if true then remove the tile
// remove tile
listKey.currentState.removeItem(
widget.index, (context, animation) => Container());
任何帮助,将不胜感激
最佳答案
更新:
发生这个问题是因为我没有从列表中删除数据,所以我所做的是我创建了一个全局数据列表并将我从数据库中获取的列表分配给该值,然后我删除了该项目的索引从磁贴小部件内的列表中。
关于flutter - 动画列表只删除最后一个项目而不是给定索引处的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62005100/