简体   繁体   中英

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

staff.ulogin is a list returned from a web service. If there is more than one item returned, I need to display of list of those items (displaying the company name). I can get the first item displaying, but I'm not sure how to display the entire list.

I also need the user to be able to tap an item so I can setup that company for use, so I'll need to know what item they chose. Thanks for any help.

  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 

I believe that the correct way of display a list o items is using a ListView. For this case you can use a ListView.builder like this:

        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]); 
                        },    
                   ),
                }
            )
        )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM