【问题标题】:How do I align one widget to the top of the screen and center another widget to the middle of the screen in Flutter?如何在 Flutter 中将一个小部件对齐到屏幕顶部,并将另一个小部件居中到屏幕中间?
【发布时间】:2023-04-02 00:25:01
【问题描述】:

在同一个正文中,我试图将一个小部件与屏幕顶部对齐,将另一个小部件与屏幕中心对齐。事实证明这很困难。

这是我的代码:

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

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

提前感谢您的帮助!

【问题讨论】:

  • 应该居中到左边的空间,红色容器和底部之间,还是居中到整个空间,状态栏和底部之间。
  • @George 我试图让它居中于左边的空间,在红色容器和底部之间
  • 您尝试过 Stack 小部件吗?我觉得这就是你需要的。还有其他方法,例如 Positioned Widget。

标签: dart flutter flutter-layout


【解决方案1】:

一种选择是使用Expanded 小部件。此小部件将扩展填充父级中的所有可用空间,然后将子级居中放置在其中。请注意,这只适用于 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,
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    ),
  ]
),

【讨论】:

  • ListView 不是 Flex 小部件。
  • 确实,我已经删除了对 ListView 的引用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
相关资源
最近更新 更多