如何在 Flutter 中将一个小部件对齐到屏幕顶部并将另一个小部件居中到屏幕中间?

How do I align one widget to the top of the screen and center another widget to the middle of the screen in Flutter?

在同一主体内,我试图将一个小部件对齐到屏幕顶部,将另一个小部件对齐到屏幕中央。这被证明是困难的。

这是我的代码:

body: Column(children: <Widget>[
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              color: Colors.redAccent,
              width: screenWidth,
              height: 50.0,
              child: Center(
                child: Text(
                  "Hello World!",
                  style: TextStyle(
                    fontSize: 18.0,
                    color: Colors.white,
                  ),
                ),
              ),
            ),
          ],
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Container(
              color: Colors.blueAccent,
              width: screenWidth,
              height: 50.0,
              child: Center(
                child: Text(
                  "Thanks for the help!",
                  style: TextStyle(
                    fontSize: 18.0,
                    color: Colors.white,
                  ),
                ),
              ),
            ),
          ],
        ),
      ]),

当此代码在我的 Xcode 模拟器中为 运行 时,结果如下:

https://i.imgur.com/JtPKyq0.png

所以要明确这个结果是错误的,因为我希望蓝色容器位于屏幕的中心,而不是顶部。

在此先感谢您的帮助!

一种选择是使用 Expanded 小部件。此小部件将展开并填充父项中可用的所有 space,然后您将子项置于其中。请注意,这仅适用于 Flex 小部件,例如 RowColumn

对于您的情况,我还建议删除行宽的 screenWidth 变量,并使用 Expanded 小部件使容器水平填充屏幕。

这是最终代码:

body: Column(children: <Widget>[
    Row(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Expanded( //makes the red row full width
          child: Container(
            color: Colors.redAccent,
            height: 50.0,
            child: Center(
              child: Text(
                "Hello World!",
                style: TextStyle(
                  fontSize: 18.0,
                  color: Colors.white,
                ),
              ),
            ),
          ),
        ),
      ],
    ),
    // This expands the row element vertically because it's inside a column
    Expanded( 
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          // This makes the blue container full width.
          Expanded(
            child: Container(
              color: Colors.blueAccent,
              height: 50.0,
              child: Center(
                child: Text(
                  "Thanks for the help!",
                  style: TextStyle(
                    fontSize: 18.0,
                    color: Colors.white,
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    ),
  ]
),