Using ListView and GridView in Flutter
When building modern mobile apps with Flutter, displaying lists and grids of data is a common requirement. Flutter offers two essential widgets to handle such UI patterns — ListView and GridView. Both are highly flexible and efficient, and they help developers create scrollable, dynamic interfaces quickly. In this blog, we’ll explore how to use both ListView and GridView in your Flutter applications with examples and best practices.
What is ListView in Flutter?
ListView is a scrollable list of widgets arranged linearly, either vertically or horizontally. It is perfect when you want to display a simple, vertically scrolling list of items such as messages, news headlines, or menus.
Types of ListView
ListView() – For a fixed small number of children.
ListView.builder() – Best for large or infinite lists. It builds items lazily.
ListView.separated() – Allows adding a separator widget between list items.
ListView.custom() – Offers full control, but is used less commonly.
Example: Using ListView.builder()
dart
ListView.builder(
itemCount: 10,
itemBuilder: (context, index) {
return ListTile(
leading: Icon(Icons.person),
title: Text('Person ${index + 1}'),
);
},
)
This code creates a scrollable list of tiles with a leading icon and a title.
What is GridView in Flutter?
GridView is used to create a 2D array of widgets. It's ideal for image galleries, product catalogs, or any layout that requires multiple items per row.
Types of GridView
GridView.count() – Simple way to create a grid with a fixed number of columns.
GridView.builder() – Builds grid items lazily, perfect for large lists.
GridView.extent() – Creates grid with maximum cross-axis extent.
GridView.custom() – Advanced usage for full customization.
Example: Using GridView.count()
dart
GridView.count(
crossAxisCount: 2,
padding: EdgeInsets.all(10),
children: List.generate(6, (index) {
return Card(
child: Center(child: Text('Item ${index + 1}')),
);
}),
)
This example creates a grid with two columns and six cards.
ListView vs GridView: When to Use What?
Feature ListView GridView
Layout Single column or row Multiple rows and columns
Use case Chats, notifications Galleries, e-commerce
Scrolling Vertical/Horizontal Vertical/Horizontal
If your content fits into a linear format, ListView is the better choice. But for tiled or matrix-style content, GridView is ideal.
Performance Tips
Use .builder() constructors when working with large data sets.
Wrap your list or grid in an Expanded or Flexible widget inside a Column to avoid overflow.
Use const constructors where possible to reduce widget rebuilds.
Conclusion
Both ListView and GridView are foundational widgets in Flutter, enabling developers to design rich and dynamic UIs. With just a few lines of code, you can create efficient, scrollable layouts that adapt well to different screen sizes. By understanding when and how to use them effectively, you’ll be well on your way to building powerful mobile experiences with Flutter.
Learn : Master Flutter Development with Expert Training
Read More: Flutter vs React Native: Which One to Choose?
Read More: Flutter Forms: Validation and Input Handling
Visit our IHUB Talent training in Hyderabad
Get Direction
Comments
Post a Comment