如何告诉GridView Builder只需采取必要的高度?

发布于 2025-01-24 03:43:25 字数 1909 浏览 0 评论 0原文

是否可以制作一个gridview.builder占据孩子的高度,而不是指定父母的一定身高?

我有一个gridview.builder,应该放置列表的内容。我遇到了一个错误,因为我没有在父容器上指定高度,但是我希望GridView仅采用最小的必要高度,因此孩子们适合。

这是我的GridView:

return SizedBox(
      // height: 400,
      child: GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          childAspectRatio: MediaQuery.of(ctx).size.width /
              (MediaQuery.of(ctx).size.height / 4),
          crossAxisCount: MediaQuery
              .of(ctx)
              .size
              .width > 600 ? 3 : 1,
        ),
        itemCount: content.length,
        itemBuilder: (BuildContext ctx, index) {
          return SizedBox(
            height: 100,
            width: 300,
            child: ListTile(
              leading: content[index]["icon"],
              title: content[index]["title"],
              subtitle: Expanded(
                child: Text(
                  content[index]["text"],
                  style: const TextStyle(fontSize: 18),
                ),
              ),
            ),
          );
        },
      ),
    );

列表是这样的:

List content = [
      {"icon": ...,
        "title": ...,
        "text": ...},
      {"icon": ...,
        "title": ...,
        "text": ...},
      {"icon": ...,
        "title": ...,
        "text": ...},
      {"icon": ...,
        "title": ...,
        "text": ...},
    ];

这些是​​我的控制台上出现的一些错误:

​/I.SSTATIC.NET/QYM7J.PNG“ alt =”在此处输入图像描述“>

如果我删除sizebox包装gridview:

​具有大小,但在索引1上丢失了。我不知道这意味着什么。

有人可以帮我吗?我的疑问是非常基本的,但是如果我是做错了什么,我附上了我的代码,这是非常具体的。

先感谢您。

Is it possible to make a GridView.builder that takes the height of its children, instead of specifying a certain height on its parent?

I have a GridView.builder that is supposed to put the content of a list. I get an error because I didn't specify height on the parent container, but I would like the GridView to take only the minimal necessary height so the children fit.

This is my GridView:

return SizedBox(
      // height: 400,
      child: GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          childAspectRatio: MediaQuery.of(ctx).size.width /
              (MediaQuery.of(ctx).size.height / 4),
          crossAxisCount: MediaQuery
              .of(ctx)
              .size
              .width > 600 ? 3 : 1,
        ),
        itemCount: content.length,
        itemBuilder: (BuildContext ctx, index) {
          return SizedBox(
            height: 100,
            width: 300,
            child: ListTile(
              leading: content[index]["icon"],
              title: content[index]["title"],
              subtitle: Expanded(
                child: Text(
                  content[index]["text"],
                  style: const TextStyle(fontSize: 18),
                ),
              ),
            ),
          );
        },
      ),
    );

The list is something like this:

List content = [
      {"icon": ...,
        "title": ...,
        "text": ...},
      {"icon": ...,
        "title": ...,
        "text": ...},
      {"icon": ...,
        "title": ...,
        "text": ...},
      {"icon": ...,
        "title": ...,
        "text": ...},
    ];

And these are some of the errors that appear on my console:
enter image description here

enter image description here

enter image description here

This is what I get if I remove the SizeBox wrapping the GridView:

enter image description here

Note how index 0 has size but on index 1 it is missing. I don't know what that means.

Could somebody help me? My doubt is pretty basic but I attached my code in case I am the one doing something wrong which is very specific.

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

Smile简单爱 2025-01-31 03:43:25

是的,Minheight& BoxConstraints内部的Maxheight属性...
由于未提及父母小部件的宽度和高度属性,您面临的错误。
有必要为ListView或GridView定义宽度和高度属性...
而不是使用Sipedbox将其更改为容器...
然后,在约束属性中,您可以定义大小。对于良好的代码标准,请始终使用最小/最大宽度和高度

示例代码:

Container(
          constraints: BoxConstraints( //below mentioned sizes for reference only
                    minWidth: 100.0,
                    maxWidth: 320.0, 
                    minHeight: 100.0,
                    maxHeight: 600.0),
          child: GridView.builder(
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              childAspectRatio: MediaQuery.of(ctx).size.width /
                  (MediaQuery.of(ctx).size.height / 4),
              crossAxisCount: MediaQuery
                  .of(ctx)
                  .size
                  .width > 600 ? 3 : 1,
            ),
            itemCount: content.length,
            itemBuilder: (BuildContext ctx, index) {
              return SizedBox(
                height: 100,
                width: 300,
                child: ListTile(
                  leading: content[index]["icon"],
                  title: content[index]["title"],
                  subtitle: Expanded(
                    child: Text(
                      content[index]["text"],
                      style: const TextStyle(fontSize: 18),
                    ),
                  ),
                ),
              );
            },
          ),
        );

Yes it is possible with minHeight & maxHeight property inside BoxConstraints...
The errors you facing because of not mentioned width and height property for your Parent Widget.
It is necessary to define width and height property for ListView or GridView...
instead of using SizedBox change it to Container...
then in constraints property you can define your sizes. For good code standard always use Min/Max width and height

Example code :

Container(
          constraints: BoxConstraints( //below mentioned sizes for reference only
                    minWidth: 100.0,
                    maxWidth: 320.0, 
                    minHeight: 100.0,
                    maxHeight: 600.0),
          child: GridView.builder(
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              childAspectRatio: MediaQuery.of(ctx).size.width /
                  (MediaQuery.of(ctx).size.height / 4),
              crossAxisCount: MediaQuery
                  .of(ctx)
                  .size
                  .width > 600 ? 3 : 1,
            ),
            itemCount: content.length,
            itemBuilder: (BuildContext ctx, index) {
              return SizedBox(
                height: 100,
                width: 300,
                child: ListTile(
                  leading: content[index]["icon"],
                  title: content[index]["title"],
                  subtitle: Expanded(
                    child: Text(
                      content[index]["text"],
                      style: const TextStyle(fontSize: 18),
                    ),
                  ),
                ),
              );
            },
          ),
        );

流年里的时光 2025-01-31 03:43:25

也许您可以将其包装在列中,并使用灵活或扩展的小部件为GridView和其他孩子设置比例。

Column(
        children: [
        Flexible(
          flex: 1,
          child: SizedBox(
            // height: 400,
            child: GridView.builder(
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                childAspectRatio: MediaQuery.of(ctx).size.width /
                    (MediaQuery.of(ctx).size.height / 4),
                crossAxisCount:
                    MediaQuery.of(ctx).size.width > 600 ? 3 : 1,
              ),
              itemCount: content.length,
              itemBuilder: (BuildContext ctx, index) {
                return SizedBox(
                  height: 100,
                  width: 300,
                  child: ListTile(
                    leading: content[index]["icon"],
                    title: content[index]["title"],
                    subtitle: Expanded(
                      child: Text(
                        content[index]["text"],
                        style: const TextStyle(fontSize: 18),
                      ),
                    ),
                  ),
                );
              },
            ),
          ),
        ),//Other childs
        Flexible(
          flex: 3,
          child: SizedBox(
            child: Container(
              color: Colors.red,
            ),
          ),
        )
      ],
    ),

Maybe you could wrap it inside a column and use flexible or expanded widgets to set the proportion for the GridView and other childs.

Column(
        children: [
        Flexible(
          flex: 1,
          child: SizedBox(
            // height: 400,
            child: GridView.builder(
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                childAspectRatio: MediaQuery.of(ctx).size.width /
                    (MediaQuery.of(ctx).size.height / 4),
                crossAxisCount:
                    MediaQuery.of(ctx).size.width > 600 ? 3 : 1,
              ),
              itemCount: content.length,
              itemBuilder: (BuildContext ctx, index) {
                return SizedBox(
                  height: 100,
                  width: 300,
                  child: ListTile(
                    leading: content[index]["icon"],
                    title: content[index]["title"],
                    subtitle: Expanded(
                      child: Text(
                        content[index]["text"],
                        style: const TextStyle(fontSize: 18),
                      ),
                    ),
                  ),
                );
              },
            ),
          ),
        ),//Other childs
        Flexible(
          flex: 3,
          child: SizedBox(
            child: Container(
              color: Colors.red,
            ),
          ),
        )
      ],
    ),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文