Flutter. How to add Padding for SliverGrid

Issue

I use CustomScrollView with SliverGrid. I add space between grid items using this:
mainAxisSpacing: 8, crossAxisSpacing: 8.
But is it possible to add space (margin/padding) before the first and after the last column.
I tried to wrap my SliverGrid in Padding, but I get the error:

A RenderViewport expected a child of type RenderSliver but received a
child of type RenderPadding.

RenderObjects expect specific types of children because they
coordinate with their children during layout and paint. For example, a
RenderSliver cannot be the child of a RenderBox because a RenderSliver
does not understand

Please, help me how to add start and end margin of my GridView.

Solution

You can use the SliverPadding class:

SliverPadding(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        sliver: SliverGrid.count(
          ...
        ),
      ),

Answered By – Alexander Krol

Answer Checked By – Cary Denson (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *