Overview
GridView is a scrollable, 2D array of controls arranged in a grid pattern. It is very effective for large lists with thousands of items and provides smooth scrolling performance. GridView is preferred over wrapping Column or Row controls when you need efficient scrolling with many items.Basic Example
Class Definition
Properties
controls
A list of controls to display inside the grid. Controls are arranged in grid cells from left to right, top to bottom (or according to the scroll direction).
horizontal
Whether to layout the grid items horizontally.
False(default) - Vertical scrolling grid (scrolls top to bottom)True- Horizontal scrolling grid (scrolls left to right)
reverse
Whether the scroll view scrolls in the reading direction.
-
If the reading direction is left-to-right and
horizontalisTrue, the scroll view scrolls from left to right whenreverseisFalseand from right to left whenreverseisTrue. -
If
horizontalisFalse, the scroll view scrolls from top to bottom whenreverseisFalseand from bottom to top whenreverseisTrue.
runs_count
The number of children in the cross axis.
- In vertical mode (default): This is the number of columns
- In horizontal mode: This is the number of rows
runs_count=3 in vertical mode creates a 3-column grid.max_extent
The maximum width or height of a grid item.When set, the grid automatically calculates the number of runs (columns or rows) based on the available space and this maximum extent, overriding
runs_count.This is useful for responsive layouts where the number of columns should adapt to screen size.spacing
The number of logical pixels between each child along the main axis (scroll direction).
- In vertical mode: Vertical spacing between rows
- In horizontal mode: Horizontal spacing between columns
run_spacing
The number of logical pixels between each child along the cross axis.
- In vertical mode: Horizontal spacing between columns
- In horizontal mode: Vertical spacing between rows
child_aspect_ratio
The ratio of the cross-axis to the main-axis extent of each child.
1.0- Square cells2.0- Cells are twice as wide as they are tall (in vertical mode)0.5- Cells are half as wide as they are tall (in vertical mode)
padding
The amount of space by which to inset the children.Can be a single number for uniform padding, or use
ft.padding.all(), ft.padding.symmetric(), or ft.padding.only() for more control.clip_behavior
The content will be clipped (or not) according to this option.Options:
ClipBehavior.HARD_EDGE- Clip with hard edgesClipBehavior.ANTI_ALIAS- Clip with anti-aliasingClipBehavior.ANTI_ALIAS_WITH_SAVE_LAYER- Clip with anti-aliasing and save layerClipBehavior.NONE- No clipping
semantic_child_count
The number of children that will contribute semantic information for accessibility.Useful when the actual number of children is different from the semantic count.
cache_extent
Items that fall in the cache area (before or after the visible area) are laid out even though they are not yet visible on screen.The
cache_extent describes how many pixels the cache area extends before the leading edge and after the trailing edge of the viewport.The total extent covered is: cache_extent before + extent of main axis + cache_extent after.A larger cache extent improves scrolling smoothness but uses more memory.build_controls_on_demand
Whether the controls should be built lazily/on-demand.When
True (default), controls are only rendered when they’re about to become visible. This dramatically improves performance when dealing with a large number of controls.Layout Behavior
How Children Are Arranged
-
Grid Pattern: Children are arranged in a grid with
runs_countcolumns (in vertical mode) or rows (in horizontal mode). - Main Axis (Scroll Direction): Items scroll along this axis (vertical by default).
-
Cross Axis: Items are arranged across this axis according to
runs_count. -
Cell Size: Each cell’s size is determined by:
- Available space divided by
runs_count - Adjusted by
child_aspect_ratio - Or limited by
max_extentif set
- Available space divided by
-
Spacing:
spacingcontrols main-axis gaps,run_spacingcontrols cross-axis gaps.
Scrolling
GridView inherits fromScrollableControl and is scrollable by default:
Examples
Basic 3-Column Grid
Image Gallery
Responsive Grid with max_extent
Product Grid
Horizontal Scrolling Grid
Large Grid with Lazy Loading
Card Grid with Different Aspect Ratios
Performance Optimization
For Large Grids
When working with thousands of items:- Enable lazy rendering: Keep
build_controls_on_demand=True(default) - Adjust cache extent: Use smaller values to reduce memory usage
- Optimize child widgets: Keep child controls simple and lightweight
Use Cases
- Photo Galleries: Display image collections in a grid
- Product Catalogs: Show products with images and details
- App Launchers: Create app icon grids
- Dashboard Widgets: Arrange dashboard cards in a grid
- Icon Pickers: Display selectable icons
- Calendar Views: Create calendar-style layouts
- Tile-Based Games: Build game boards with grid cells
Comparison with Other Layouts
| Feature | GridView | ListView | Column with wrap |
|---|---|---|---|
| Scrolling | Yes | Yes | No |
| Grid layout | Yes | No | Yes |
| Performance (1000s items) | Excellent | Excellent | Poor |
| Lazy rendering | Yes | Yes | No |
| Responsive columns | Yes (max_extent) | N/A | Yes |