V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
lixyz
V2EX  ›  Flutter

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

  •  
  •   lixyz · 2020-03-18 16:32:45 +08:00 · 5354 次点击
    这是一个创建于 1471 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    2 条回复    2020-04-03 10:00:08 +08:00
    yimity
        1
    yimity  
       2020-03-19 11:21:28 +08:00
    这个好像是因为 Container 在渲染的时候,有一个扩大和缩小的策略。具体受影响的因素比较多。没有细究,你可以再看看 Container 的大小的文章。
    lihuichaoo
        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
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1002 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 22:20 · PVG 06:20 · LAX 15:20 · JFK 18:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.