• Flutter组件--ConstrainedBox、BoxConstraints、UnconstrainedBox(根据内容自适应控件宽度和高度)


    1.ConstrainedBox

    主要目的是对其子组件添加额外的约束,有时候子组件需要自动调整宽度和高度,以达到最佳的适配设计,那么这时候使用ConstrainedBox 是最佳的选择。

    序列号字段属性描述
    1constraintsBoxConstraints对子组件添加额外约束
    2childWidget被约束的子组件

    ConstrainedBox基本使用

    1. ConstrainedBox(
    2. constraints: BoxConstraints(
    3. maxWidth: 100,
    4. minHeight: 30,
    5. ),
    6. child: Container(
    7. color: Colors.orangeAccent,
    8. child: Text("ConstrainedExample"),
    9. ),
    10. )

    2.BoxConstraints

    BoxConstraints对 RenderBox 布局的不可变布局约束,而 RenderBox 是二维笛卡尔坐标系中的渲染对象

    序列号字段属性描述
    1minWidthdouble最小宽度,默认0.0
    2maxWidthdouble最大宽度,默认double.infinity
    3minHeightdouble最小高度,默认0.0
    4maxHeightdouble最大高度,默认double.infinity

    BoxConstraints基本使用 

    1. ConstrainedBox(
    2. constraints: BoxConstraints(
    3. minWidth: 100,
    4. maxWidth: 200,
    5. minHeight: 30,
    6. maxHeight: 100
    7. ),
    8. child: Container(
    9. color: Colors.orangeAccent,
    10. child: Text("ConstrainedExample"),
    11. ),
    12. )

    BoxConstraints方法和说明

    1、tight()

    容器的宽度和高度取决于传进来的size,设定多大就是多大。

    1. BoxConstraints.tight(Size size)
    2. : minWidth = size.width,
    3. maxWidth = size.width,
    4. minHeight = size.height,
    5. maxHeight = size.height;

     2.tightFor()

    宽度和高度是可选参数,在不传入的情况下能大则大,在传入参数时设定多大就是多大

    1. const BoxConstraints.tightFor({
    2. double? width,
    3. double? height,
    4. }) : minWidth = width ?? 0.0,
    5. maxWidth = width ?? double.infinity,
    6. minHeight = height ?? 0.0,
    7. maxHeight = height ?? double.infinity;

    3.tightForFinite()

    宽度和高度默认给最大值,在不传参数的时候能大则大,在传入参数的时候能紧则紧

    1. const BoxConstraints.tightForFinite({
    2. double width = double.infinity,
    3. double height = double.infinity,
    4. }) : minWidth = width != double.infinity ? width : 0.0,
    5. maxWidth = width != double.infinity ? width : double.infinity,
    6. minHeight = height != double.infinity ? height : 0.0,
    7. maxHeight = height != double.infinity ? height : double.infinity;

    4.loose()

    最大宽度和最大高度限定于传入的size,未超出能紧则紧

    1. BoxConstraints.loose(Size size)
    2. : minWidth = 0.0,
    3. maxWidth = size.width,
    4. minHeight = 0.0,
    5. maxHeight = size.height;

    5.expand()

    宽度和高度是可选参数,在不传入时依赖于父组件, 占用父组件剩余的全部空间,在传入时设定多大就是多大

    1. const BoxConstraints.expand({
    2. double? width,
    3. double? height,
    4. }) : minWidth = width ?? double.infinity,
    5. maxWidth = width ?? double.infinity,
    6. minHeight = height ?? double.infinity,
    7. maxHeight = height ?? double.infinity;

     3.UnconstrainedBox

    UnconstrainedBox 不会对子组件产生任何限制,允许其子组件按照本身大小绘制,那么在我们的平时开发过程中用到该组件会相对较少,一般用于去除多重限制 的时候会有一些帮助。

    比如AppBar 中 actions 属性的按钮大小是固定的,如果想要修改就可以借助 UnconstrainedBox 去除父元素的限制。

    1. AppBar(
    2. title: Text("ConstrainedExample"),
    3. actions: [
    4. UnconstrainedBox(
    5. child: Container(
    6. width: 20,
    7. height: 20,
    8. color: Colors.pink,
    9. child: IconButton(onPressed: () {}, icon: Icon(Icons.alarm), iconSize: 20, padding: EdgeInsets.zero,),
    10. ),
    11. ),
    12. IconButton(onPressed: () {}, icon: Icon(Icons.add)),
    13. ],
    14. )

    总结

    ConstrainedBox 会根据子组件的需要自动调整宽度和高度以达到最佳的适配效果,如果确切知道组件需要设定多少宽高那么ConstrainedBox 就不在适合。 BoxConstraints 为 ConstrainedBox 额外增加的附加选项,尽可能达到业务需求。UnconstrainedBox 使用场景非常少,主要用于去除多重限制。

  • 相关阅读:
    webview_flutter
    看门狗 WDG
    Java回顾-Collection-List-ArratList/LinkedList/Vector的对比
    干货!阿里「大型分布式技术手册」,站在巨人肩膀上学习架构
    齐岳|近红外染料CY7.5标记PCL聚已内酯纳米载体CY7.5-PCL|PCL-CY7.5|CY7.5-PEG-PCL
    Leetcode27-移除元素详解
    视频超分之BasicVSR-阅读笔记
    linux 中 tar \ zip 解压错误后撤回
    关于我在编程里学表白这件事。。。。【python表白代码】
    社区系统项目复盘-5
  • 原文地址:https://blog.csdn.net/eastWind1101/article/details/127969767