首页 > 解决方案 > Flutter:如何在 Column 中将容器大小设置为 wrap_content

问题描述

我想设置Container大小动态我的意思是wrap_content如何在颤动中做?

在下面的代码中,默认情况下采用全宽Container,但我想将宽度设置ContainerText()

Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisSize: MainAxisSize.max,
      children: <Widget>[
        Container(
          margin: EdgeInsets.all(10),
          alignment: Alignment.center,
          color: Colors.orange[800],
          child: Text("Hello How are you"),
        ),
        Container(
          margin: EdgeInsets.all(10),
          alignment: Alignment.center,
          color: Colors.yellow[800],
          child: Text("Welcome"),
        )
      ],
    );

我明白了:

在此处输入图像描述

但我想要这个输出:

在此处输入图像描述

标签: flutterflutter-layout

解决方案


在此处输入图像描述

最简单的解决方案是使用Align.

Column(
  mainAxisAlignment: MainAxisAlignment.center,
  children: <Widget>[
    Align(
      child: Container(
        margin: EdgeInsets.all(10),
        color: Colors.orange[800],
        child: Text("Hello How are you"),
      ),
    ),
    Align(
      child: Container(
        margin: EdgeInsets.all(10),
        color: Colors.yellow[800],
        child: Text("Welcome"),
      ),
    )
  ],
)

推荐阅读