gpt4 book ai didi

Flutter GridView.count 在行布局错误中

转载 作者:行者123 更新时间:2023-12-04 11:27:26 25 4
gpt4 key购买 nike

我是flutter的新手,我想连续绘制一个图像网格。我用了这个例子 Flutter - Layout a Grid但每次出现错误时它都会显示给我

Widget buildView(){
return new Container(
color: Colors.white,
child:
new Padding(
padding: EdgeInsets.all(8.0),
child: new Column(
children: <Widget>[
new Row(
children: <Widget>[
Flexible(
child: new GridView.count(
crossAxisCount: 4,
childAspectRatio: 1.0,
padding: const EdgeInsets.all(4.0),
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
children: <String>[
'https://via.placeholder.com/150',
'https://via.placeholder.com/150',
'https://via.placeholder.com/150',
'https://via.placeholder.com/150',
'https://via.placeholder.com/150',
'https://via.placeholder.com/150',

].map((String url) {
return new GridTile(
child: new Image.network(url, fit: BoxFit.cover));
}).toList()),
),
],
),
],
),
],
),
),
);
}

每次出现此错误“垂直视口(viewport)被赋予无限高度”时,我都会编码

最佳答案

除非您尝试分割行,否则您不需要将 GridView 包装在 Row 中。您可以只使用 GridView。

new GridView.count(
crossAxisCount: 4,
childAspectRatio: 1.0,
padding: const EdgeInsets.all(4.0),
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
children: <String>[
'https://via.placeholder.com/150',
'https://via.placeholder.com/150',
'https://via.placeholder.com/150',
'https://via.placeholder.com/150',
'https://via.placeholder.com/150',
'https://via.placeholder.com/150',

].map((String url) {
return new GridTile(
child: new Image.network(url, fit: BoxFit.cover));
}).toList()),

如果你真的想把它包裹在一行中,你需要用一个容器包裹 GridView 并定义一个宽度或包裹在一个灵活的
Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Flexible(
child: new GridView.count(
crossAxisCount: 4,
childAspectRatio: 1.0,
padding: const EdgeInsets.all(4.0),
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
children: <String>[
'https://via.placeholder.com/150',
'https://via.placeholder.com/150',
'https://via.placeholder.com/150',
'https://via.placeholder.com/150',
'https://via.placeholder.com/150',
'https://via.placeholder.com/150',

].map((String url) {
return new GridTile(
child: new Image.network(url, fit: BoxFit.cover));
}).toList()),
),
],
),

如果你得到无限的高度错误,只需用容器或 sizedbox 包裹它并定义一个高度

关于Flutter GridView.count 在行布局错误中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56351394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com