简体   繁体   English

为什么GridView.builder返回高度扩展的元素,而ListView.Seperated返回高度正常的元素?

[英]Why does the GridView.builder returns elements expanded in height whereas the ListView.Seperated returns the element with normal height?

I wanted to use the grid view for the landscape mode. 我想将网格视图用于横向模式。 So i used GridView but all the Grid elements are expanded vertically. 所以我使用了GridView,但是所有Grid元素都是垂直扩展的。

I even tried for the ListView.builder using GridView.builder setting the crossAxisCount to 1 to make the same effect as the ListView.builder but there also all the items are expanded vertically. 我什至尝试使用GridView.builder将ListAxis.builder设置为crossAxisCount到1,以产生与ListView.builder相同的效果,但是所有项目都垂直展开。

Widget buildTopNews(BuildContext context) {
  return Container(
    padding: EdgeInsets.only(bottom: 20),
    child: MediaQuery.of(context).orientation == Orientation.portrait
        ? ListView.separated(
            shrinkWrap: true,
            physics: const NeverScrollableScrollPhysics(),
            itemBuilder: (BuildContext context, int index) {
              return NewsCard(
                news: topNews[index],
              );
            },
            itemCount: topNews.length,
            separatorBuilder: (BuildContext context, int index) {
              return SizedBox(
                height: 20,
              );
            },
          )
        : GridView.builder(
            physics: NeverScrollableScrollPhysics(),
            shrinkWrap: true,
            itemCount: topNews.length,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              mainAxisSpacing: 20,
            ),
            itemBuilder: (BuildContext context, int index) {
              return NewsCard(
                news: topNews[index],
              );
            },
          ),
  );
}

Gridview has an aspect ratio for its children. Gridview为其子级提供长宽比。 So that's why your items are having the same height as its width(width is set out first). 这就是为什么您的商品的高度与其宽度相同(首先列出宽度)的原因。 Change the aspect ratio value in your gridview to get the desired height. 在gridview中更改纵横比值以获取所需的高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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