Flutter 尺寸限制类容器组件

Flutter 尺寸限制类容器组件

简述

Flutter中尺寸限制类容器组件包含:Container、ConstrainedBox、UnconstrainedBox、SizedBox、AspectRatio、FractionallySizedBox、LimiteBox等,这些组件可以约束子组件的尺寸。

Container

ConstrainedBox

对子组件尺寸限制,可以设置最大值、最小值。

在这里插入图片描述

ConstrainedBox(
    constraints: const BoxConstraints(
        minWidth: 50,
        minHeight: 50,
        maxHeight: 100,
        maxWidth: 100,
    ),
    child: Container(
        width: 10,
        height: 10,
        color: Colors.red,
    ),
),
const SizedBox(height: 10),
ConstrainedBox(
    constraints: const BoxConstraints(
        minWidth: 50,
        minHeight: 50,
        maxHeight: 100,
        maxWidth: 100,
    ),
    child: Container(
        width: 200,
        height: 200,
        color: Colors.red,
    ),
)

SizedBox

为子组件设置固定的宽高。功能上类似ConstrainedBox组件。

在这里插入图片描述

SizedBox(
    width: 50,
    height: 50,
    child: Container(color: Colors.green),
),
const SizedBox(height: 10),
SizedBox.fromSize(
    size: const Size(50, 50),
    child: Container(color: Colors.green),
)

UnconstrainedBox

用于去除父组件的约束,允许子组件按照自己的尺寸绘制。

在这里插入图片描述

红色文字部分只会在debug模式下显示,在release模式下只会显示蓝色区域。

SizedBox(
    width: 10,
    height: 10,
    child: UnconstrainedBox(
        child: Container(width: 100, height: 100, color: Colors.blue),
    ),
)

AspectRatio

固定宽高比得组件。

aspectRatio:表示宽高比。可以为分数形式或小数形式。

在这里插入图片描述

Container(
    width: 100,
    height: 100,
    alignment: Alignment.center,
    color: Colors.red,
    child: AspectRatio(
        aspectRatio: 2 / 1,
        child: Container(color: Colors.blue),
    ),
)

FractionallySizedBox

相对尺寸组件,控制子元素占父组件的百分比空间。

在这里插入图片描述

Container(
    width: 100,
    height: 100,
    color: Colors.blue,
    child: FractionallySizedBox(
        widthFactor: 0.5,
        heightFactor: 0.5,
        child: Container(color: Colors.red),
    ),
)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值