如何删除列中的小部件之间的间隙?

发布于 2025-01-23 10:20:28 字数 2076 浏览 0 评论 0 原文

我有一个,带有两个容器 s。没有填充物,没有边距。 尺寸完全合并在一起。 但是,我仍然在小部件之间得到了这个子像素差距。在模拟器和真实设备上可见。

我的代码下面:

       Column(
          children: [
            SizedBox(
              height: 57.0.h(context),
              width: MediaQuery.of(context).size.width,
              child: LayoutBuilder(
                builder: (context, constraints) => Column( // the column I actually am worried about
                  children: [
                    Container(
                      margin: EdgeInsets.zero,
                      padding: EdgeInsets.zero,
                      height: constraints.maxHeight * .45,
                      color: Theme.of(context).primaryColor,
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.stretch,
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          // here's some code
                        ],
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.zero,
                      padding: EdgeInsets.zero,
                      height: constraints.maxHeight * .55,
                      color: Theme.of(context).primaryColor,
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.stretch,
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          // here's some code
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
            Expanded(
              child: PageView.builder(
                // here's some code
              ),
            ),
          ],
        ),

我尝试将容器制作到以下 (因此要占用所有剩余空间),但没有任何更改。

I have a Column with two Containers. There is no padding, no margin.
The sizes perfectly fit together.
But still, I got this subpixel gap between widgets. It is visible on the simulator and real device.

gap

My code below:

       Column(
          children: [
            SizedBox(
              height: 57.0.h(context),
              width: MediaQuery.of(context).size.width,
              child: LayoutBuilder(
                builder: (context, constraints) => Column( // the column I actually am worried about
                  children: [
                    Container(
                      margin: EdgeInsets.zero,
                      padding: EdgeInsets.zero,
                      height: constraints.maxHeight * .45,
                      color: Theme.of(context).primaryColor,
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.stretch,
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          // here's some code
                        ],
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.zero,
                      padding: EdgeInsets.zero,
                      height: constraints.maxHeight * .55,
                      color: Theme.of(context).primaryColor,
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.stretch,
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          // here's some code
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
            Expanded(
              child: PageView.builder(
                // here's some code
              ),
            ),
          ],
        ),

I've tried to make Container below Expanded (so to occupy ALL the remaining space) but nothing has changed.

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

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

发布评论

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

评论(2

两个我 2025-01-30 10:20:28

我遇到了同样的问题。将每个小部件包裹在所需颜色的0像素边框中的容器中,这是一个技巧:

    Container(
        decoration: BoxDecoration(
          border: Border.all(
            width: 0.0,
            color: /* same Color that your widget has */,
          ),
          color: /* same Color that your widget has */,
        ),
        child: // child widget goes here
    )

由于抗降低的行为,该问题是在此github问题

我认为这解决了问题,因为边框的颜色用于不与硬件像素对齐的抗体缩放边缘。但这只是我的理论。

I had the same problem. Wrapping each widget in a container with a 0-pixel border of the desired color did the trick:

    Container(
        decoration: BoxDecoration(
          border: Border.all(
            width: 0.0,
            color: /* same Color that your widget has */,
          ),
          color: /* same Color that your widget has */,
        ),
        child: // child widget goes here
    )

The issue is propably caused because of the anti-aliasing behavior, as discussed in this GitHub issue.

I think this solves the problem because the color of the border is used for anti-aliasing edges that are not aligned with hardware pixels. But that's just my theory.

壹場煙雨 2025-01-30 10:20:28

尝试添加容器'S 装饰具有零边框宽度。

Container(
  height: constraints.maxHeight * .45,
  decoration: BoxDecoration(
    color: Theme.of(context).primaryColor,
    border: Border.all(
      // one or both
      color: Colors.transparent,
      width: 0.0,
    ),
  ),
.....

Try adding Container's decoration with zero border width.

Container(
  height: constraints.maxHeight * .45,
  decoration: BoxDecoration(
    color: Theme.of(context).primaryColor,
    border: Border.all(
      // one or both
      color: Colors.transparent,
      width: 0.0,
    ),
  ),
.....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文