ListView Seperated  Widget and Attributes

ListView Seperated Widget and Attributes

ยท

2 min read

Flutter's rich set of widgets plays a crucial role in crafting seamless and dynamic user interfaces. One such widget, ListView.separated, provides an elegant solution for displaying a scrollable list with separators between its items. In this blog post, we'll delve into the usage of ListView.separated and explore its attributes with practical examples.

What is ListView.separated?

ListView.separated is a Flutter widget designed for efficiently displaying a scrollable list of items with custom separators between them. This widget is particularly useful when you want to customize the appearance of dividers or spaces between your list items.

Attributes of ListView.separated:

  1. itemBuilder (required): A callback that returns a widget for each item in the list. This is where you define the structure and content of your list items.

     itemBuilder: (BuildContext context, int index) {
       return ListTile(
         title: Text('Item $index'),
       );
     },
    
  2. separatorBuilder (required): A callback that returns a widget to be used as a separator between adjacent items in the list.

     separatorBuilder: (BuildContext context, int index) {
       return Divider();
     },
    
  3. itemCount (required): The number of items in the list.

     itemCount: 10,
    
  4. padding: The padding around the list.

     padding: EdgeInsets.all(8.0),
    
  5. physics: The scroll physics applied to the list.

     physics: BouncingScrollPhysics(),
    

Example Usage:

Let's create a simple example where we display a list of colors with dividers between them.

ListView.separated(
  itemCount: colors.length,
  itemBuilder: (BuildContext context, int index) {
    return ListTile(
      title: Text(colors[index]),
    );
  },
  separatorBuilder: (BuildContext context, int index) {
    return Divider();
  },
),

In this example, colors is a list containing color names, and each item in the list is displayed as a ListTile with a Divider separating them.

Did you find this article valuable?

Support Vinit Mepani (Flutter Developer) by becoming a sponsor. Any amount is appreciated!

ย