如何避免 GridView 中颤振列的底部溢出
虽然我已经了解了颤振列和行的工作原理,但我无法解决由一个图像和两个文本小部件组成的列中的以下底部溢出问题。
小部件排列在网格中,应该如下所示,但具有更大的图像:
但是一旦我增加图像圆圈的半径,就会出现底部溢出,如下所示:
这是抛出错误的小部件的代码:
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(top: 5, bottom: 5),
color: Colors.transparent,
child: Column(children: [
InkWell(
child: CircleAvatar(
backgroundImage: CachedNetworkImageProvider(ingredient.imgSrc,
imageRenderMethodForWeb: ImageRenderMethodForWeb.HttpGet),
radius: 34,
),
onTap: widget.onTap,
),
Text(
ingredient.name,
style: TextStyle(color: textColor),
),
Text(
"g",
style: TextStyle(
color: textColor,
),
),
]));
}
这是包含图块的父网格小部件:
Card( margin: EdgeInsets.all(10),
child: new GridView.count(
// primary: true,
// padding: const EdgeInsets.all(0),
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
crossAxisCount: 3,
mainAxisSpacing: 3,
padding: EdgeInsets.all(2),
children: [
...getAllIngredientTiles(), // here I assign the widget list
Padding(
child: SizedBox(
width: 16,
height: 16,
child: ElevatedButton(
onPressed: _openAddIngredientScreen,
child: Icon(
Icons.add,
size: 36,
color: Colors.white,
),
style: ButtonStyle(
shape: MaterialStateProperty.all(CircleBorder()),
padding:
MaterialStateProperty.all(EdgeInsets.all(2)),
backgroundColor: MaterialStateProperty.all(
Colors.teal), // <-- Button color,
),
)),
padding: EdgeInsets.only(
left: 22, right: 22, bottom: 22, top: 0)),
//
],
))
我尝试过:
- 用容器包裹列并手动设置高度
- 将每个小部件包裹在里面这具有固定高度的容器/大小框的列
- 用扩展的包裹
无论我做什么,我都无法使列增加其高度并使元素适合它。
我读了很多其他相关问题,但答案对我不起作用。我将非常感谢任何允许我保留网格并增加图像尺寸而不溢出的建议。
I though I had understood how flutter columns and rows work but I am unable to resolve the following bottom overflow in my column consisting of an Image- and two Textwidgets.
The widgets are arranged in a grid and should look like this but with bigger images:
But once I increase the radius of the image circles I get bottom overflow like here:
This is the code of the widget with throw the errors:
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(top: 5, bottom: 5),
color: Colors.transparent,
child: Column(children: [
InkWell(
child: CircleAvatar(
backgroundImage: CachedNetworkImageProvider(ingredient.imgSrc,
imageRenderMethodForWeb: ImageRenderMethodForWeb.HttpGet),
radius: 34,
),
onTap: widget.onTap,
),
Text(
ingredient.name,
style: TextStyle(color: textColor),
),
Text(
"g",
style: TextStyle(
color: textColor,
),
),
]));
}
And this is the parent grid widget which contains the tiles:
Card( margin: EdgeInsets.all(10),
child: new GridView.count(
// primary: true,
// padding: const EdgeInsets.all(0),
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
crossAxisCount: 3,
mainAxisSpacing: 3,
padding: EdgeInsets.all(2),
children: [
...getAllIngredientTiles(), // here I assign the widget list
Padding(
child: SizedBox(
width: 16,
height: 16,
child: ElevatedButton(
onPressed: _openAddIngredientScreen,
child: Icon(
Icons.add,
size: 36,
color: Colors.white,
),
style: ButtonStyle(
shape: MaterialStateProperty.all(CircleBorder()),
padding:
MaterialStateProperty.all(EdgeInsets.all(2)),
backgroundColor: MaterialStateProperty.all(
Colors.teal), // <-- Button color,
),
)),
padding: EdgeInsets.only(
left: 22, right: 22, bottom: 22, top: 0)),
//
],
))
What I tried:
- Wrapping the Column with a Container and set height manually
- Wrapping each of the widgets inside the Column with Containers/SizedBoxes with fixed height
- Wrapping with Expanded
Whatever I do I cant make the column increase its height and make the elements fit into it.
I read a lot of other related questions but the answers did not work for me. I would be very thankful for any suggestions which allow me to keep the grid and increase the image sizes without overflow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了一种方法来实现我想要的。
为了增加图块的高度,我使用了 SliverGridDelegateWithFixedCrossAxisCount 的 mainAxisExtent 属性:
这是我增加图块高度并防止底部溢出的唯一有效方法。现在看起来应该是这样的:
I found a way to achieve what I want.
In order to increase the height of the tiles I made use of the
mainAxisExtent
property of SliverGridDelegateWithFixedCrossAxisCount:It was the only working way for me to increase the tile height and prevent bottom overflow. Now it looks as it should:
尝试下面的代码希望对您有帮助。只需将您的小部件更改为我的小部件即可。
您的结果屏幕->
Try below code hope its help to you.Just chnage your widget to my widget.
Your result screen->