简体   繁体   中英

Flutter - How to add first top and last bottom list with divider() on ListView.separated()

I added a divider on my list but i also want to have a divider line on the top part and last end part on the list. Because the divider line are only in the middle of each list.

ListView.separated(
 itemCount: user.personList.length,
 separatorBuilder: (context, index) => Divider(),
 itemBuilder: (context, index){
   return ListTile(
    title: Text(user.personList[index].id)
  )
 }
)

You can add divider on top and bottom in following way.

you have to increase length by 2 and on 0 and length-1 return container, which will give make divider more clearly. if you want dedicated divider then you can also use divider widget.

ListView.separated(
      itemCount: user.personList.length + 2,
      separatorBuilder: (context, index) => Divider(),
      itemBuilder: (context, index) {
        if (index == 0 || index == user.personList.length + 1) {
          return Divider(
            color: Colors.black,
            thickness: 2,
          );
        }
        return ListTile(title: Text((index - 1).toString())); // note: you have to access element by -1;
      },
    ),

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