扑。如何为 SliverGrid 添加 Padding

Flutter. How to add Padding for SliverGrid

我将 CustomScrollView 与 SliverGrid 一起使用。我使用以下方法在网格项之间添加 space: mainAxisSpacing:8,crossAxisSpacing:8。 但是是否可以在第一列之前和最后一列之后添加 space (margin/padding) 。 我试图将我的 SliverGrid 包装在 Padding 中,但出现错误:

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

请帮助我如何添加 GridView 的开始和结束边距。

您可以使用 SliverPadding class:

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