Sub topic 6
Dividers and gaps with ListView.separated
This chapter assumes you can write a basic ListView.builder, that you know itemBuilder receives a BuildContext and an index, and that you can run a Flutter app with hot reload. It covers how ListView.separated works, how to choose between dividers and gaps, how to vary separators by position, and the errors people hit most often.
What ListView.separated does
ListView.separated is ListView.builder plus a second builder. It calls itemBuilder for each item and separatorBuilder for the widget that goes between two adjacent items. You get dividers or spacing without wrapping every item yourself.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: ContactsPage()));
}
class ContactsPage extends StatelessWidget {
const ContactsPage({super.key});
static const List<String> names = [
'Ada', 'Alan', 'Barbara', 'Bjarne', 'Claude', 'Dennis', 'Donald', 'Grace',
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Contacts')),
body: ListView.separated(
itemCount: names.length,
itemBuilder: (context, index) => ListTile(
leading: const CircleAvatar(child: Icon(Icons.person)),
title: Text(names[index]),
),
separatorBuilder: (context, index) => const Divider(height: 1),
),
);
}
}itemCount is required here. The list needs to know how many items exist to work out how many separators to build.
How the indexes line up
For itemCount: n, Flutter builds n items and n - 1 separators. The list never adds a separator before the first item or after the last one. The index passed to separatorBuilder is the index of the item above (or to the left of) that separator. The first separator gets 0, the last gets n - 2.
Items and separators are both built lazily, in the same way as in ListView.builder. Only the ones near the viewport exist at any moment, so a separator costs almost nothing for a long list.
Dividers and gaps
There are two common kinds of separator. A divider draws a visible line. A gap is empty space.
| Separator | Use it when | Notes |
|---|---|---|
| `Divider` | Rows are plain and need visual grouping | Draws a horizontal line. `height` is the total space the divider takes, and `thickness` is the line itself. |
| `Divider` with `indent` / `endIndent` | Rows have a leading icon or avatar | Starts the line after the leading widget, as in many platform lists. |
| `SizedBox` | Items are cards or tiles with their own background | Adds empty space only. |
| `VerticalDivider` | The list scrolls horizontally | Draws a vertical line. `width` is the space it takes. |
Two details about Divider trip people up. height is the space reserved, not the line thickness. A Divider(height: 1) reserves 1 logical pixel, and the default thickness is 0, which draws a hairline. A Divider(height: 40) draws the same thin line but adds about 20 pixels above and below it, because the line is centered in the reserved space. To get a thicker line, set thickness, and set height at least as large.
For cards, a gap usually looks better than a line:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: CardsPage()));
}
class CardsPage extends StatelessWidget {
const CardsPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.separated(
padding: const EdgeInsets.all(16),
itemCount: 10,
itemBuilder: (context, index) => Card(
child: ListTile(title: Text('Card $index')),
),
separatorBuilder: (context, index) => const SizedBox(height: 12),
),
);
}
}padding handles the outer edges and separatorBuilder handles only the space between items. Because there is no separator before the first item or after the last, the outer margin must come from padding. A common mistake is to add a SizedBox to the top of every item and then find the list has a stray gap at the start.
Varying separators by position
separatorBuilder runs for each position, so it can return different widgets. Since index is always at most itemCount - 2, you can safely read index + 1. This example draws a thicker, indented divider only where the first letter of the name changes:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: GroupedNames()));
}
class GroupedNames extends StatelessWidget {
const GroupedNames({super.key});
static const List<String> names = [
'Ada', 'Alan', 'Barbara', 'Bjarne', 'Claude', 'Dennis', 'Donald', 'Grace',
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: ListView.separated(
itemCount: names.length,
itemBuilder: (context, index) => ListTile(title: Text(names[index])),
separatorBuilder: (context, index) {
final startsNewGroup = names[index][0] != names[index + 1][0];
if (startsNewGroup) {
return const Divider(height: 24, thickness: 2);
}
return const Divider(height: 1, indent: 16, endIndent: 16);
},
),
),
);
}
}This works because the separator can see the item before it and the item after it. If you need a real header widget with text, a separator is the wrong tool. Put the header into the item data, or use slivers (a later chapter).
Short lists: divideTiles
For a short list with a fixed set of children, you don't need a builder. ListTile.divideTiles returns the tiles with dividers inserted between them:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: SettingsPage()));
}
class SettingsPage extends StatelessWidget {
const SettingsPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: ListTile.divideTiles(
context: context,
tiles: const [
ListTile(title: Text('Profile')),
ListTile(title: Text('Settings')),
ListTile(title: Text('About')),
],
).toList(),
),
);
}
}This builds every child up front, like the default ListView constructor, and it only works with ListTiles. Use ListView.separated for anything dynamic or long.
Horizontal lists
With scrollDirection: Axis.horizontal, the list's cross axis is vertical, so a horizontal list needs a bounded height. A separator is laid out along the scroll axis, which is now horizontal. Use VerticalDivider or a SizedBox with a width:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: ChipsRow()));
}
class ChipsRow extends StatelessWidget {
const ChipsRow({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: [
SizedBox(
height: 56,
child: ListView.separated(
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.symmetric(horizontal: 16),
itemCount: 12,
itemBuilder: (context, index) =>
Center(child: Chip(label: Text('Tag $index'))),
separatorBuilder: (context, index) => const SizedBox(width: 8),
),
),
],
),
),
);
}
}A Divider in this list would not draw a vertical line, and a SizedBox(height: 8) would add no horizontal gap. Match the separator's dimension to the scroll axis.
Common errors
- Missing
itemCount. The parameter is required. UnlikeListView.builder, there is no sensible "infinite" mode for separated lists in practice, because the list can't tell where the last separator goes. - Index out of range in
separatorBuilder. Readingnames[index + 1]is safe. Readingnames[index + 2]fails on the last separator. - Separators that should appear at the ends. They never do. Use
padding, or wrap the list with a widget above or below it. - Zero-height separators. A
SizedBox.shrink()is a valid way to skip a divider for some positions. It still counts as a separator child, but it takes no space. - Dividers with
heightset only to a large number. The line stays thin and the list looks spaced out instead of divided. Setthicknesstoo. - Putting a
Dividerinside each item as well. You then get double lines. Pick one place for dividers.
Exercise
A ListView.separated has itemCount: 5. How many times is separatorBuilder called in total if the user scrolls through the whole list, and what index values does it receive? Then say what you would change so the list also has a 16-pixel gap before the first item and after the last.
Show answer
It is called 4 times, with indexes 0, 1, 2 and 3. Each index is the item above the separator, and there is no separator before item 0 or after item 4. To add the outer gaps, set padding: const EdgeInsets.symmetric(vertical: 16) on the ListView.separated, because separators never appear at the ends of the list.
Exercise
You want a horizontal list of thumbnails with 12 pixels between them. A teammate used separatorBuilder: (context, index) => const SizedBox(height: 12) and sees no gap. Why, and what is the fix?
Show answer
In a horizontal list the scroll axis is horizontal, so the gap between items must come from the separator's width. The height has no effect on spacing along the scroll axis. Change it to const SizedBox(width: 12). If you want a visible line, use VerticalDivider(width: 12).