0

I have used GridView.builder to show API data with below code.

GridView.builder(
    shrinkWrap: true,
    physics: ScrollPhysics(),
    itemCount: snapshot.data!.items.length,
    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
      crossAxisCount: 2,
      childAspectRatio: 1 / .3,
    ),
    itemBuilder: (context, index) {
      return InkWell(
        onTap: () {},
        child: CategoryItem(categoryItem: snapshot.data!.items[index],
        ),
      );
    },
  ),

Here Now my data length is 4 only. when it increases then data will be scrollable. And that working fine But thing is, I want to get resizable height with data length. It takes a default height which is not compatible.

Is there any way to reduce default height or customize it?

Thanks in advance.

Getting below output :

enter image description here

2
  • wrap it with container, you can set height container Commented Apr 26, 2022 at 1:37
  • tried with Container & Flexible also, but not working Commented Apr 26, 2022 at 7:21

1 Answer 1

0

As specified in this response, the key is childAspectRatio. The child height is reduced when you're increasing childAspectRatio.

For instance try to set childAspectRatio: 5

GridView.builder(
    shrinkWrap: true,
    physics: ScrollPhysics(),
    itemCount: snapshot.data!.items.length,
    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
      crossAxisCount: 2,
      childAspectRatio: 5,
    ),
    itemBuilder: (context, index) {
      return InkWell(
        onTap: () {},
        child: CategoryItem(categoryItem: snapshot.data!.items[index],
        ),
      );
    },
  ),
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.