DEV Community

Cover image for staggered_grid_layout: masonry and quilted grids for Flutter that don't jump
Abdeldjalil Chougui
Abdeldjalil Chougui

Posted on Originally published at abdeldjalilchougui.biz

staggered_grid_layout: masonry and quilted grids for Flutter that don't jump

A masonry grid, the Pinterest layout, is easy to describe: several columns, items of different heights, and each new item goes into the shortest column. It is harder to scroll. An item's position depends on every item before it, so when the user scrolls down and then back up, the grid has to put each item exactly where it was. If it recomputes instead, items change column, the content jumps under the finger, and two columns of equal height flicker.

Data changes add more cases: an image finishes loading and its tile grows, an item is inserted, the column count changes on rotation.

staggered_grid_layout is a Flutter package with five grid layouts: masonry, aligned rows, quilted, woven and staggered. The scrolling ones are built on real slivers, layouts are cached by index so scrolling back up does not jump, and every layout supports RTL and both scroll axes.

What it does

Masonry. SliverMasonryGrid (.count, .extent) and MasonryGridView (.count, .extent, .builder, .custom). Children are built lazily and size themselves along the main axis.

Aligned rows. SliverAlignedGrid and AlignedGridView. Children are built lazily and stretched to the tallest child of their row, which is what you want for product cards with different text lengths.

Quilted. SliverQuiltedGrid and SliverQuiltedGridDelegate. Tiles follow a repeating pattern of sizes such as 2x2, 1x1, 1x2.

Woven. SliverWovenGrid and SliverWovenGridDelegate. Tiles alternate aspect ratios and alignments by pattern.

Staggered. StaggeredGrid with StaggeredGridTile.count, .extent and .fit. This one does not scroll and builds all its children.

The parts that matter under the surface:

  • Real slivers. Masonry and aligned grids are RenderSliverMultiBoxAdaptor slivers. They build only the children that are visible or inside the cache extent, dispose the others, report an exact SliverGeometry once every child has been measured, and support keep-alive. Quilted and woven grids are SliverGridDelegates for Flutter's own RenderSliverGrid.
  • No jumps when scrolling up. Each masonry placement (column, offset, extent) is cached by index, so scrolling back up reuses the layout computed on the way down. When a child changes size it keeps its column. A child that starts above the viewport grows upward and the scroll offset is corrected, so what is on screen stays put. Aligned grids cache row extents the same way.
  • Deterministic placement. A child goes into the column whose content ends first. Ties go to the lowest column, even with floating-point noise.
  • Data changes without crashes. Insert, remove and reorder, with or without findChildIndexCallback, and change the column count while scrolled. The first visible child stays on screen.
  • GridView parity. mainAxisSpacing, crossAxisSpacing, padding, addAutomaticKeepAlives, addRepaintBoundaries, addSemanticIndexes, cacheExtent, reverse, shrinkWrap, controller and physics.
  • RTL and both axes. Columns mirror under TextDirection.rtl, and every layout scrolls vertically or horizontally.
  • No dependencies besides Flutter.

How to use it

Add it from GitHub:

dependencies:
  staggered_grid_layout:
    git: https://github.com/AbdeldjalilChougui/staggered_grid_layout
Enter fullscreen mode Exit fullscreen mode
import 'package:staggered_grid_layout/staggered_grid_layout.dart';
Enter fullscreen mode Exit fullscreen mode

A masonry grid:

MasonryGridView.count(
  crossAxisCount: 3,
  mainAxisSpacing: 8,
  crossAxisSpacing: 8,
  padding: const EdgeInsets.all(8),
  itemCount: photos.length,
  itemBuilder: (context, index) => PhotoCard(photos[index]),
)
Enter fullscreen mode Exit fullscreen mode

The same layout as a sliver, next to an app bar:

CustomScrollView(
  slivers: [
    const SliverAppBar(title: Text('Feed')),
    SliverMasonryGrid.extent(
      maxCrossAxisExtent: 200,
      childCount: posts.length,
      itemBuilder: (context, index) => PostCard(posts[index]),
    ),
  ],
)
Enter fullscreen mode Exit fullscreen mode

Leave itemCount (or childCount) null for an endless feed. The grid grows until itemBuilder returns null.

A quilted grid with a mirrored repeating pattern:

CustomScrollView(
  slivers: [
    SliverQuiltedGrid.builder(
      crossAxisCount: 4,
      mainAxisSpacing: 4,
      crossAxisSpacing: 4,
      repeatPattern: QuiltedGridRepeatPattern.mirrored,
      pattern: const [
        QuiltedGridTile(2, 2), // mainAxisCount, crossAxisCount
        QuiltedGridTile(1, 1),
        QuiltedGridTile(1, 1),
        QuiltedGridTile(1, 2),
      ],
      itemCount: images.length,
      itemBuilder: (context, index) => ImageTile(images[index]),
    ),
  ],
)
Enter fullscreen mode Exit fullscreen mode

SliverQuiltedGridDelegate also works with GridView.custom and SliverGrid, and the README has examples for aligned, woven and staggered grids. The example/ app demos every layout and has an RTL switch.

Coming from flutter_staggered_grid_view

Most code migrates by changing the import. Class and constructor names are the same unless the README's table says otherwise. The differences worth knowing:

  • AlignedGridView.custom(gridDelegate:, itemBuilder:) is renamed AlignedGridView.builder, since it takes a builder.
  • StaggeredGrid.custom, StairedGridTile and SliverStairedGridDelegate are not provided. Custom quilted repeat patterns are not supported: QuiltedGridRepeatPattern is now an enum with same, inverted and mirrored.
  • SliverPatternGridDelegate becomes PatternGridLayout, which you return from your own SliverGridDelegate.getLayout.
  • Several widgets gain findChildIndexCallback and the keep-alive, repaint-boundary and semantic-index flags.

Behaviour changes:

  • Quilted placement. Each tile goes into the first free position, row by row, where it fits, in pattern order. Patterns like [1x1, 1x1, 2x2] with three columns no longer throw a RangeError: the wide tile moves to the next row. A pattern that does not fill a rectangle leaves holes, so design repeating patterns that do.
  • Pattern grids stop scrolling at their last child.
  • Masonry after a resize. A child that changes size keeps its column instead of reshuffling the grid. To get a fresh shortest-column layout, for example after replacing the whole data set, give the grid a new Key.

The README lists the open issues of flutter_staggered_grid_view it was checked against on 2026-09-24: jumps and jank when scrolling up, items changing column on resize, flicker with equal column heights, several masonry slivers in one scroll view, findChildIndexCallback on reorder, AlignedGridView without itemCount, the quilted RangeError, RTL and dynamic crossAxisCount. For each of these, a test in test/ reproduces the scenario. Two reports about assertion and null errors inside the masonry layout were not reproduced, because they have no minimal repro; the package uses a different algorithm with no nullable placement state.

Things to know

  • Jumps measure everything before them. Masonry and aligned grids measure every child before the scroll position on a jump: jumpTo(50000) builds and disposes each child up to that point once. Later scrolling reuses the cache.
  • A permanent resize above the viewport can leave a gap. When a masonry child that starts above the viewport changes size for good, the grid grows it upward and corrects the scroll offset. The other columns can then start with a gap at the very top of the grid. A child that changes size and back again, such as a placeholder replaced by an image of the same size, leaves no gap.
  • Placements are keyed by index, not by item identity. After an insert or a reorder, visible children take the column of their new index.
  • StaggeredGrid suits small grids. It builds and lays out all its children.
  • Not implemented: custom quilted repeat patterns and the staired layout of flutter_staggered_grid_view.

Why I built it

The README frames the package around a list of concrete problems in masonry grids: content that jumps when you scroll back up, items that move to another column when one of them resizes, flicker when two columns are equally tall, crashes on some quilted patterns, and missing RTL support. All of these come from the same place: a layout that is recomputed instead of remembered, and placement rules that are not deterministic.

So the design starts from two decisions. Cache every placement by index, and never move a child out of its column once it is placed. Then keep the public API close to the names people already use, so moving an existing screen over is mostly an import change, and write a test for each scenario so the behaviour stays fixed.

Get the code

The source, the full migration table, the tests and the example/ app are on GitHub: https://github.com/AbdeldjalilChougui/staggered_grid_layout. It is MIT licensed. Issues and pull requests are welcome.


Originally published at abdeldjalilchougui.biz.

Top comments (0)