Mark Dionnie
Mark Dionnie

Reputation: 435

How to set width on Flutter SliverGrid

I'm trying display a slivergridview view and it just doesn't look good if it's always on full width especially on large screens

enter image description here

But CustomScrollView only accepts sliver widgets, which makes container & sizedbox unusable. What is your workaround on this one?

Upvotes: 4

Views: 1102

Answers (1)

Mark Dionnie
Mark Dionnie

Reputation: 435

  1. First, wrap your Scaffold body with LayoutBuilder
  2. Wrap SliverGrid with SliverPadding and set the vertical and horizontal width
    SliverPadding(
      padding: EdgeInsets.symmetric(
      vertical: 15,
      horizontal: max((constraints.maxWidth - 1200) / 2, 0) > 15 ? max((constraints.maxWidth - 1200) / 2, 0) : 15),
      sliver: SliverGrid( ...

Here's the result enter image description here

Upvotes: 6

Related Questions