What is listView, listview.builder in Flutter

Hasnain Mirrani
4 min readMay 21, 2023

and all types of listview, Scrollable, physics, Ascending and descending properties

In Flutter, a ListView is a widget used to display a scrollable list of items. It's a fundamental component for building dynamic and scrollable user interfaces. ListView is highly customizable and supports various types, each designed to fulfill specific requirements. Here are the different types of ListView in Flutter:

ListView: The basic ListView displays a list of widgets in a single column, vertically scrolling if necessary.

ListView.builder:

This type of ListView is used when you have a large or infinite list of items. It lazily constructs the widgets on-demand as they become visible on the screen, optimizing performance and memory usage.

ListView.separated:

Similar to ListView.builder, this type allows you to build a list with separators between items. You can define custom separators by specifying a separator builder callback.

ListView.custom: The ListView.custom gives you complete control over the list by providing custom delegates. It allows you to specify your own SliverChildDelegate to define the list's children and their layout.

ListView.fixedExtent:

This type creates a list where all items have the same extent, meaning they have fixed and equal dimensions. It's useful when you want items to take up the same amount of space, such as a grid-like layout.

ListView.horizontal:

While most ListView types are vertically oriented, ListView.horizontal enables you to create a horizontally scrolling list of items.

Advanced usage of ListView involves optimizing performance and enhancing the user experience. Here are a few techniques:

Use the ListView.builder or ListView.custom with a limited number of child widgets to improve performance. This way, only the visible items are constructed.

Implement lazy loading or pagination to load data incrementally as the user scrolls through the list. This is especially useful when dealing with large datasets.

Implement pull-to-refresh functionality using RefreshIndicator to allow users to reload the list content.

Utilize widgets like AnimatedList or SliverList for more advanced and animated list behaviors.

Optimize item rendering by using IndexedWidgetBuilder and recycling widgets with keys. This technique avoids rebuilding the entire item when its content changes.

These are some ways to make the most of ListView in Flutter, allowing you to create efficient, dynamic, and interactive lists in your applications.

Certainly! Here are code examples demonstrating each type of ListView in Flutter:

ListView:

ListView(
children: <Widget>[
ListTile(title: Text('Item 1')),
ListTile(title: Text('Item 2')),
ListTile(title: Text('Item 3')),
// Add more items as needed
],
)

ListView.builder

ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return ListTile(title: Text(items[index]));
},
)

ListView.separated

ListView.separated(
itemCount: items.length,
separatorBuilder: (context, index) => Divider(),
itemBuilder: (context, index) {
return ListTile(title: Text(items[index]));
},
)

ListView.custom

ListView.custom(
childrenDelegate: SliverChildBuilderDelegate(
(context, index) {
return ListTile(title: Text(items[index]));
},
childCount: items.length,
),
)

ListView.fixedExtent

ListView.fixedExtent(
itemExtent: 50, // Set the height or width of each item
children: <Widget>[
ListTile(title: Text('Item 1')),
ListTile(title: Text('Item 2')),
ListTile(title: Text('Item 3')),
// Add more items as needed
],
)

ListView.horizontal

ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
ListTile(title: Text('Item 1')),
ListTile(title: Text('Item 2')),
ListTile(title: Text('Item 3')),
// Add more items as needed
],
)

These examples demonstrate how to use each type of ListView in Flutter. Remember to customize the list items and adjust the code according to your specific requirements.

Scrollable and Scroll physics

In Flutter, both "scrollable" and "physics" are properties of a ListView widget that control how the list behaves during scrolling.

Scrollable:
The "scrollable" property determines whether the ListView can be scrolled or not. By default, a ListView is scrollable, meaning it allows the user to scroll through the list of items. If you set the "scrollable" property to false, the ListView becomes non-scrollable, and the user won't be able to scroll through the items.
Example of a non-scrollable ListView

ListView(
scrollable: false,
children: <Widget>[
ListTile(title: Text('Item 1')),
ListTile(title: Text('Item 2')),
ListTile(title: Text('Item 3')),
// Add more items as needed
],
)

Physics

The "physics" property of a ListView determines the scrolling behavior and the type of physics applied to the scrollable list. Flutter provides several physics options that can be assigned to the "physics" property. The choice of physics affects how the list responds to user input, such as scrolling, flinging, or bouncing.
Some commonly used physics options are:

BouncingScrollPhysics: This physics allows the list to scroll beyond its limits and provides a bouncing effect when reaching the edge of the list.
ClampingScrollPhysics: This physics prevents the list from scrolling beyond its limits and applies a clamping effect when reaching the edge.
AlwaysScrollableScrollPhysics: This physics always allows the list to be scrolled, regardless of the list’s size.
Example of using different physics:

ListView(
physics: BouncingScrollPhysics(),
children: <Widget>[
ListTile(title: Text('Item 1')),
ListTile(title: Text('Item 2')),
ListTile(title: Text('Item 3')),
// Add more items as needed
],
)

By specifying different physics for the ListView, you can customize the scrolling behavior and provide the desired user experience for your app’s list.

Sorting

Here’s an example of how you can use the sort method of a List to achieve ascending or descending sorting:

List<String> items = ['Item 3', 'Item 1', 'Item 2'];

// Sort the list in ascending order
items.sort((a, b) => a.compareTo(b));

// Create a ListView with ascending sorted items
ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return ListTile(title: Text(items[index]));
},
)

In the above code snippet, the sort method is used to sort the items list in ascending order based on string comparison. You can use different comparison methods based on your specific data types and sorting requirements.

To achieve descending sorting, you can reverse the order of the sorted list:

// Sort the list in descending order
items.sort((a, b) => b.compareTo(a));

// Create a ListView with descending sorted items
ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return ListTile(title: Text(items[index]));
},
)

By manipulating the order of the items in your data source, you can control whether the ListView displays the items in ascending or descending order.

--

--

Hasnain Mirrani

Update the lattest and well explain All about Flutter make you from Zero to Hero in Flutter. follow https://www.linkedin.com/in/hasnain-mirrani-b47ab7131