How to implement a horizontal ListView in Flutter

Last updated on February 15, 2022 Augustus Loading... Post a comment

To make a horizontal ListView in Flutter, just set its scrollDirection property to Axis.horizontal, like this:

ListView(
      scrollDirection: Axis.horizontal,
      children: [],
)

Example 1

The code:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      // Hide the debug banner
      debugShowCheckedModeBanner: false,
      title: 'Kindacode.com',
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('Kindacode.com'),
        ),
        body: SafeArea(
          child: SizedBox(
            width: double.infinity,
            height: 330,
            child: ListView(
              scrollDirection: Axis.horizontal,
              children: [
                Container(
                    margin: const EdgeInsets.all(15),
                    width: 200,
                    height: 300,
                    color: Colors.amber,
                    alignment: Alignment.center,
                    child: const Text(
                      '1',
                      style: TextStyle(fontSize: 80),
                    )),
                Container(
                    margin: const EdgeInsets.all(15),
                    width: 200,
                    height: 300,
                    color: Colors.amber,
                    alignment: Alignment.center,
                    child: const Text(
                      '2',
                      style: TextStyle(fontSize: 80),
                    )),
                Container(
                    margin: const EdgeInsets.all(15),
                    width: 200,
                    height: 300,
                    color: Colors.amber,
                    alignment: Alignment.center,
                    child: const Text(
                      '3',
                      style: TextStyle(fontSize: 80),
                    )),
                Container(
                    margin: const EdgeInsets.all(15),
                    width: 200,
                    height: 300,
                    color: Colors.amber,
                    alignment: Alignment.center,
                    child: const Text(
                      '4',
                      style: TextStyle(fontSize: 80),
                    )),
                Container(
                    margin: const EdgeInsets.all(15),
                    width: 200,
                    height: 300,
                    color: Colors.amber,
                    alignment: Alignment.center,
                    child: const Text(
                      '5',
                      style: TextStyle(fontSize: 80),
                    )),
                Container(
                    margin: const EdgeInsets.all(15),
                    width: 200,
                    height: 300,
                    color: Colors.amber,
                    alignment: Alignment.center,
                    child: const Text(
                      '6',
                      style: TextStyle(fontSize: 80),
                    )),
              ],
            ),
          ),
        ));
  }
}

Output:

Example 2: Using ListView.builder

The code:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      // Hide the debug banner
      debugShowCheckedModeBanner: false,
      title: 'Kindacode.com',
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  // Generating dummy data
  final List myData = List.generate(100, (index) => 'Item $index');
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('Kindacode.com'),
        ),
        body: SafeArea(
          child: SizedBox(
              width: double.infinity,
              height: 330,
              child: ListView.builder(
                scrollDirection: Axis.horizontal,
                itemCount: myData.length,
                itemBuilder: (BuildContext ctx, index) {
                  return Container(
                    margin: const EdgeInsets.all(15),
                    color: Colors.blue,
                    width: 150,
                    alignment: Alignment.center,
                    child: Text(
                      myData[index],
                      style: const TextStyle(color: Colors.white, fontSize: 30),
                    ),
                  );
                },
              )),
        ));
  }
}

Output:

Further reading:

You can also check out our Flutter category page or Dart category page for the latest tutorials and examples.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Related Articles