Flutter 66: 图解基本约束 Box (一)

简介: 0 基础学习 Flutter,第六十六步:学习一下基本约束盒子 (一)!

      小菜在学习过程中,为了调整或适配 Widget 大小时,会设置 Widget 或嵌套使用一些约束 Widget;小菜针对性学习一下这一系列的约束 Box

SizedBox

源码分析

class SizedBox extends SingleChildRenderObjectWidget {
  // 创建固定大小的约束Box
  const SizedBox({ Key key, this.width, this.height, Widget child })
    : super(key: key, child: child);

  // 创建父类允许最大尺寸的约束Box
  const SizedBox.expand({ Key key, Widget child })
    : width = double.infinity,
      height = double.infinity,
      super(key: key, child: child);

  // 创建父类允许最小尺寸的约束Box
  const SizedBox.shrink({ Key key, Widget child })
    : width = 0.0,
      height = 0.0,
      super(key: key, child: child);

  // 创建指定大小的约束Box
  SizedBox.fromSize({ Key key, Widget child, Size size })
    : width = size?.width,
      height = size?.height,
      super(key: key, child: child);
}

      分析源码可知,SizeBox 继承自 SingleChildRenderObjectWidget 仅提供子 Child 的存储并不提供更新逻辑;且 SizedBox 提供了多种使用方法,小菜逐一尝试;

案例尝试

1. SizedBox({ Key key, this.width, this.height, Widget child })

      SizedBox 可自定义 widthheight,当限制宽高时,子 Widget 无论宽高如何,均默认填充;通过设置 double.infinity 填充父类 Widget 宽高,注意此时父类要有限制,不可是无限宽高;当 widthheight 未设置时,根据子 Widget 大小展示;

return SizedBox(width: 100.0, height: 100.0, child: Container(color: Colors.teal.withOpacity(0.4)));
return SizedBox(width: 100.0, height: 100.0, child: Container(color: Colors.red.withOpacity(0.4), width: 200.0, height: 200.0));
return SizedBox(width: 100.0, height: 100.0, child: Container(color: Colors.pink.withOpacity(0.4), width: 50.0, height: 50.0));
return SizedBox(width: double.infinity, height: 100.0, child: Container(color: Colors.blueAccent.withOpacity(0.4)));

2. SizedBox.expand({ Key key, Widget child })

      .expand 是便捷方法,默认 widthheight 均为 double.infinity,填充整个父类 Widget

return SizedBox.expand(child: Container(color: Colors.blueAccent.withOpacity(0.4)));

3. SizedBox.shrink({ Key key, Widget child })

      .shrink 同样是便捷方法,但 widthheight 均为 0.0,无论子 Widget 设置多少均不展示;小菜暂时未想出此类方法的应用场景;

return SizedBox.shrink(child: Container(color: Colors.pink.withOpacity(0.4)));
4. SizedBox.fromSize({ Key key, Widget child, Size size })

      .fromSize 便捷方式与基本的构建方式基本相同,只是 widthheightSize 封装,小菜用此方法实现与基本方式相同的效果;

return SizedBox.fromSize(size: Size(100.0, 100.0), child: Container(color: Colors.teal.withOpacity(0.4)));
return SizedBox.fromSize(size: Size(100.0, 100.0), child: Container(color: Colors.red.withOpacity(0.4), width: 200.0, height: 200.0));
return SizedBox.fromSize(size: Size(100.0, 100.0), child: Container(color: Colors.pink.withOpacity(0.4), width: 50.0, height: 50.0));
return SizedBox.fromSize(size: Size(double.infinity, 100.0), child: Container(color: Colors.blueAccent.withOpacity(0.4)));

ConstrainedBox

源码分析

class ConstrainedBox extends SingleChildRenderObjectWidget {
  ConstrainedBox({
    Key key,
    @required this.constraints,
    Widget child,
  })
}

class BoxConstraints extends Constraints {
  // 用指定的约束大小创建框架大小
  const BoxConstraints({
    this.minWidth = 0.0,
    this.maxWidth = double.infinity,
    this.minHeight = 0.0,
    this.maxHeight = double.infinity,
  });

  // 仅用指定大小创建框架大小
  BoxConstraints.tight(Size size)
    : minWidth = size.width,
      maxWidth = size.width,
      minHeight = size.height,
      maxHeight = size.height;

  // 用指定的约束大小创建框架大小
  const BoxConstraints.tightFor({
    double width,
    double height,
  })

  // 创建需要给定宽度或高度的框约束,除非它们是无限的
  const BoxConstraints.tightForFinite({
    double width = double.infinity,
    double height = double.infinity,
  })

  // 创建禁止大小大于给定大小的框约束
  BoxConstraints.loose(Size size)
    : minWidth = 0.0,
      maxWidth = size.width,
      minHeight = 0.0,
      maxHeight = size.height;

  // 创建扩展为填充另一个框约束的框约束
  const BoxConstraints.expand({
    double width,
    double height,
  })
}

      分析源码可知,ConstrainedBox 同样是继承自 SingleChildRenderObjectWidget;约束方式主要是对 constraints 的操作;相对于 SizedBox 约束更为灵活;

案例尝试

1. BoxConstraints()

      BoxConstraints 的构造方法可以设置最大最小宽度和高度四个属性,在实际场景中可以灵活运用;其约束的大小按照 minWidth <= child.width <= maxWidth && minHeight <= child.Height <= maxHeight,当我们不设置宽高尺寸时,minWidth = 0.0 && minHeight = 0.0 && maxWidth = double.infinity && maxHeight = double.infinity;

return ConstrainedBox(constraints: BoxConstraints(), child: Container(color: Colors.pink.withOpacity(0.4), child: _textWid('ConstrainedBox')));
return ConstrainedBox(constraints: BoxConstraints(), child: Container(color: Colors.blueAccent.withOpacity(0.4), child: _textWid('ConstrainedBox: A widget that imposes additional constraints on its child.')));
return ConstrainedBox(constraints: BoxConstraints(minWidth: 100.0, minHeight: 100.0), child: Container(color: Colors.orange.withOpacity(0.4), child: _textWid('ConstrainedBox')));
return ConstrainedBox(constraints: BoxConstraints(maxHeight: 100.0, maxWidth: 100.0),  child: Container(color: Colors.teal.withOpacity(0.4), child: _textWid('ConstrainedBox: A widget that imposes additional constraints on its child.')));

2. BoxConstraints.tight(Size size)

      .tight 构造方法中需要传一个固定尺寸的 Size 其中 minWidth == maxWidth == size.width && maxWidth == maxHeight == size.height;无论子 Widget 设置多大宽高均按照 Size 宽高约束;

return ConstrainedBox(constraints: BoxConstraints.tight(Size(110.0, 120.0)),
        child: Container(color: Colors.purple.withOpacity(0.4), child: _textWid('ConstrainedBox: A widget that imposes additional constraints on its child.')));

3. BoxConstraints.tightFor()

      小菜理解 .tightFor 方式是 .tight 方式的细化,可以自定义 widthheight;若宽高未设置,则最大宽高为无限,最小宽高为 0.0;若设置宽高则以设置尺寸进行约束;

return ConstrainedBox(constraints: BoxConstraints.tightFor(height: 110.0),
        child: Container(color: Colors.teal.withOpacity(0.4), child: _textWid('ConstrainedBox: A widget that imposes additional constraints on its child.')));
return ConstrainedBox(constraints: BoxConstraints.tightFor(width: 110.0, height: 120.0),
        child: Container(color: Colors.purple.withOpacity(0.4), child: _textWid('ConstrainedBox: A widget that imposes additional constraints on its child.')));

4. BoxConstraints.tightForFinite()

      .tightForFinite.tightFor 类似,默认最大宽高为无限,最小宽高为 0.0,其判断的是设置宽高是否为无限,不是 double.infinityminWidth == maxWidth == width && maxWidth == maxHeight == height 进行约束;

return ConstrainedBox(constraints: BoxConstraints.tightForFinite(),
        child: Container(color: Colors.teal.withOpacity(0.4), child: _textWid('ConstrainedBox: A widget that imposes additional constraints on its child.')));
return ConstrainedBox(constraints: BoxConstraints.tightForFinite(width: 110.0, height: 120.0),
        child: Container(color: Colors.purple.withOpacity(0.4), child: _textWid('ConstrainedBox: A widget that imposes additional constraints on its child.')));

5. BoxConstraints.loose()

      .loose 方式限制给定 Size 大小,minWidth / minHeight 均为 0.0,最大宽高为 Size 大小;其中整体约束由 Size 限制,实际展示与子 Widget 大小相关;

return ConstrainedBox(constraints: BoxConstraints.loose(Size(110.0, 150.0)),
        child: Container(color: Colors.purple.withOpacity(0.4), child: _textWid('ConstrainedBox: A widget that imposes additional constraints on its child.')));
return ConstrainedBox(constraints: BoxConstraints.loose(Size(110.0, 150.0)),
        child: Container(width: 200.0, height: 200.0, color: Colors.grey.withOpacity(0.6),
            child: _textWid('ConstrainedBox: A widget that imposes additional constraints on its child.')));
return ConstrainedBox(constraints: BoxConstraints.loose(Size(110.0, 120.0)),
        child: Container(width: 100.0, height: 100.0, color: Colors.green.withOpacity(0.6),
            child: _textWid('ConstrainedBox: A widget that imposes additional constraints on its child.')));

6. BoxConstraints.expand()

      大部分 .expand 方式是填充方式,默认填满父 Widget,若不设置宽高,默认 minWidth == maxWidth == double.infinity && minHeight == maxHeight == double.infinity,使用时注意父类约束;

return ConstrainedBox(constraints: BoxConstraints.expand(),
    child: Container(color: Colors.teal.withOpacity(0.4), child: _textWid('ConstrainedBox: A widget that imposes additional constraints on its child.')));
return ConstrainedBox(constraints: BoxConstraints.expand(width: 110.0, height: 150.0),
    child: Container(color: Colors.purple.withOpacity(0.4), child: _textWid('ConstrainedBox: A widget that imposes additional constraints on its child.')));


      Flutter 提供了多种约束 Box,所谓条条大路通罗马,同一种效果可以有多种实现方式,但小菜认为还是应该多多了解,尽量尝试最优方案;小菜对 Box 系列的研究还很浅显,有错误的地方请多多指导!

来源: 阿策小和尚

目录
相关文章
|
15天前
深入理解Flutter鸿蒙next版本 中的Widget继承:使用extends获取数据与父类约束
本文详细介绍了Flutter中如何通过继承其他Widget来创建自定义组件。首先解释了Widget继承的基本概念,包括StatelessWidget和StatefulWidget的区别。接着通过具体示例展示了如何继承StatelessWidget和StatefulWidget,并在子类中访问父类的build方法和状态。最后,结合多个自定义Widget展示了如何在实际应用中灵活使用继承和组合来构建复杂的UI。
66 8
|
6月前
|
容器
Flutter笔记:Box协议的布局约束原理与应用
Flutter笔记:Box协议的布局约束原理与应用
92 0
|
算法 容器
flutter 约束(constrains )实例研究(下)
flutter 约束(constrains )实例研究(下)
162 0
flutter 约束(constrains )实例研究(下)
|
Web App开发 前端开发 Go
flutter 约束(constrains )实例研究(上)
flutter 约束(constrains )实例研究
126 0
flutter 约束(constrains )实例研究(上)
flutter系列之:把box布局用出花来
flutter中的layout有很多,基本上看layout的名字就知道这个layout到底是做什么用的。比如说这些layout中的Box,从名字就知道这是一个box的布局,不过flutter中的box还有很多种,今天我们来介绍最常用的LimitedBox,SizedBox和FittedBox。
flutter系列之:把box布局用出花来
|
容器
Flutter 67: 图解基本约束 Box (二)
0 基础学习 Flutter,第六十七步:学习基础约束 Box (二)!
1198 0
Flutter 67: 图解基本约束 Box (二)
|
容器
Flutter 68: 图解基本约束 Box (三)
0 基础学习 Flutter,第六十八步:学习以下基本约束 Box (三)!
1230 0
|
1月前
|
Android开发 iOS开发 容器
鸿蒙harmonyos next flutter混合开发之开发FFI plugin
鸿蒙harmonyos next flutter混合开发之开发FFI plugin
|
28天前
|
开发者
鸿蒙Flutter实战:07-混合开发
鸿蒙Flutter混合开发支持两种模式:1) 基于har包,便于主项目开发者无需关心Flutter细节,但不支持热重载;2) 基于源码依赖,利于代码维护与热重载,需配置Flutter环境。项目结构包括AppScope、flutter_module等目录,适用于不同开发需求。
69 3
|
13天前
|
传感器 开发框架 物联网
鸿蒙next选择 Flutter 开发跨平台应用的原因
鸿蒙(HarmonyOS)是华为推出的一款旨在实现多设备无缝连接的操作系统。为了实现这一目标,鸿蒙选择了 Flutter 作为主要的跨平台应用开发框架。Flutter 的跨平台能力、高性能、丰富的生态支持和与鸿蒙系统的良好兼容性,使其成为理想的选择。通过 Flutter,开发者可以高效地构建和部署多平台应用,推动鸿蒙生态的快速发展。
114 0