Giulia
Giulia

Reputation: 807

How to set the height of the Container of GridView to "auto" in Flutter

A recurring issue I have in my Flutter learning journey, is that GridView does require a parent container with a specified height.

What is the best practice to maximize the height so that it stretches out as much as needed without always incurring in overflow issues or unpleasant UI?

Here are a few methods I tried:

double.infinity - This one doesn't work properly, GridView doesn't show at all.

Container(
  height: double.infinity,
  child: GridView.builder(
    ....
)

MediaQuery.of(context).size.height - This one doesn't work properly either, overflow issues occur and just doesn't work completely when other elements are displayed (for example in a column).

Container(
  height: MediaQuery.of(context).size.height,
  child: GridView.builder(
    ....
)

fixed size - This one doesn't allow any flexibility at all and isn't ideal for responsiveness too.

Container(
  height: 800,
  child: GridView.builder(
    ....
)

Thank you.

Upvotes: 1

Views: 1278

Answers (1)

Avinash
Avinash

Reputation: 597

Instead of using Container try to use Flexible widget.

Upvotes: 3

Related Questions