如何将一个容器固定在屏幕的特定位置

huangapple go评论62阅读模式
英文:

How to fix a container at a particular spot on the screen

问题

我正在学习Flutter,制作了一个类似这样的应用程序:

如何将一个容器固定在屏幕的特定位置

我面临的问题是如何将容器固定在屏幕上的特定位置,就像它必须与顶部中心对齐一样。这是我面临的问题:

如何将一个容器固定在屏幕的特定位置

以下是代码:

class Program7 extends StatefulWidget {
  const Program7({super.key});

  @override
  State<Program7> createState() => _Program7State();
}

class _Program7State extends State<Program7> {
  double cHeightAndWidth = 300;

  
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          Container(
            height: cHeightAndWidth,
            width: cHeightAndWidth,
            decoration: BoxDecoration(
              color: Colors.purple,
            ),
          ),
          Column(
            children: [
              //一堆按钮的行,
            ],
          ),
        ],
      ),
    );
  }
}

附注:我已经尝试使用align将容器固定在另一个容器的顶部中心,但紫色颜色似乎渗出到了较大的容器中。

英文:

I'm learning flutter and I have made an app that looks like this:

如何将一个容器固定在屏幕的特定位置

I'm facing a problem as to how to fix the container fixed on a particular spot on the screen like it has to be aligned to the top center. Here's the problem I'm facing:

如何将一个容器固定在屏幕的特定位置

Here's the code:

class Program7 extends StatefulWidget {
  const Program7({super.key});

  @override
  State&lt;Program7&gt; createState() =&gt; _Program7State();
}

class _Program7State extends State&lt;Program7&gt; {
  double cHeightAndWidth = 300;

  
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          Container(
            height: cHeightAndWidth,
            width: cHeightAndWidth,
            decoration: BoxDecoration(
              color: Colors.purple,
            ),
          ),
          Column(
            children: [
              //A bunch of rows of buttons,
            ],
          ),
        ],
      ),
    );
  }
}

P.S.: I already tried to fix the container to the top center of another container using align but the purple color somehow bleeds out into the bigger container.

答案1

得分: 1

问题出在使用了MainAxisAlignment.spaceAround。它会使用剩余的空间,将一半放在子元素前面,另一半放在子元素后面。

对于顶部的容器,您可以使用固定的间隙。

return SafeArea(
  child: Column(
    children: [
      SizedBox(height: 50),
      Container(
        height: cHeightAndWidth,
        width: cHeightAndWidth,
        decoration: BoxDecoration(
          color: Colors.purple,
        ),
      ),
      Spacer(), // 或其他小部件
      Column(
        children: [
          // 一堆按钮行,
        ],
      ),
    ],
  ),
);
英文:

The issue is using MainAxisAlignment.spaceAround,. It will use the free space and put half before and another half at end of the child.

You can use fixed gap for top(Container).

return SafeArea(
  child: Column(
    children: [
      SizedBox(height: 50),
      Container(
        height: cHeightAndWidth,
        width: cHeightAndWidth,
        decoration: BoxDecoration(
          color: Colors.purple,
        ),
      ),
      Spacer(), // or other widget
      Column(
        children: [
          //A bunch of rows of buttons,
        ],
      ),
    ],
  ),
);

huangapple
  • 本文由 发表于 2023年6月1日 19:37:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定