Sub topic 3
Direction, padding and reverse
This chapter covers three constructor arguments shared by ListView and GridView: scrollDirection, padding and reverse. It assumes you can build a basic ListView, know how Row, Column and Expanded pass constraints, and can run a Flutter app with hot reload. You will use these arguments in almost every real list, and each has an edge case that causes common bugs.
Main axis and cross axis
Every scrollable has a main axis, the direction it scrolls, and a cross axis, the other direction. scrollDirection picks the main axis. It takes an Axis value and defaults to Axis.vertical.
The scrollable fills the main axis with a viewport that can show only part of its content. On the cross axis, it takes whatever size its parent gives it. A scrollable does not shrink to fit its content on the cross axis. This is why a horizontal list needs a bounded height, as the next section shows.
Horizontal lists
Set scrollDirection: Axis.horizontal to get a carousel-style list. Put it in a widget that gives it a height:
import 'package:flutter/material.dart';
class Carousel extends StatelessWidget {
const Carousel({super.key});
@override
Widget build(BuildContext context) {
return SizedBox(
height: 120,
child: ListView.builder(
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.symmetric(horizontal: 16),
itemCount: 10,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.only(right: 12),
child: AspectRatio(
aspectRatio: 1,
child: Card(child: Center(child: Text('Card $index'))),
),
);
},
),
);
}
}Without the SizedBox, placing this list in a Column fails with "Horizontal viewport was given unbounded height." A Column gives its children unlimited height, and the horizontal list has no way to choose a height on its own. shrinkWrap does not fix this. It only shrinks the main axis, and here the main axis is horizontal.
Each card gets the full 120 pixels of cross-axis height. The AspectRatio then makes it square, so the width follows from the height.
Horizontal grids
GridView accepts the same argument. In a horizontal grid, the roles of the grid settings turn with the axes:
SizedBox(
height: 220,
child: GridView.count(
scrollDirection: Axis.horizontal,
crossAxisCount: 2,
mainAxisSpacing: 8,
crossAxisSpacing: 8,
padding: const EdgeInsets.all(8),
children: List.generate(
20,
(index) => Card(child: Center(child: Text('$index'))),
),
),
)Here crossAxisCount: 2 means two rows, because the cross axis is now vertical. mainAxisSpacing is the horizontal gap between columns, and crossAxisSpacing is the vertical gap between the two rows. Read these names as relative to the scroll direction, not as "columns" and "rows".
Padding
padding insets the content inside the scrollable. It takes any EdgeInsetsGeometry, such as EdgeInsets.all(16), EdgeInsets.symmetric(vertical: 8) or EdgeInsets.fromLTRB(16, 8, 16, 88).
The two options look different while scrolling:
| Approach | Padding at the ends | Items at the edges while scrolling |
|---|---|---|
| `ListView(padding: ...)` | Scrolls with the content | Slide all the way to the viewport edge |
| `Padding(child: ListView(...))` | Fixed in place | Disappear at the padded edge |
Automatic safe-area padding
When you leave padding as null, the list reads MediaQuery and adds the device's safe-area inset along the main axis. A vertical list gets top and bottom padding, so the first and last items are not hidden behind a notch or a home indicator. A Scaffold already removes the part of the inset that its app bar covers, so inside one you usually see no extra gap.
If you pass any padding value, even EdgeInsets.zero, this automatic behavior stops. Your value replaces it. For a list that runs under a system bar, add the inset yourself, for example by wrapping the list in SafeArea or by adding MediaQuery.paddingOf(context).bottom to your bottom padding.
Leaving room for floating buttons
A floating action button sits on top of the list and covers the last item at full scroll. Add bottom padding to the list so the last item can scroll clear of the button. A button is 56 pixels high plus a 16-pixel margin, so 88 pixels of bottom padding is a safe value:
ListView.builder(
padding: const EdgeInsets.fromLTRB(16, 16, 16, 88),
itemCount: 30,
itemBuilder: (context, index) => ListTile(title: Text('Row $index')),
)Right-to-left languages
EdgeInsets uses physical sides: left is always left. In a layout that mirrors for Arabic or Hebrew, use EdgeInsetsDirectional with start and end. In a horizontal list, start becomes the right side in a right-to-left layout. The list also begins at the right edge and scrolls the other way, because Directionality flips the horizontal main axis.
Reverse
reverse: true flips the direction in which the list lays out. In a vertical list, item 0 sits at the bottom, later items stack upward, and the scroll position starts at the bottom. A horizontal list starts at the right, or at the left in a right-to-left layout.
This matches how chat screens work. The newest message is at the bottom, and the list opens there without any scrolling code. You store the data newest first and let reverse place it:
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: ChatDemo()));
class ChatDemo extends StatefulWidget {
const ChatDemo({super.key});
@override
State<ChatDemo> createState() => _ChatDemoState();
}
class _ChatDemoState extends State<ChatDemo> {
// Newest message first, so index 0 is drawn at the bottom.
final List<String> _messages = ['Message 3', 'Message 2', 'Message 1'];
void _send() {
setState(() {
_messages.insert(0, 'Message ${_messages.length + 1}');
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Chat')),
body: ListView.builder(
reverse: true,
padding: const EdgeInsets.fromLTRB(16, 16, 16, 88),
itemCount: _messages.length,
itemBuilder: (context, index) {
return ListTile(title: Text(_messages[index]));
},
),
floatingActionButton: FloatingActionButton(
onPressed: _send,
child: const Icon(Icons.send),
),
);
}
}Two details matter here. First, insert(0, ...) adds the message at the visual bottom. If the user has scrolled up to read history, their view stays anchored to the same content, because offsets are measured from the bottom edge. A non-reversed list that appends to the end gives you neither the automatic start at the bottom nor this anchoring.
Second, the padding sides remain physical. With reverse: true, bottom: 88 is still the bottom of the screen, which is now the start of the list. The 88 pixels appear below message 1's neighbor at index 0, which is where the button sits.
What reverse changes and what it does not
- It changes where item 0 sits, where the scroll starts, and which way positive scroll offsets run.
- It does not reverse the order of your data. Index 0 is still the first item the builder asks for.
- It does not change the order of children in a plain
ListView(children: [...]). The first child is still drawn at the start of the main axis, which is now the bottom or the right. - It interacts with
shrinkWrap. A short reversed list withshrinkWrap: trueinside a taller parent is packed against the parent's start edge, not against the visual top. Check this with a few items, because it surprises people building chat bubbles that should sit at the bottom. - In a
ScrollController, offset 0 is the starting edge, which is the bottom for a reversed vertical list. Code that checksextentAfterfor "load more" therefore triggers at the top of a reversed chat, which is the right place for loading older messages.
Summary of the three arguments
| Argument | Default | Effect | Common mistake |
|---|---|---|---|
| `scrollDirection` | `Axis.vertical` | Chooses the main axis | Horizontal list in a `Column` without a height |
| `padding` | `null` (safe-area inset) | Insets content inside the viewport | Expecting it to stay fixed, or losing the automatic inset by passing a value |
| `reverse` | `false` | Starts at the end, flips item order on screen | Assuming it reverses the data or the `children` order |
Exercise
A horizontal ListView placed directly in a Column throws "Horizontal viewport was given unbounded height." Why does adding shrinkWrap: true not fix it, and what does?
Show answer
shrinkWrap only makes the list size itself to its content along its main axis, which is horizontal here. The problem is on the cross axis: the Column gives its children unlimited height, and a scrollable never sizes itself to its content on the cross axis. Wrapping the list in a SizedBox with a fixed height, or in an Expanded or another widget that gives a bounded height, supplies the missing constraint.
Exercise
In a chat list with reverse: true and padding: EdgeInsets.fromLTRB(16, 16, 16, 88), where does the 88 pixels of padding appear, and which item is next to it?
Show answer
It appears at the bottom of the screen, because padding sides are physical. In a reversed vertical list, the bottom is the start of the list, so item 0 (the newest message) sits right above that 88-pixel gap. This is the space that keeps the newest message clear of a floating action button.