首页 > 解决方案 > 定位在堆栈内

问题描述

如何正确定位小部件?

我认为定位应该通过一些模板而不是眼睛。

假设我希望白色方块位于黑色容器的顶部中心,一半在黑色容器内部,一半在外部,我该怎么做?

在此处输入图像描述

编码:

Positioned(
                top: 80,
                right: 30,
                left: 30,
                child: Container(
                  height: 200,
                  width: 400.0,
                  color: Colors.black,
                  child: Column(
                    children: <Widget>[],
                  ),
                ),
              ),
              Positioned(
                top: 40,
                child: Container(
                  height: 100.0,
                  width: 100.0,
                  color: Colors.white,
                ),
              ),

标签: dartflutterflutter-layout

解决方案


你可以这样尝试

Stack(
      alignment: Alignment.center,
      children: <Widget>[
          Positioned(
            top: 80,
            right: 30,
            left: 30,
            child: Container(
              height: 200,
              width: 400.0,
              color: Colors.black,
              child: Column(
                children: <Widget>[],
              ),
            ),
          ),
          Positioned(
            top: 40,
            child: Container(
              height: 100.0,
              width: 100.0,
              color: Colors.white,
            ),
          ),
      ],
    )

推荐阅读