[英]Flutter - How to add items on the top of a Listview builder?
我的應用上有一個Listview.builder
可以在我的游戲中添加玩家。 正如我在此處的 gif 中所示:
我想要做的是,而不是在播放器“test”下添加播放器“test2”,我想將它添加到他之上......我嘗試了reverse
的方法,但如果我這樣做,Listview 從屏幕底部...
你有什么想法來解決這個問題嗎?
這是我的代碼:
body: ListView.builder(
itemCount: Provider.of<PlayerProvider>(context).getPlayerList.length,
itemBuilder: (context, index) {
return Column(
children: [
PlayerDismissible(index),
Divider(
color: Colors.orange,
height: 0,
)
],
);
}),
bottomSheet: BottomPlayerBar(),
當您將新播放器添加到列表時,您可以使用List
上的insert方法將新播放器添加為列表中的第一個,例如..
Player newPlayer = Player('name');
playersList.insert(0, newPlayer);
添加到列表的頂部效率不高,因為它需要將列表的所有剩余項目向右移動。 只需通過add()
添加到列表並通過反轉索引獲取項目:
PlayerDismissible(length - index - 1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.