Photo by eberhard 🖐 grossgasteiger on Unsplash

ListView separated constructor builds separator children in between child items and that makes a huge difference when we want to place a list of items.

There are similarities in ListView.builder and ListView.separated constructors, as they both take at least one IndexedWidgetBuilder. That is itemBulder parameter, which builds the children on demand.

However, there is one difference between these two ListView constructors.

The ListView.separated constructor has another parameter separatorBuilder, which similarly builds separator children which appear in between the child items.

As a whole, the ListView.separated constructor is a very useful API. We can use it to add separators between child items inside a flutter ListView.

Firstly, let us take a look at the screenshots. Secondly, after that, we'll discuss the code snippets. You may compare ListView.separated with the previous article on ListView.builder constructor. That'll clarify if any doubt arises.

The separators have a special character. They only appear between list items. That means, the last separator appears before the last item.

ListView separated sample one
ListView separated sample one

The first screenshot clearly displays the separator after the first student instance. The separator, a horizontal grey line, is attached with the image, as we've not kept the divider inside any container with a well-defined padding parameter.

Now, we'll take a look at the second screenshot that shows no separator after the last student instance.

ListView separated sample two
ListView separated sample two

Therefore, we have come to know one key point about the ListView.separated constructor. There will be no separator above the first item, and below the last item.

As we scroll, we find this characteristic quite logical. After the last instance we don't really need any separator.

It's also true for the first one. Why? Because it doesn't look good if one unnecessary separator shows up above the first instance.

Although we've only used a Divider widget as the separatorBuilder here, but we could have used a list of other widgets as its separator children in between the student instances.

For a fixed number of children, the ListView separated constructor is very useful.

Further reading >>

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

<a href="https://mesanjib.wordpress.com/2021/12/17/listview-separated-in-flutter/My books at Amazon

">Courses at Educative

GitHub repository

Technical blog

Twitter


This free site is ad-supported. Learn more