Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter. How to add Padding for SliverGrid

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.

like image 744
testivanivan Avatar asked Dec 10 '25 05:12

testivanivan


1 Answers

You can use the SliverPadding class:

SliverPadding(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        sliver: SliverGrid.count(
          ...
        ),
      ),
like image 180
Alexander Krol Avatar answered Dec 12 '25 21:12

Alexander Krol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!