我有一个这样的SliverAnimatedList:
SliverAnimatedList(
key: _myListkey,
itemBuilder: (context, index, animation) {
return Container(
child: Column(
children: [
FlashcardCreateTile(
autocreate: autocreate,
entertomovefocus: entertomovefocus,
flashcard: flashcards[index],
islast:
(index + 1) == flashcards.length ? true : false,
plusmode: true,
promode: true,
uid: widget.uid,
focus: null,
animation: animation,
formKey: _formkey,
delete: () {
flashcards.removeAt(index);
SliverAnimatedList.of(context).removeItem(
index,
(context, animation) => FlashcardCreateTile(
autocreate: autocreate,
entertomovefocus: entertomovefocus,
flashcard:
Flashcard(recto: "", verso: ""),
islast: false,
plusmode: true,
promode: true,
uid: widget.uid,
focus: null,
animation: animation,
formKey: _formkey,
delete: () {},
add: () {}),
duration: const Duration(milliseconds: 100));
},
add: () {
int insertitem = index + 1;
print(insertitem);
setState(() {
flashcards.insert(
insertitem,
Flashcard(
recto: "",
verso: "",
mode: 0,
isrelearning: false,
easefactor: widget
.folder
.decklist[widget.deckindex]
.startingEase,
currentInterval:
Duration(microseconds: 0),
previousInterval:
Duration(microseconds: 0)));
SliverAnimatedList.of(context)
.insertItem(insertitem);
SliverAnimatedList.of(context).build(context);
});
},
),
Container(
child: (index + 1) == flashcards.length
? Container(
child: SizedBox(
height: 50,
),
)
: Container(),
)
],
),
);
},
initialItemCount: flashcards.length,
)当我单击和一个按钮时,flashcardcreatetile将返回add函数:
IconButton(
icon: Icon(
Icons.add,
color: Colors.red,
),
onPressed: widget.add)下面是它正在做的事情:

正如您所看到的,该项目确实已插入,但只有当我向下滚动和向上滚动时,sliveranimatedlist才会显示它,因此我认为它需要重新构建自身。
我想让新卡直接显示出来,有什么想法吗?顺便说一句,删除项目工作正常
发布于 2020-11-08 08:20:18
您需要向项目列表中添加一个键。我建议您阅读this article,因为您将了解为什么需要密钥,它们有什么好处,以及如何解决您的问题。
发布于 2020-11-08 06:49:47
您应该向FlashcardCreateTile项目添加一个key: Key(index),以使其具有唯一性。
Flutter引擎需要它来正确地建立你的列表
https://stackoverflow.com/questions/64733137
复制相似问题