我正在嘗試在 Flutter中使用簡單的ListView 。問題是我想將串列的兩個索引(索引)用于一張卡。
因此,如果 ListView 擴展時,它應該始終使用兩個索引。第一張卡可以訪問索引 1 和 2 中的資料,第二張卡可以訪問索引 3 和 4 中的資料,依此類推。
如果有人知道如何構建這樣的東西,我將不勝感激:)
uj5u.com熱心網友回復:
我看到了兩種處理這種情況的方法......
第一的
您可以生成其他串列,其中每個元素將包含原始串列的 2 個元素。
第二
使用 isOdd 或 isEven 忽略某些索引。
ListView.builder(
itemCount: myList.length
itemBuilder: (_, index) {
if(index.isEven){
return MyCustomTile(
first: myList[index]
second: index 1 == myList.length ? null : myList[index 1]
);
}
return Container();
}
)
注意不要讓索引超出串列的范圍
uj5u.com熱心網友回復:
這就是我處理它的方式,以防將來有人可以使用它:
ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: itemList.length,
itemBuilder: (context, index) {
if (index % 2 == 0 && index < itemList.length) {
return Column(
children: [
ListElementWidget(data: itemList[index]),
(index 1 < itemList.length)
? Padding(
padding: const EdgeInsets.only(top: 8.0),
child: ListElementWidget(
data: itemList[index 1]),
)
: const SizedBox(
width: 0,
),
],
);
} else {
return const SizedBox(width: 0);
}
},
),
),
除了 sizedbox 大小為 0 之外,肯定有更好的方法來設計不存在的小部件。但我對顫動相當陌生,它可以作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/429929.html
上一篇:JPAEntityGraph如何允許在運行時選擇物體圖?
下一篇:ListView上的位置引數太多
