如何避免 GridView 中颤振列的底部溢出

发布于 2025-01-09 11:56:35 字数 2973 浏览 4 评论 0原文

虽然我已经了解了颤振列和行的工作原理,但我无法解决由一个图像和两个文本小部件组成的列中的以下底部溢出问题。

小部件排列在网格中,应该如下所示,但具有更大的图像:

在此处输入图像描述

但是一旦我增加图像圆圈的半径,就会出现底部溢出,如下所示:

在此处输入图像描述

这是抛出错误的小部件的代码:

  @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:

enter image description here

But once I increase the radius of the image circles I get bottom overflow like here:

enter image description 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光下的泡沫是彩色的 2025-01-16 11:56:35

我找到了一种方法来实现我想要的。
为了增加图块的高度,我使用了 SliverGridDelegateWithFixedCrossAxisCount 的 mainAxisExtent 属性:

GridView.builder(
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 3,
                    mainAxisExtent: 150 // <-- this works!
                    ...
                )

这是我增加图块高度并防止底部溢出的唯一有效方法。现在看起来应该是这样的:

在此处输入图像描述

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:

GridView.builder(
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 3,
                    mainAxisExtent: 150 // <-- this works!
                    ...
                )

It was the only working way for me to increase the tile height and prevent bottom overflow. Now it looks as it should:

enter image description here

不打扰别人 2025-01-16 11:56:35

尝试下面的代码希望对您有帮助。只需将您的小部件更改为我的小部件即可。

Padding(
  padding: EdgeInsets.all(8),
  child: GridView.builder(
    itemCount: 7,
    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
      crossAxisCount: 3,
      crossAxisSpacing: 10,
      mainAxisSpacing: 10,
    ),
    itemBuilder: (context, int index) {
      return Container(
        height: 120,
        width: 100,
        child: Column(
          children: [
            CircleAvatar(
              backgroundColor: Colors.green,
              child: Image.network(
                'https://miro.medium.com/max/1400/1*-6WdIcd88w3pfphHOYln3Q.png',
                height: 50,
                width: 30,
              ),
            ),
            SizedBox(
              height: 10,
            ),
            Text(
              'Product Name',
            ),
            SizedBox(
              height: 10,
            ),
            Text(
              '9',
            ),
          ],
        ),
      );
    },
  ),
),

您的结果屏幕-> 图片

Try below code hope its help to you.Just chnage your widget to my widget.

Padding(
  padding: EdgeInsets.all(8),
  child: GridView.builder(
    itemCount: 7,
    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
      crossAxisCount: 3,
      crossAxisSpacing: 10,
      mainAxisSpacing: 10,
    ),
    itemBuilder: (context, int index) {
      return Container(
        height: 120,
        width: 100,
        child: Column(
          children: [
            CircleAvatar(
              backgroundColor: Colors.green,
              child: Image.network(
                'https://miro.medium.com/max/1400/1*-6WdIcd88w3pfphHOYln3Q.png',
                height: 50,
                width: 30,
              ),
            ),
            SizedBox(
              height: 10,
            ),
            Text(
              'Product Name',
            ),
            SizedBox(
              height: 10,
            ),
            Text(
              '9',
            ),
          ],
        ),
      );
    },
  ),
),

Your result screen-> Image

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文