Sub topic 6
Dividers and Gaps with `ListView.separated`
Assume you know basic Dart lists and closures, Flutter widgets, and how build returns a widget tree. This chapter focuses on ListView.separated, a list constructor for placing a separator between adjacent items.
Why use `ListView.separated`?
A ListView displays children in a scrollable, one-dimensional viewport. The default constructor receives an explicit list of widgets. ListView.builder creates item widgets on demand from an index. ListView.separated adds a second builder for the widgets between items.
Use it when every pair of items needs the same divider, gap, or decoration:
itemBuilderbuilds the data items.separatorBuilderbuilds the widget between two items.itemCountgives the number of data items.- No separator appears after the final item.
The separator index identifies the item before the separator. For example, separator index 0 is between items 0 and 1.
Build a separated list
The following app displays messages with a one-pixel divider:
import 'package:flutter/material.dart';
void main() {
runApp(const MessagesApp());
}
class MessagesApp extends StatelessWidget {
const MessagesApp({super.key});
@override
Widget build(BuildContext context) {
const messages = [
'Welcome to Flutter',
'Your report is ready',
'Meeting starts at 10:00',
];
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Messages')),
body: ListView.separated(
padding: const EdgeInsets.all(16),
itemCount: messages.length,
itemBuilder: (context, index) {
return ListTile(
leading: const Icon(Icons.mail_outline),
title: Text(messages[index]),
);
},
separatorBuilder: (context, index) {
return const Divider(height: 1);
},
),
),
);
}
}ListView.separated is still lazy. Flutter builds only the items and separators needed near the visible part of the list. This makes it appropriate for long or changing collections.
Dividers, gaps, and custom separators
A Divider draws a line. Its height is the total space occupied by the divider, including any empty space around the line. For a simple gap, return a SizedBox instead:
ListView.separated(
itemCount: products.length,
itemBuilder: (context, index) {
return ProductTile(product: products[index]);
},
separatorBuilder: (context, index) {
return const SizedBox(height: 12);
},
)You can also use a separator that depends on its index. For example, return a thicker gap after every fifth item. Keep this logic in separatorBuilder, rather than adding extra fake entries to your data list.
Common errors
A separated list inside a Column needs a bounded height. Without a constraint, the list cannot determine how tall its viewport should be. Use Expanded when the list should occupy the remaining space:
Column(
children: [
const Text('Inbox'),
Expanded(
child: ListView.separated(
itemCount: 100,
itemBuilder: (context, index) {
return ListTile(title: Text('Message $index'));
},
separatorBuilder: (context, index) {
return const Divider();
},
),
),
],
)Use shrinkWrap: true only when the list must size itself inside another scrollable. It can require more layout work, so Expanded is usually better for a screen-sized list.
Exercise
If a list has five items, how many times does Flutter call separatorBuilder?
Show answer
Four times, because separators appear only between adjacent items, not after the last item.