1

I want to create a shopping cart app. I used a Sliver grid having different items with different heights but it's not working. Please help

grid in flutter

Amon Chowdhury
  • 101
  • 1
  • 5

1 Answers1

1

This is not an answer to your particular problem but it may help anyone who just wants to resize the height of all SliverGrid items.

I had a similar problem and went to view the docs here. There is no explicit way to declare the height but I used childAspectRatio property of the SliverGridDelegate. A ratio of 1 means the sliver item retains its preset height. Any ratio above 1 reduces the height. In order to increase the height, I used a decimal ratio.

childAspectRatio: 0.5,

Here is the complete code of the SliverGrid and its delegate as well as a simple render of placeholder items:

SliverGrid(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          childAspectRatio: 0.5,
        ),
        delegate: SliverChildBuilderDelegate(
              (BuildContext context, int index) {
            return Card(
              child: Text(
                  "Item $index"
              ),
            );
          },
          childCount: 10,
        ),
      )

Please note that this will change the height of all grid items.

Kenneth Murerwa
  • 778
  • 12
  • 16