请教一个 Flutter 中关于 BoxConstraints 的问题

2020-03-18 16:32:45 +08:00
 lixyz

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 的吗?有点儿整迷糊了。

5385 次点击
所在节点    Flutter
2 条回复
yimity
2020-03-19 11:21:28 +08:00
这个好像是因为 Container 在渲染的时候,有一个扩大和缩小的策略。具体受影响的因素比较多。没有细究,你可以再看看 Container 的大小的文章。
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

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/653921

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX