Flutter实战ConstrainedBox、SizedBox尺寸限制类容器使用详解

本文详细介绍了Flutter中的ConstrainedBox、BoxConstraints、SizedBox和UnconstrainedBox的使用,包括它们的属性、案例及在尺寸约束中的应用。通过实例展示了如何限制和解除子组件的尺寸约束,并提到了其他尺寸限制类容器的实战应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 目录

ConstrainedBox

BoxConstraints

ConstrainedBox、BoxConstraints案例

SizedBox

UnconstrainedBox


ConstrainedBox

先来看下 ConstrainedBox 属性

class ConstrainedBox extends SingleChildRenderObjectWidget {
   
//继承于 SingleChildRenderObjectWidget  说明只能有一个孩子
// constraints 为必须的 
  ConstrainedBox({
    Key key,
    @required this.constraints,
    Widget child,
  })

这里 constraints 为必传项 传入BoxConstraints实例,ConstrainedBox可对子组件约束。如果你想让子组件的最小高度是80像素,你可以使用const BoxConstraints(minHeight: 80.0)作为子组件的约束。

BoxConstraints

BoxConstraints用于设置 约束规则 如下

constraints: BoxConstraints(
          minWidth: 230,  //最小宽度
          minHeight: 50.0 , //最小高度
          maxWidth: 230, //最大宽度
          maxHeight:180, //最大高度
          ),

BoxConstraints 还提供了比较编辑的一些构造如:

constraints: BoxConstraints.expand(width:250,height:250),
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值