簡體   English   中英

如何使用 Flutter 中的 ListTile 顯示列內的項目列表

[英]How to display a list of items inside a column using a ListTile in Flutter

staff.ulogin 是從 web 服務返回的列表。 如果退回的物品不止一件,我需要顯示這些物品的列表(顯示公司名稱)。 我可以顯示第一個項目,但我不確定如何顯示整個列表。

我還需要用戶能夠點擊一個項目,以便我可以設置該公司以供使用,所以我需要知道他們選擇了什么項目。 謝謝你的幫助。

  if (staff.ulogin.length > 1) {
    Alert(
      context: context,
      title: 'Choose Company',
      content: Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[

            //how to display all the items
            ListTile(
              title: Text(staff.ulogin[0].company),
              onTap () {},    //  <--- how to get the index of the item tapped
            ),
          ],
        ),
      ),
      buttons: [
        DialogButton(
          child: Text('Cancel', style: TextStyle(color: Colors.white, fontSize: 20)),
          color: kMainColor,
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
      ],
    ).show();
  } else 

我相信顯示項目列表的正確方法是使用 ListView。 對於這種情況,您可以像這樣使用 ListView.builder:

        Container(
             height: 300.0, // Change as you wish
             width: 300.0, // Change as you wish
              child: ListView.builder
              (
                itemCount: staff.ulogin.length,
                itemBuilder: (context, index) {
                  return ListTile(
                     title: Text(staff.ulogin[index].company),
                         onTap () {
                               someFunction(staff.ulogin[index]); 
                        },    
                   ),
                }
            )
        )

暫無
暫無

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

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