V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
lixyz
V2EX  ›  Flutter

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

  •  
  •   lixyz · Mar 18, 2020 · 6165 views
    This topic created in 2242 days ago, the information mentioned may be changed or developed.

    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 replies    2020-04-03 10:00:08 +08:00
    yimity
        1
    yimity  
       Mar 19, 2020
    这个好像是因为 Container 在渲染的时候,有一个扩大和缩小的策略。具体受影响的因素比较多。没有细究,你可以再看看 Container 的大小的文章。
    lihuichaoo
        2
    lihuichaoo  
       Apr 3, 2020
    The constructor width and height arguments are combined with the constraints argument to set this property.

    maxWidth: 100 会将 width: 200 限制到 100
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   981 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 22:13 · PVG 06:13 · LAX 15:13 · JFK 18:13
    ♥ Do have faith in what you're doing.