簡體   English   中英

Flutter For Loop inside children only allows one widget

[英]Flutter For Loop inside children only allows one widget

我在列表視圖小部件內有一個 For 循環來填充其子項。 現在我有一個小部件我的自定義歷史視圖小部件:

  Expanded(
    flex: 5,
    child: Container(
      width: double.infinity,
      color: Colors.white,
      child: ListView(
        padding: const EdgeInsets.only(top: 10.0),
        children: [
          for (var i = Provider.of<WeekList>(context)
                  .listOfWeeks
                  .toList()
                  .length;
              i > 1;
              i--)
            HistoryView(index: i - 2),
        ],
      ),
    ),
  )

問題是我想在歷史視圖小部件下添加第二個小部件,但我似乎無法弄清楚如何正確地執行此操作。 我認為它會像這樣簡單:

      Expanded(
        flex: 5,
        child: Container(
          width: double.infinity,
          color: Colors.white,
          child: ListView(
            padding: const EdgeInsets.only(top: 10.0),
            children: [
              for (var i = Provider.of<WeekList>(context)
                      .listOfWeeks
                      .toList()
                      .length;
                  i > 1;
                  i--){
                      HistoryView(index: i - 2),
                      WeekWidget(index: i - 2 ),
                  }
                
            ],
          ),
        ),
      )

您可以將HistoryViewWeekWidget包裝在一列中

使用擴展運算符

      Expanded(
        flex: 5,
        child: Container(
          width: double.infinity,
          color: Colors.white,
          child: ListView(
            padding: const EdgeInsets.only(top: 10.0),
            children: [
              for (var i = Provider.of<WeekList>(context)
                      .listOfWeeks
                      .toList()
                      .length;
                  i > 1;
                  i--)
                      ...[
                      HistoryView(index: i - 2),
                      WeekWidget(index: i - 2 ),
                    ],                
            ],
          ),
        ),
      )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM