How to add a ListView to a Column in Flutter?

ListView Widget has been introduced to reduce the overload of having various layouts performing the same task.

Purpose Of ListView Widget

Most of the time, the dynamic behavior is achieved by changing the contents that are being displayed in the body. So Flutter provides ListView.Builder() constructor which can be used to generate dynamic contents from external sources.

How to add a ListView to a Column Widget In Flutter?

In Flutter, you can add a ListView widget inside a Column widget to create a scrollable list of items vertically aligned within a column. Here’s how you can do it:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'ListView in Column Example',
      home: Scaffold(
        appBar: AppBar(
          title: Text('ListView in Column Example by flutterfever.com'),
        ),
        body: Column(
          children: [
            // Other widgets can go here before or after the ListView
            Text(
              'Above ListView',
              style: TextStyle(fontSize: 20),
            ),
            Expanded(
              child: ListView.builder(
                itemCount: 50, // Example number of list items
                itemBuilder: (BuildContext context, int index) {
                  return ListTile(
                    title: Text('Item $index'),
                  );
                },
              ),
            ),
            // Other widgets can go here before or after the ListView
            Text(
              'Below ListView',
              style: TextStyle(fontSize: 20),
            ),
          ],
        ),
      ),
    );
  }
}

In this example:

  • We have a Column widget as the body of the Scaffold.
  • Inside the Column, we have added some text widgets and then an Expanded widget containing a ListView.builder.
  • The Expanded widget allows the ListView to take all the remaining space in the column.
  • We’re using ListView.builder to create a scrollable list with a specified number of items.

Remember to replace the itemCount and the itemBuilder with your own data and UI elements according to your application’s requirements. You can customize the appearance of each list item by modifying the itemBuilder function.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More