我有一個容器,我試圖從串列中列印出專案,減去串列中的最后一項。我正在使用 list.generate:
child: Container(
width: double.infinity,
color: Colors.white,
child: ListView(
padding: const EdgeInsets.only(top: 10.0),
children: List.generate(
Provider.of<WeekList>(context).listOfWeeks.toList().length -
1,
(index) => ListTileCustom(
index: index,
),
),
),
),
我的問題是當它列印出我的自定義ListTileCustom小部件時,添加到串列中的每個新專案都添加到視圖的底部而不是頂部。

在上圖中,順序應該是:
- 紅條
- 紅/綠條
- 綠條
當我通過按下按鈕添加新專案時,它們應該被添加到頂部而不是底部。
我嘗試添加reverse: true并得到正確的順序,但將所有內容移動到底部并在它們上方添加大量空白......也不確定滾動是否會在那個時候朝著正確的方向作業。

uj5u.com熱心網友回復:
要在 Dart 中反轉串列,可以使用..reversed方法。在您發布的代碼中,這將是:children: List.generate(/* omitted */).reversed.toList().
另一個(可能更好)的解決方案是for loop直接使用 in children,例如:
ListView(
children: [
for (int i = 0; i < 5; i ) FlutterLogo(),
],
)
使用這種方法,您可以通過更改回圈(例如)或其他方式輕松地反轉串列(并省略一個元素,如您所愿)for (int i = 10; i > 1; i--)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/448436.html
上一篇:使用QtDBus捕獲并回應Connman'RequestInput'方法呼叫
下一篇:Res.Render正在停止
