Flutter之ConstrainedBox、SizedBox、UnconstrainedBox(尺寸限制类容器)

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: Flutter之ConstrainedBox、SizedBox、UnconstrainedBox(尺寸限制类容器)

1 ConstrainedBox、SizedBox、UnconstrainedBox介绍

1)、ConstrainedBox用于对子组件添加额外的约束。例如,如果你想让子组件的最小高度是80像素

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

们可以看到这里有个constraints

  final BoxConstraints constraints;
class BoxConstraints extends Constraints {
  /// Creates box constraints with the given constraints.
  const BoxConstraints({
    this.minWidth = 0.0,
    this.maxWidth = double.infinity,
    this.minHeight = 0.0,
    this.maxHeight = double.infinity,
  }) : assert (minWidth != null),
       assert (maxWidth != null),
       assert (minHeight != null),
       assert (maxHeight != null);

我们可以看到BoxConstraints继承Constraints,然后一些属性设置。

有多重ConstrainedBox限制时,对于minWidth和minHeight来说,是取父子中相应数值较大的

2)、SizedBox:用于给子元素指定固定的宽高

3)、UnconstrainedBox:不会对子组件产生任何限制,它允许其子组件按照其本身大小绘制

一般用来去掉父约束


2 测试代码

测试1、

  @override
  Widget build(BuildContext context) {
      return MaterialApp(
          title: 'open url',
          home: Scaffold(
            appBar: AppBar(
              // Here we take the value from the MyHomePage object that was created by
              // the App.build method, and use it to set our appbar title.
              title: Text('hello flutter'),
            ),
            body: Center(
              child: ConstrainedBox(
                  constraints: BoxConstraints(
                      minWidth: double.infinity,
                      minHeight: 100
                  ),
                child: Container(
                    width: 50,
                    height: 50,
                    color: Colors.green,
                ),
              ),
            ),
          ),
      );
  }
}

测试2、

  @override
  Widget build(BuildContext context) {
      return MaterialApp(
          title: 'open url',
          home: Scaffold(
            appBar: AppBar(
              title: Text('hello flutter'),
            ),
            body: Center(
              child: ConstrainedBox(
                  constraints: BoxConstraints(
                      minWidth: 200,
                      minHeight: 100
                  ),
                child: ConstrainedBox(
                  constraints: BoxConstraints(
                      minWidth: 100,
                      minHeight: 200
                  ),
                  child:  Container(
                    width: 50,
                    height: 50,
                    color: Colors.green,
                  ),
                ),
              ),
            ),
          ),
      );
  }
}

测试3、

  @override
  Widget build(BuildContext context) {
      return MaterialApp(
          title: 'open url',
          home: Scaffold(
            appBar: AppBar(
              title: Text('hello flutter'),
            ),
            body: Center(
              child: ConstrainedBox(
                  constraints: BoxConstraints(
                      minWidth: 200,
                      minHeight: 100
                  ),
                child: UnconstrainedBox(
                  child: ConstrainedBox(
                    constraints: BoxConstraints(
                        minWidth: 50,
                        minHeight: 50
                    ),
                    child: Container(
                      width: 10,
                      height: 15,
                      color: Colors.red,
                    ),
                  ),
                ),
              ),
            ),
          ),
      );
  }
}

3 运行效果

2020090822072095.jpg

20200908220741116.jpg

20200908220734125.jpg

相关文章
|
9月前
|
Ubuntu NoSQL Linux
《docker基础篇:3.Docker常用命令》包括帮助启动类命令、镜像命令、有镜像才能创建容器,这是根本前提(下载一个CentOS或者ubuntu镜像演示)、容器命令、小总结
《docker基础篇:3.Docker常用命令》包括帮助启动类命令、镜像命令、有镜像才能创建容器,这是根本前提(下载一个CentOS或者ubuntu镜像演示)、容器命令、小总结
522 6
《docker基础篇:3.Docker常用命令》包括帮助启动类命令、镜像命令、有镜像才能创建容器,这是根本前提(下载一个CentOS或者ubuntu镜像演示)、容器命令、小总结
|
12月前
|
搜索推荐
Flutter 中的 AnimationController 类
【10月更文挑战第18天】深入了解了 Flutter 中的 `AnimationController`类。它是构建精彩动画效果的重要基石,掌握它的使用方法对于开发具有吸引力的 Flutter 应用至关重要。
325 59
|
安全 算法 Java
【Java集合类面试二】、 Java中的容器,线程安全和线程不安全的分别有哪些?
这篇文章讨论了Java集合类的线程安全性,列举了线程不安全的集合类(如HashSet、ArrayList、HashMap)和线程安全的集合类(如Vector、Hashtable),同时介绍了Java 5之后提供的java.util.concurrent包中的高效并发集合类,如ConcurrentHashMap和CopyOnWriteArrayList。
【Java集合类面试二】、 Java中的容器,线程安全和线程不安全的分别有哪些?
|
Java 容器
【Java集合类面试一】、 Java中有哪些容器(集合类)?
这篇文章列出了Java中的四大类集合接口:Set、List、Queue和Map,以及它们的常用实现类,如HashSet、TreeSet、ArrayList、LinkedList、ArrayDeque、HashMap和TreeMap。
【Java集合类面试一】、 Java中有哪些容器(集合类)?
|
容器
【Qt 学习笔记】Qt常用控件 | 容器类控件 | Group Box的使用及说明
【Qt 学习笔记】Qt常用控件 | 容器类控件 | Group Box的使用及说明
1147 3
|
Unix API 开发者
Flutter笔记:使用Flutter私有类涉及的授权协议问题
Flutter笔记:使用Flutter私有类涉及的授权协议问题
232 1
|
容器
【Qt 学习笔记】Qt常用控件 | 容器类控件 | Tab Widget的使用及说明
【Qt 学习笔记】Qt常用控件 | 容器类控件 | Tab Widget的使用及说明
1678 2
|
数据采集 监控 Kubernetes
Job类日志采集问题之iLogtail以减小容器发现和开始采集的延时如何优化
Job类日志采集问题之iLogtail以减小容器发现和开始采集的延时如何优化
|
数据采集 Kubernetes Java
Job类日志采集问题之在日志中添加容器的元信息标签,如何操作
Job类日志采集问题之在日志中添加容器的元信息标签,如何操作
|
存储 Kubernetes 数据处理
Job类日志采集问题之为什么Job容器的日志采集要考虑容器发现速度和开始采集延时,如何理解
Job类日志采集问题之为什么Job容器的日志采集要考虑容器发现速度和开始采集延时,如何理解