Dart 颤振-创建从minHeight开始到maxHeight的长方体的正确方法
我有一个容器,我想以最小的大小开始,并将其增长(如果用户添加内容时其内容增长)到最大大小,然后停止 正确的小部件似乎是ConstrainedBox,如下所示:Dart 颤振-创建从minHeight开始到maxHeight的长方体的正确方法,dart,flutter,Dart,Flutter,我有一个容器,我想以最小的大小开始,并将其增长(如果用户添加内容时其内容增长)到最大大小,然后停止 正确的小部件似乎是ConstrainedBox,如下所示: new ConstrainedBox( constraints: new BoxConstraints( minHeight: 35.0, maxHeight: 60.0, ), child: ...child with growing content (has default height 25.0)...
new ConstrainedBox(
constraints: new BoxConstraints(
minHeight: 35.0,
maxHeight: 60.0,
),
child: ...child with growing content (has default height 25.0)...
),
但是,这会在最大高度处启动长方体
我尝试使用hasBoundedHeight,但似乎无法为其构造正确的语法,也无法在文档中找到示例
使盒子按上述方式工作的最佳方法是什么?没有“从最大/最小尺寸开始”的概念
问题是,containedbox
只对它的子对象添加约束。但最终,它没有选择一个尺寸
如果你想让你的孩子打minSize,那么他们就不能花钱。转换为宽度/高度不为double.INFINITY
。事实上,double.INFINITY
是许多小部件的默认值,包括Container
另一方面,一些小部件(如DecoratedBox
)的默认大小为0。
这意味着该代码:
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的红方块。您可以使用deviceWidth和deviceHeight检查最小和最大条件。 使用以下代码在build方法中获取deviceWidth和deviceHeight
double deviceWidth = MediaQuery.of(context).size.width;
double deviceHeight = MediaQuery.of(context).size.height;
在Container
的width
和height
属性中,使用deviceWidth
和deviceHeight
形成您的条件
Container(
width: deviceWidth<200?50:deviceWidth*0.5,
height: deviceHeight<500?50:deviceHeight>800?200:deviceHeight*0.2,
child: //child,
)
容器(
width:deviceWidth下面的示例将帮助您根据需要增大小部件的大小
Container(
color: Colors.blueAccent,
constraints: BoxConstraints(
minHeight: 100, minWidth: double.infinity, maxHeight: 400),
child: ListView(
shrinkWrap: true,
children: <Widget>[
...List.generate(
10, // Replace this with 1, 2 to see min height works.
(index) => Text(
'Sample Test: ${index}',
style: TextStyle(fontSize: 60, color: Colors.black),
),
),
],
),
),
容器(
颜色:Colors.blueAccent,
约束:BoxConstraints(
最小高度:100,最小宽度:双无限,最大高度:400),
子:ListView(
收缩膜:对,
儿童:[
…List.generate(
10,//将其替换为1,2,以查看最小高度是否有效。
(索引)=>文本(
'样本测试:${index}',
样式:TextStyle(字体大小:60,颜色:Colors.black),
),
),
],
),
),
单个项目的最小高度输出:
10个项目的最小高度输出:
注意:这将根据所提到的最大高度显示小部件。是的,ConstrainedBox是用于此目的的合适小部件。
如果您仅为例如300提供minHeight参数,则子对象将具有最小高度,并将根据其内容增加其大小
下面是一个例子:
ConstrainedBox(
constraints: BoxConstraints(
minHeight: 300,
),
child: Container(child : SomeWidget(),)
如果SomeWidget()所需的高度小于300,则其高度将为300。否则,其高度将相应增加。
为了装饰和构造SomeWidget(),可以使用容器的填充和其他属性。
您可能会猜到容器必须在70到150像素之间,但您可能错了。约束框
仅对从其父级接收到的约束施加额外的约束
在这里,屏幕强制约束框
与屏幕大小完全相同,因此它告诉其子容器
也采用屏幕大小,从而忽略其约束
参数。ohhhhh。因此我需要一个新问题,“如何监听和测量一个从一个大小开始到另一个大小的对象的大小”你通常不需要这个。大多数小部件都有“适合最小大小”的选项"。例如,ListView
/GridView
具有shrinkWrap
属性。Row
/Column
具有mainAxisSize
,可以设置为mainAxisSize.min
,…我真正关心的是在这种情况下对象增长到最大大小时。是的,但您可以将其与约束框相结合。谢谢引入这个约束框。效果很棒
ConstrainedBox(
constraints: BoxConstraints(
minWidth: 70,
minHeight: 70,
maxWidth: 150,
maxHeight: 150,
),
child: Container(color: Colors.red, width: 10, height: 10),
)