Menu
CoddyTech

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 icon

Challenge

Easy

The 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)),
              ),
          ],
        ),
      ),
    );
  }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Lists and Screens

Practice on your own: Online Flutter compiler