How to use Listview in Flutter

In Flutter, Listview is also the most crucial widget we can use to display a list of data, in this article we are going to learn How to use Listview in Flutter.

We can use various types of listview like ListViewListView.builder, ListView.separated, etc Let's  take a simple example of  listview 

Constructor of ListView.builder:

ListView.builder(
{Key key,
Axis scrollDirection: Axis.vertical,
bool reverse: false,
ScrollController controller,
bool primary,
ScrollPhysics physics,
bool shrinkWrap: false,
EdgeInsetsGeometry padding,
double itemExtent,
@required IndexedWidgetBuilder itemBuilder,
int itemCount,
bool addAutomaticKeepAlives: true,
bool addRepaintBoundaries: true,
bool addSemanticIndexes: true,
double cacheExtent,
int semanticChildCount,
DragStartBehavior dragStartBehavior: DragStartBehavior.start,
ScrollViewKeyboardDismissBehavior keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.manual,
String restorationId,
Clip clipBehavior: Clip.hardEdge}
)

We can see various properties of the ListView.builder constructor we can set according to our needs. if you want to know in more detail you can read here. 

Full 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 MaterialApp(
      title: 'Errorgram',
      home: const Errorgram(title: 'Errorgram'),
    );
  }
}

class Errorgram extends StatefulWidget {
  const Errorgram({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<Errorgram> createState() => _ErrorgramState();
}

class _ErrorgramState extends State<Errorgram> {


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.red,
          title: Text("Errorgram"),
        ),
        body: ListView.builder(
          itemCount: 100,
          itemBuilder: (_, i) => Card(
            child: ListTile(
                title: Text(
              'Data $i',
              style: TextStyle(color: Colors.red),
            )),
          ),
        ));
  }


}

 

Output:

How to use listview in flutter?

 

Conclusion:

In this post, We saw How to use Listview in Flutter with example, I hope this post may help you.

Tags: