Flutter 在嵌套 Row 和 ListView 中扩展高度

发布于 2025-01-13 22:38:09 字数 1515 浏览 2 评论 0原文

卡片示例

我在 ListView 中有一张卡片,应该如上图所示。

//Code has been simplified by removing most theme.
ListView(
          children: [
            Card(
              child: Row(children: [
                Container(
                  color: Colors.red,
                  width: 8,
                  height: double.infinity, //Promblem here!!!
                ),
                SizedBox(width: 8),
                Expanded(
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Text(
                        'Detail',
                      ),
                      Text('Detail'),
                      SizedBox(
                        width: double.infinity,
                        child: Text(
                          DateTime.now().toString(),
                          textAlign: TextAlign.end,
                        ),
                      ),
                    ],
                  ),
                )
              ]),
            ),
          ]),

问题是红色的容器。这是一条红色垂直线作为通知。我希望它与 Row 或 Card 一样高,但保持有限的宽度,例如 4。

所以我尝试了 height: double.infinityExpanded 与容器。它要么是异常,要么是溢出。 而且,尝试用 Expanded 包装 Row 会导致另一个布局异常。

那么如何让这条红色竖线有合适的高度呢?

Card Sample

I have a Card inside a ListView which should be like the picture above.

//Code has been simplified by removing most theme.
ListView(
          children: [
            Card(
              child: Row(children: [
                Container(
                  color: Colors.red,
                  width: 8,
                  height: double.infinity, //Promblem here!!!
                ),
                SizedBox(width: 8),
                Expanded(
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Text(
                        'Detail',
                      ),
                      Text('Detail'),
                      SizedBox(
                        width: double.infinity,
                        child: Text(
                          DateTime.now().toString(),
                          textAlign: TextAlign.end,
                        ),
                      ),
                    ],
                  ),
                )
              ]),
            ),
          ]),

The problem is the Container with red color. It's a red vertical line as a notification thing. I want it to as high as Row or Card, but keep a limited width like 4.

So I have tried height: double.infinity and Expanded with the Container. It's either an exception or an overflow.
And also, trying to wrap the Row with Expanded cause another layout exception.

So how to make this red vertical line have a properly height?

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

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

发布评论

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

评论(1

一抹淡然 2025-01-20 22:38:09

实现此目的的简单方法是:

ListView(
        children: [
          ListTile(
            leading: SizedBox(height: 40, width: 8.0, child: Container(color: Colors.red, width: 6.0,)),
            title: const Text("Random Name"),
            subtitle:  Text("Short Description \n ${DateTime.now()}"),
          ),
          ListTile(
            leading: SizedBox(height: 40, width: 8.0, child: Container(color: Colors.red, width: 6.0,)),
            title: const Text("Random Name"),
            subtitle:  Text("Short Description \n ${DateTime.now()}"),
          ),
          ListTile(
            leading: SizedBox(height: 40, width: 8.0, child: Container(color: Colors.red, width: 6.0,)),
            title: const Text("Random Name"),
            subtitle:  Text("Short Description \n ${DateTime.now()}"),
          ),ListTile(
            leading: SizedBox(height: 40, width: 8.0, child: Container(color: Colors.red, width: 6.0,)),
            title: const Text("Random Name"),
            subtitle:  Text("Short Description \n ${DateTime.now()}"),
          ),

        ],
      ),

在此处输入图像描述

Easy way to Achieve this is:

ListView(
        children: [
          ListTile(
            leading: SizedBox(height: 40, width: 8.0, child: Container(color: Colors.red, width: 6.0,)),
            title: const Text("Random Name"),
            subtitle:  Text("Short Description \n ${DateTime.now()}"),
          ),
          ListTile(
            leading: SizedBox(height: 40, width: 8.0, child: Container(color: Colors.red, width: 6.0,)),
            title: const Text("Random Name"),
            subtitle:  Text("Short Description \n ${DateTime.now()}"),
          ),
          ListTile(
            leading: SizedBox(height: 40, width: 8.0, child: Container(color: Colors.red, width: 6.0,)),
            title: const Text("Random Name"),
            subtitle:  Text("Short Description \n ${DateTime.now()}"),
          ),ListTile(
            leading: SizedBox(height: 40, width: 8.0, child: Container(color: Colors.red, width: 6.0,)),
            title: const Text("Random Name"),
            subtitle:  Text("Short Description \n ${DateTime.now()}"),
          ),

        ],
      ),

enter image description here

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