[英]How to use ListTile inside horizontal ListView in Flutter?
[英]Flutter: How to add an extra ListTile to ListView manually
我想要做的是在ListView
的最后添加一個額外的LisTile
但我仍然不明白。 我目前的代碼是這樣的。
child: ListView.builder(
itemBuilder: (context, index) {
if (index == 0) {
// Add an extra item to the start
return ListTile(
...
);
}
index -= 1;
final item = _items[index];
// I want to an extra item here as well
if (index == _items.length) {
return ListTile();
}
return ListTile(
...
);
},
itemCount: _items.length + 2,
上面的方法我已經試過了,還是不行。 有錯誤。
無效值:不在 0..4 范圍內,包括 5
當我將itemCount
更改為_items.length + 1
,它不會顯示我想要添加到最后的額外ListTile
。
如果你想添加到開頭和結尾,請檢查下面
child: ListView.builder(
itemBuilder: (context, index) {
if (index == 0) {
// Add an extra item to the start
return ListTile(
...
);
}
if (index == _items.length + 1) {
return ListTile();
}
index -= 1;
final item = _items[index];
return ListTile(
...
);
},
itemCount: _items.length + 2,
你的錯誤在這里:
itemCount: _items.length + 2,
你應該加一
因為你添加了 2 個元素,最后一次調用你得到 index = list.length + 1,然后你從中減去 1,最后你的長度是 1。
例如,假設您的列表有 5 個元素,因為您有
itemCount: _items.length + 2
Listview 將調用您的 func 7 次,在第 7 次調用中,您的索引為 6,您正在執行索引 =-1,這等於 5,而 5 超出了您的范圍。 (你有表格 0 到 4)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.