颤动-创建以minHeight开始,增长到maxHeight的框的正确方法 [英] flutter - correct way to create a box that starts at minHeight, grows to maxHeight
问题描述
我有一个容器,我希望以最小尺寸开始,然后增长(如果用户添加内容时其内容增长)到最大尺寸,然后停止。
I have a container that I want to start off at a minimum size and grow (if its contents grow while user is adding content) to a maximum size, then stop.
正确的小部件似乎是ConstrainedBox,如下所示:
The correct widget for this seems to be ConstrainedBox, like so:
new ConstrainedBox(
constraints: new BoxConstraints(
minHeight: 35.0,
maxHeight: 60.0,
),
child: ...child with growing content (has default height 25.0)...
),
,但是,这会将框从maxHeight开始。
however, this starts the box off at the maxHeight.
我尝试使用hasBoundedHeight,但似乎无法为其构造正确的语法或在文档中找到示例。
I tried to use hasBoundedHeight, but can not seem to construct the correct syntax for it or find an example in documentation.
什么是最好的
推荐答案
没有从最大/最小尺寸开始的概念。
There's no notion of "Starts from max/min size".
问题是, ContrainedBox
只会为其子项添加约束。
The thing is, ContrainedBox
only add constraints to it's child. But in the end, it doesn't pick a size.
如果您希望您的孩子达到minSize,则他们必须不消费。转换为 not 的宽度/高度为 double.INFINITY
。事实是 double.INFINITY
是许多小部件(包括 Container
)的默认值。
If you want your child to hit minSize, then they have to not expend. Which translate into not having a width/height of double.INFINITY
. Fact is that double.INFINITY
is the default value of many widgets, including Container
.
另一方面,某些小部件,例如 DecoratedBox
的默认大小为0。
其中意味着这段代码:
On the other hand, some widgets such as DecoratedBox
have a default size of 0.
Which means that this code :
return new ConstrainedBox(
constraints: new BoxConstraints(
minHeight: 5.0,
minWidth: 5.0,
maxHeight: 30.0,
maxWidth: 30.0,
),
child: new DecoratedBox(
decoration: new BoxDecoration(color: Colors.red),
),
);
将渲染一个5.0 * 5.0的红色方块。
Will render a 5.0*5.0 red square.
这篇关于颤动-创建以minHeight开始,增长到maxHeight的框的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!