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

How to avoid bottom overflow in flutter columns within a GridView

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

小部件排列在网格中,应该看起来像这样,但图像更大:

但是一旦我增加图像圈的半径,我就会像这里一样底部溢出:

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

  @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)),
                //
              ],
            ))

我尝试了什么:

无论我做什么,我都无法使柱子增加高度并使元素适合它。

我阅读了很多其他相关问题,但答案对我不起作用。我将非常感谢任何允许我保留网格并增加图像大小而不会溢出的建议。

尝试下面的代码希望它有助于you.Just将您的小部件更改为我的小部件。

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',
            ),
          ],
        ),
      );
    },
  ),
),

您的结果屏幕->

我找到了实现我想要的方法。 为了增加瓷砖的高度,我使用了 mainAxisExtent 属性 of SliverGridDelegateWithFixedCrossAxisCount:

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

这是我唯一的工作方式,可以增加瓦片高度并防止底部溢出。现在它看起来应该是: