Flutter:在行中将2个大小相同的容器放在列的中间

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

Flutter: Place 2 sized Containers in the middle of a column in a row

问题

I have 2 containers with width and height set to them and a background color (basically a small box). I want to put those 2 containers in the middle or a parent container, on the same row divided to 2 columns in the middle of each column.

这两个容器的宽度和高度已经设置好了,并且有背景颜色(基本上是一个小方块)。我想把这两个容器放在父容器的中间,放在同一行分成2列,每列都在中间。

I know it sounds confusing. It really isn't. This is what I want to achieve:

我知道听起来有点混乱。但实际上并不是。这是我想要实现的效果:

Having the 2 yellow containers in the middle of an imaginary column that takes half the width of the container.

将这两个黄色容器放在一个虚拟列的中间,这个虚拟列占据了容器一半的宽度。

This is my row containing the 2 yellow boxes:

这是包含两个黄色方块的行:

Row(
    children: [
      SwitchPreviewButton(),
      
      SwitchPreviewButton(),
    ],
);

And this is the yellow box widget:

这是黄色方块的小部件:

Widget build(BuildContext context) {
    double width = 10.22;
    double height = 17.32;

    return Container(
      constraints: BoxConstraints(maxWidth: width, maxHeight: height),
      width: width,
      height: height,
      decoration: BoxDecoration(
        color: Colors.yellow, //const Color(0xFFB0B0B0),
        borderRadius: BorderRadius.circular(1.16)
      ),
    );
}

And this is what I get:

这是我得到的效果:

Flutter:在行中将2个大小相同的容器放在列的中间

If I try to use Expand and center the Yellow widget takes 100% of available space.

如果我尝试使用Expand并居中,黄色小部件会占据所有可用空间。

How can I achieve that?

如何实现这个效果?

英文:

I have 2 containers with width and height set to them and a background color (basically a small box). I want to put those 2 containers in the middle or a parent container, on the same row divided to 2 columns in the middle of each column.

I know it sounds confusing. It really isn't. This is what I want to achieve:

Flutter:在行中将2个大小相同的容器放在列的中间

Having the 2 yellow containers in the middle of an imaginary column that takes half the width of the container.

This is my row containing the 2 yellow boxes:

Row(
    children: [
      SwitchPreviewButton(),
      
      SwitchPreviewButton(),
    ],
  );

And this is the yellow box widget:

Widget build(BuildContext context) {
    double width = 10.22;
    double height = 17.32;

    return Container(
      constraints: BoxConstraints(maxWidth: width, maxHeight: height),
      width: width,
      height: height,
      decoration: BoxDecoration(
        color: Colors.yellow, //const Color(0xFFB0B0B0),
        borderRadius: BorderRadius.circular(1.16)
      ),
    );
  }

And this is what I get:

Flutter:在行中将2个大小相同的容器放在列的中间

If I try to use Expand and center the Yellow widget takes 100% of available space.

How can I achieve that?

答案1

得分: 1

将此添加到您的行

Row(
  mainAxisAlignment: MainAxisAlignment.spaceAround,  //<---- 在此添加
  children: [
    SwitchPreviewButton(),
    SwitchPreviewButton(),
  ],
);
英文:

add this to your row

Row(
 mainAxisAlignment: MainAxisAlignment.spaceAround,  //<---- ADD THIS
    children: [
      SwitchPreviewButton(),
      
      SwitchPreviewButton(),
    ],
  );

huangapple
  • 本文由 发表于 2023年6月29日 21:42:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581613.html
匿名

发表评论

匿名网友

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

确定