
Flutter 新手,在写练习的时候发现这么个问题, 在 Container 类的 API 中对 alignment 属性的描述是这样的:
Additional constraints to apply to the child. 按照字面意思理解是适用于 Container 中 child 子元素的约束。
但是我在代码里将 Container 设置为宽高都是 200,然后将 constraints 属性设置为
constraints: BoxConstraints( maxWidth: 100, ), 整个 body 代码是这样的:
body: Container( width: 200, height: 200, alignment: Alignment.center, constraints: BoxConstraints( maxWidth: 100, ), child: Text( "哈哈哈", style: TextStyle(backgroundColor: Colors.lightGreenAccent), ), ), 显示的却是一个 100*200 的矩形
请问是怎么回事儿啊,constraints 不是针对 child 的吗?有点儿整迷糊了。
1 yimity 2020-03-19 11:21:28 +08:00 这个好像是因为 Container 在渲染的时候,有一个扩大和缩小的策略。具体受影响的因素比较多。没有细究,你可以再看看 Container 的大小的文章。 |
2 lihuichaoo 2020-04-03 10:00:08 +08:00 The constructor width and height arguments are combined with the constraints argument to set this property. maxWidth: 100 会将 width: 200 限制到 100 |