首页 > 解决方案 > 颤振我正在使用gridview.count,但我无法设置高度

问题描述

我正在使用 GridView.count 和 list.generate。

我想设置图像的高度。

我尝试在需要的地方设置高度,但没有变化

看来我的代码很愚蠢。

这是一个低层次的问题,但如果你能回答它,我将不胜感激:)

SingleChildScrollView(
                      scrollDirection: Axis.vertical,
                      child: Container(
                        height: MediaQuery.of(context).size.height,
                        child: GridView.count(
                          mainAxisSpacing: 5.0,
                          crossAxisSpacing: 5.0,
                          crossAxisCount: 3,
                          children: List.generate(
                            snapshot.data!.length,
                            (index) => ClipRRect(
                              borderRadius: BorderRadius.circular(15.0),
                              child: snapshot.data![index].posterPath.isEmpty
                                  ? Container()
                                  : CachedNetworkImage(
                                      fit: BoxFit.cover,
                                      imageUrl:
                                          'https://image.tmdb.org/t/p/original/${snapshot.data![index].posterPath}',
                                    ),
                            ),
                          ),
                        ),
                      ),
                    );

标签: flutter

解决方案


尝试childAspectRatio使用GridView.

它是一个比率width / height

// As tall as it is wide
childAspectRatio:1

// 3 parts width for 2 parts height (1.5 times wider than it is tall) 
childAspectRatio:3/2

// 1 part width for 2 parts height (2 times taller than it is wide) 
childAspectRatio: 1/2


推荐阅读