ListView Widget and Attributes


In Flutter, the ListView widget is used to create a scrollable, linear list of widgets. It's a commonly used widget for displaying a list of items in a vertical or horizontal direction. The ListView widget can be customized with various attributes to control its behavior and appearance.
Now, let's go through some key properties and attributes of the ListView widget in this example:
children (List<Widget>):
- Specifies the list of widgets to be displayed in the ListView.
scrollDirection (Axis, default: Axis.vertical):
- Determines the scroll direction of the list, either vertical or horizontal.
shrinkWrap (bool, default: false):
- Specifies whether the ListView should shrink-wrap its content or take up all available space.
padding (EdgeInsets):
- Adds padding around the entire ListView.
itemExtent (double):
- Forces the children of the ListView to have a specific extent (height for vertical, width for horizontal).
physics (ScrollPhysics):
- Defines the physics of the scrolling behavior, such as bouncing or clamping.
controller (ScrollController):
- Provides a controller for interacting with the scroll position and controlling the ListView programmatically.
separatorBuilder (IndexedWidgetBuilder):
- Defines a separator between consecutive list items.
cacheExtent (double):
- Sets the maximum distance in pixels outside the ListView that the framework should cache.
addAutomaticKeepAlives (bool):
- Enables automatic management of keeping items alive when they are scrolled out of view.
Here's an example of a basic ListView with some common properties:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ListView Example'),
),
body: ListView(
// Example 1: Basic List of Text Widgets
children: <Widget>[
ListTile(
leading: Icon(Icons.star),
title: Text('Item 1'),
subtitle: Text('Subtitle for Item 1'),
onTap: () {
// Handle item tap
},
),
ListTile(
leading: Icon(Icons.star),
title: Text('Item 2'),
subtitle: Text('Subtitle for Item 2'),
onTap: () {
// Handle item tap
},
),
// ... add more list items as needed
],
// Example 2: List Builder for Dynamic Content
// children: List.generate(
// 10,
// (index) => ListTile(
// title: Text('Item $index'),
// ),
// ),
),
);
}
}
Subscribe to my newsletter
Read articles from Vinit Mepani directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Vinit Mepani
Vinit Mepani
"Hello World, I'm Vinit Mepani, a coding virtuoso driven by passion, fueled by curiosity, and always poised to conquer challenges. Picture me as a digital explorer, navigating through the vast realms of code, forever in pursuit of innovation. In the enchanting kingdom of algorithms and syntax, I wield my keyboard as a magical wand, casting spells of logic and crafting solutions to digital enigmas. With each line of code, I embark on an odyssey of learning, embracing the ever-evolving landscape of technology. Eager to decode the secrets of the programming universe, I see challenges not as obstacles but as thrilling quests, opportunities to push boundaries and uncover new dimensions in the realm of possibilities. In this symphony of zeros and ones, I am Vinit Mepani, a coder by passion, an adventurer in the digital wilderness, and a seeker of knowledge in the enchanting world of code. Join me on this quest, and let's create digital wonders together!"