Sub topic 3
Direction, Padding, and Reverse in Flutter Scrollables
This chapter assumes basic Dart lists and closures, Flutter widgets, and Row, Column, and Expanded. You should also be able to run an app and use hot reload.
A ListView displays children inside a scrollable viewport. Its main axis is the direction in which items are placed. A normal list uses vertical scrolling. You can change it to horizontal scrolling with scrollDirection.
Choosing the scroll direction
Use Axis.vertical for rows of content, such as messages or settings. Use Axis.horizontal for a row of cards, images, or categories.
When the direction is horizontal, each child needs a useful width. Otherwise, Flutter may give the child an unexpected size.
import 'package:flutter/material.dart';
void main() {
runApp(const DirectionExample());
}
class DirectionExample extends StatelessWidget {
const DirectionExample({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Categories')),
body: ListView(
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.all(16),
children: const [
SizedBox(width: 140, child: CategoryCard('Books')),
SizedBox(width: 140, child: CategoryCard('Music')),
SizedBox(width: 140, child: CategoryCard('Games')),
],
),
),
);
}
}
class CategoryCard extends StatelessWidget {
const CategoryCard(this.label, {super.key});
final String label;
@override
Widget build(BuildContext context) {
return Card(
child: Center(child: Text(label)),
);
}
}Adding space with padding
The padding property adds space between the viewport's edge and the list content. It does not add space between adjacent children.
Use EdgeInsets.all for equal space on every side. Use EdgeInsets.symmetric when vertical and horizontal space differ. Use EdgeInsets.only for individual sides.
ListView(
padding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 24,
),
children: const [
ListTile(title: Text('First item')),
ListTile(title: Text('Second item')),
],
)To add space between items, put padding on each child or use ListView.separated. Padding on the list itself only surrounds the collection.
Reversing the scroll direction
reverse: true reverses the scrollable's direction. For a vertical list, the initial view is at the bottom, and scrolling upward reveals earlier items. This is useful for chat screens where new messages appear at the bottom.
reverse does not change the order of the data in your Dart list. It changes how the scrollable lays out and initially positions its content. If your data is in the wrong order, sort or reverse the data separately.
The three properties work together: direction controls the main axis, padding surrounds the content, and reverse flips the axis direction.
Exercise
You need a horizontal row of cards with 12 pixels of space around the row and 8 pixels between cards. Which ListView property controls the direction, and which property controls the outer space?
Show answer
Set scrollDirection: Axis.horizontal for the direction and padding: const EdgeInsets.all(12) for the outer space. Use ListView.separated with a separator width of 8 for the gaps between cards.