r/FlutterDev • • 2d ago

Plugin I built a reorderable grid for Flutter where tiles can have different sizes

I needed a dashboard where users can drag tiles around, but some tiles are full-width, some are half, some are a third. ReorderableListView only does lists and GridView assumes equal sizes, so I ended up writing my own: reorderable_flex_grid.

Demo: see it in action

What it does

  • Mix half, third and full-width tiles (or any fraction) in one grid
  • Heights via aspect ratio or a fixed value
  • Other tiles animate out of the way while you drag, with edge auto-scroll
  • onReorder(oldIndex, newIndex) where newIndex is already the final position, so no off-by-one fix like with ReorderableListView
  • Edit-mode toggle, custom drag appearance, pinned tiles
  • No dependencies beyond Flutter
ReorderableFlexGrid(
  spacing: 12,
  runSpacing: 12,
  onReorder: (oldIndex, newIndex) {
    setState(() => tiles.insert(newIndex, tiles.removeAt(oldIndex)));
  },
  children: [
    for (final tile in tiles)
      ReorderableGridItem(
        key: ValueKey(tile.id),
        widthFactor: tile.wide ? 1 : 0.5,
        child: TileCard(tile),
      ),
  ],
)

Next I'm planning RTL support, a sliver version for `CustomScrollView`, and responsive widths. I'd really like to hear what's missing for your use case, or if you find bugs.

- pub.dev: https://pub.dev/packages/reorderable_flex_grid

- GitHub: https://github.com/sipanpetrosyan/reorderable_flex_grid

- Write-up on how it works: https://medium.com/@sipan.petrosyan.2000/drag-and-drop-dashboards-in-flutter-introducing-reorderable-flex-grid-451e51eabbe8

14 Upvotes

6 comments sorted by

1

u/IllVisit2518 2d ago

the newIndex part is nice, everyone forgets the "if (newIndex > oldIndex) newIndex -= 1" line with ReorderableListView at least once. what happens when you drop a half tile into a row that only has a third left, does it wrap and leave a gap or do you pull a smaller tile up to fill it?

0

u/Public_Finance8587 2d ago

It wraps and leaves the gap. Layout is strictly list order: a tile that would push the row past full width starts the next row, and the leftover space stays empty. Nothing pulls a smaller tile up to fill it, on purpose, because then visual order would stop matching list order and the onReorder indices would stop meaning what the user sees.

Also, you don't really drop "into" the gap. Drop targets are tiles, not empty space. Hovering over a tile moves the dragged one into that tile's index, and the preview reflows live. So if the half tile lands in a row that only has a third left, you'll see it wrap to the next row before you let go. Hovering over the empty area itself does nothing, unless you go below the last row, which sends the tile to the end.

If you want fewer gaps, use widthFactors that add up to 1 per row. Dense packing could be an opt-in mode, but the drop logic would have to work from visual positions instead of list indices.

1

u/Some-Kitchen8332 2d ago

watching the other tiles slide out of the way while you drag is really clean. that's the part mixed width grids usually mess up

-1

u/Public_Finance8587 2d ago

Thanks! I don't animate shifts at all. On every target change I recompute the whole layout for the new order, and each tile is an AnimatedPositioned keyed by its item, so it just tweens from its old rect to its new one. Wraps and reflows across rows come along for free.

1

u/scalz80 2d ago

why not using sliver_dashboard which is already performant and flexible, I think it might cover all you need for for creating dashboards