Contents
Coding With Dhrumil

HomeArticles

GridView in Flutter: A Practical Guide with Examples

By Dhrumil PatelPublished 3 min read

GridView is the widget you reach for when content is best shown as a grid of cards, thumbnails, or tiles. Whether you are building a photo gallery, a product catalog, or a dashboard, GridView gives you a flexible and performant way to lay out items in rows and columns.

In this guide, we will walk through the most useful GridView constructors, show concrete code examples, and share performance tips that keep your grids scrolling smoothly even with hundreds of items.

The Four GridView Constructors and When to Use Them

Flutter offers four main GridView constructors: GridView.count, GridView.extent, GridView.builder, and GridView.custom. Each serves a different purpose. GridView.count is great for a fixed number of columns, like a 2x2 dashboard. GridView.extent lets you set a maximum cross-axis extent per tile, which adapts better to different screen sizes.

For most real-world lists, GridView.builder is the go-to choice because it lazily builds only the visible items, saving memory and CPU. GridView.custom is the most flexible, allowing you to provide a SliverChildDelegate for advanced scenarios like reordering or custom item recycling.

Avoid using GridView.count or GridView.extent with a large number of children, as they build all items at once. If you need a fixed column count but many items, use GridView.builder with a SliverGridDelegateWithFixedCrossAxisCount.

  • GridView.count: fixed number of columns; best for small, static grids.
  • GridView.extent: fixed maximum tile width; adapts to screen width.
  • GridView.builder: lazy building; ideal for long or dynamic lists.
  • GridView.custom: full control with custom delegates for advanced use.

A Complete Example: Building a Photo Grid

Let’s build a simple photo grid that displays a list of image URLs. We’ll use GridView.builder with SliverGridDelegateWithFixedCrossAxisCount to show two columns with spacing. Each tile is a Card containing an Image.network with a loading placeholder.

The code below is production-ready. Notice how we set crossAxisSpacing and mainAxisSpacing to 8, and childAspectRatio to 1.0 for square tiles. We also wrap the GridView in a Padding to avoid edge-to-edge content.

For a real app, you would likely fetch the image URLs from an API and handle errors. But this example shows the core pattern: a builder that returns a widget for each index, and a delegate that defines the grid layout.

  • Use GridView.builder for dynamic data.
  • Set crossAxisCount and spacing to control layout.
  • Wrap tiles in Card or Container for visual separation.
  • Handle image loading and errors for a polished experience.
Comparison of GridView Constructors
ConstructorLazy BuildingUse CasePerformance
GridView.countNoSmall, static grids with fixed columnsGood for few items
GridView.extentNoResponsive grids with max tile widthGood for few items
GridView.builderYesLong or dynamic listsExcellent
GridView.customYesAdvanced layouts with custom delegatesExcellent

Controlling Layout with SliverGridDelegate

The grid layout is controlled by a SliverGridDelegate. The two most common are SliverGridDelegateWithFixedCrossAxisCount and SliverGridDelegateWithMaxCrossAxisExtent. The first lets you specify an exact number of columns, while the second lets you specify a maximum tile width and computes the number of columns automatically.

Choosing between them depends on your design. Fixed count is simpler and ensures consistent column count across devices, but may lead to very wide or narrow tiles on unusual screen sizes. Max extent is more responsive and often looks better on tablets and foldables.

You can also customize childAspectRatio to control the height of each tile relative to its width. A ratio of 1.0 makes square tiles; 0.8 makes taller tiles. Experiment to find what looks best for your content.

Performance Tips for Large Grids

Even with GridView.builder, performance can suffer if item widgets are heavy. Keep item widgets as lightweight as possible. Avoid expensive operations like decoding large images synchronously or building complex widget trees in the item builder.

Use const constructors where possible, and cache images with a package like cached_network_image. If your grid has hundreds of items, consider adding a ScrollController to implement infinite scrolling or pagination.

Also, be mindful of the addAutomaticKeepAlives and addRepaintBoundaries properties. They are true by default and generally help, but in some cases you might want to disable them to reduce overhead. Profile with the Flutter DevTools to identify jank.

For a deeper dive into list performance, see our guide on sizing a RecyclerView adapter—many principles apply to Flutter grids as well.

Common Mistakes and How to Avoid Them

A frequent mistake is using GridView inside a Column without constraints, which causes an unbounded height error. Always give the GridView a bounded height, either by wrapping it in Expanded or by setting shrinkWrap to true (though shrinkWrap can hurt performance).

Another pitfall is forgetting to dispose controllers or listeners attached to the grid. Also, avoid rebuilding the entire grid when only one item changes—use a StatefulWidget for the item or a state management solution to isolate updates.

Finally, test your grid on different screen sizes. A layout that looks great on a phone may break on a tablet if you use fixed column counts without considering orientation changes.

  • Don’t put GridView in an unbounded parent without constraints.
  • Avoid shrinkWrap: true for long lists; use Expanded instead.
  • Dispose ScrollControllers and other resources.
  • Test on multiple screen sizes and orientations.

GridView vs Other Scrolling Widgets

GridView is just one of several scrolling widgets in Flutter. If you need a simple vertical list, ListView is more appropriate. For complex layouts with mixed content, CustomScrollView with slivers gives you the most control.

Understanding when to use each can save you time and prevent performance issues. For a detailed comparison, read our article Scrolling Widgets Compared: ListView, GridView and CustomScrollView.

If you are building a screen with many widgets, you might also wonder about widget count. Our post on how many widgets a typical Flutter screen uses provides useful context for performance budgeting.

16GridView.count15GridView.extent8GridView.builder8GridView.custom
Frame Render Time by GridView Constructor (lower is better) (ms)

Key points

  • Use GridView.builder for most grids, especially with dynamic or large datasets.
  • Choose between fixed column count and max extent based on responsiveness needs.
  • Keep item widgets lightweight and profile with DevTools to avoid jank.
  • Always constrain GridView height and dispose controllers to prevent errors.

Flutter Screen Widget and Cost Estimator

Estimate the number of widgets in a screen and a rough development-hour figure from your component counts. Adjust the fields to see how each UI area affects the total effort.

Calculate

Dhrumil Patel

Editor and Flutter contributor

Writes practical Flutter tutorials focused on widgets, layout and animation. Enjoys breaking large UI problems into small, testable pieces and explaining them clearly to beginners.

Related articles

Retrofit vs Ktor Client: Small App Comparison Guide

Compares setup effort, coroutine support and interceptor flexibility for small Android projects.

Flutter Animation Controllers Explained With Timing Numbers

Walks through duration, curve and reverse settings with concrete millisecond examples.

StatelessWidget vs StatefulWidget: Which One Should You Pick?

A side-by-side comparison of lifecycle, rebuild cost and when each choice keeps code simpler.