我想在横向模式下使用 GridView 。所以我使用了 GridView,但所有的 Grid 元素都是垂直展开的。
我什至尝试使用 GridView.builder 的 ListView.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 的 subview 有纵横比。这就是为什么您的项目具有与其宽度相同的高度(首先设置宽度)。更改 gridview 中的纵横比值以获得所需的高度。
关于listview - 为什么 GridView.builder 返回高度扩展的元素,而 ListView.Seperated 返回正常高度的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56398004/