简体   繁体   中英

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.

Instead of using Container try to use Flexible widget.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM