A List That Scrolls
Part of the Lists and Screens section of Coddy's Flutter journey. Lesson 1 of 33.
A Column is as tall as everything in it. When that is taller than the screen, the bottom is cut off, and Flutter paints yellow and black stripes where it overflows.
A ListView takes the same children: as a Column, but it scrolls. Drag it up on the phone and the rest of the list comes into view.
ListView(
children: [
Text('One'),
Text('Two'),
Text('Three'),
],
)A ListView fills the space it is given and always starts at the top, so it has no mainAxisSize and no mainAxisAlignment.
Challenge
EasyThe app lists 20 cities in a Column, and most of them do not fit: look for the stripes at the bottom of the phone. Change the Column to a ListView so you can scroll down to Wellington, the last city.
Try it yourself
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
final List<String> cities = [
'Amsterdam',
'Bangkok',
'Cairo',
'Dublin',
'Edinburgh',
'Florence',
'Geneva',
'Havana',
'Istanbul',
'Jakarta',
'Kyoto',
'Lima',
'Madrid',
'Nairobi',
'Oslo',
'Paris',
'Quito',
'Rome',
'Seoul',
'Wellington',
];
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(title: const Text('Cities')),
body: Column(
children: [
for (String city in cities)
Padding(
padding: const EdgeInsets.all(16),
child: Text(city, style: const TextStyle(fontSize: 24)),
),
],
),
),
);
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Lists and Screens
1Scrolling Lists
A List That ScrollsList TilesLeading and TrailingTap a List TileDividers Between Rows6Tabs, Drawers and Swipes
Tabs With a Navigation BarFilled Icons for the Open TabA Side DrawerSwipe to DeleteUndo a SwipePractice on your own: Online Flutter compiler