Adding top and bottom separators with ListView.separated

ListView.separated is a handy API that we can use to add separators between items inside a Flutter ListView.

According to the documentation:

Separators only appear between list items: separator 0 appears after item 0 and the last separator appears before the last item.

This means that there are no separators above the first item, and below the last item.

This is visually noticeable on iOS, where lists can over-scroll by default.

Here's some code to set things straight, and add top and bottom separators to your ListViews:

class ListViewWithAllSeparators<T> extends StatelessWidget { const ListViewWithAllSeparators({Key key, this.items, this.itemBuilder}) : super(key: key); final List<T> items; final Widget Function(BuildContext context, T item) itemBuilder; @override Widget build(BuildContext context) { return ListView.separated( itemCount: items.length + 2, separatorBuilder: (_, __) => Divider(height: 0.5), itemBuilder: (context, index) { if (index == 0 || index == items.length + 1) { return Container(); // zero height: not visible } return itemBuilder(context, items[index - 1]); }, ); } }

You're welcome. 😉

Want More?

Invest in yourself with my high-quality Flutter courses.

The Complete Dart Developer Guide

The Complete Dart Developer Guide

Learn Dart Programming in depth. Includes: basic to advanced topics, exercises, and projects. Fully updated to Dart 2.15.

Flutter Animations Masterclass - Full Course

Flutter Animations Masterclass - Full Course

Master Flutter animations and build a completely custom habit tracking application.

Flutter & Firebase Course: Build a Complete App for iOS & Android

Flutter & Firebase Course: Build a Complete App for iOS & Android

A full course with in-depth content, taking you from the basics all the way up to more advanced topics.

Flutter REST API Crash Course

Flutter REST API Crash Course

Build a Coronavirus Tracking App, and learn how to use REST APIs in Flutter.

Get the best Flutter tutorials, right in your inbox.

Join over 16,000 developers who receive my weekly Flutter email course & newsletter:

No spam, ever. Unsubscribe at any time.