简体   繁体   中英

Flutter: Add padding to only certain elements in my ListView

I'm trying to create a ListView and would only want the first item to be padded. Here is the code:

Expanded(
     child: ListView.builder(
       padding: EdgeInsets.all(16),
       itemCount: card.length,
       itemBuilder: (context, index) {
       if (index == 0) {
           return MyCard.buildRecordCard(
            card[index], context);
       } else {
          return MyCard.buildRecordsCards(
            card[index], context, index);
      }
    },
  ),
);

The output looks as follows: 电流输出

but I want cards 2...n (ie index.= 0) not to be padded and to stretch out to the end of the screen: Something like this:

if (index == 0) {
    padding: EdgeInsets.all(16),
    return MyCard.buildRecordCard(
      card[index], context);
} else {
    padding: 0,
    return MyCard.buildRecordsCards(
       card[index], context, index);
}

but that obviously doesn't work.

You just needed to wrap the widget with Padding . simply you can use ternary like

itemBuilder: (context, index) {
  return Padding(
      padding: index == 0 ? EdgeInsets.all(16) : EdgeInsets.zero,
      child: MyCard.buildRecordCard(card[index], context));
},

Solved. I just added the padding to the item I was building by wrapping it in a container as follows:

Container(
        padding: EdgeInsets.fromLTRB(16, 16, 16, 0),
        child: ...ListTile...

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