容器内的颤振容器

时间:2018-09-04 01:51:02

标签: flutter

这些天我正在练习Flutter Container,而我想到的这个例子简直令人惊讶

我将尺寸为50x50的容器放入尺寸为200x200的容器中。奇怪的是,即使内部容器的约束严格到50x50,它也会扩展到200x200。

这是代码

Container(
  width: 200.0,
  height: 200.0,
  color: Colors.orange,
  child: Container(
    width: 50.0,
    height: 50.0,
    color: Colors.blue,
  ),
)

我希望在一个较大的橙色框中有一个小的蓝色框。

有人可以解释为什么吗?

1 个答案:

答案 0 :(得分:9)

您需要指定橙色框在蓝色框中显示的位置,否则蓝色框将扩大到其父级的大小。

  Container(
    width: 200.0,
    height: 200.0,
    color: Colors.orange,
    alignment: Alignment.center, // where to position the child
    child: Container(
      width: 50.0,
      height: 50.0,
      color: Colors.blue,
    ),
  ),
相关问题