1 基本内容
1.1 继续关系
Object > Diagnosticable > DiagnosticableTree > Widget > StatelessWidget > Container
注:所有控件都是Widget的子类!

1.2 介绍
一个便利的控件,结合了常见的绘画,定位和大小调整。

1.3 行为
由于Container结合了许多其他Widget,每个Widget都有自己的布局行为,因此Container的布局行为有点复杂。

依次是:

1.采用alignment

2.以child调整自身大小

3. 采用了width,height和constraints

4.扩大以适应父Widget

5.要尽可能小

具体情况来说:

1· 如果Container没有子Widget,没有height,没有width,没有constraints,并且父窗口提供无限制约束,则Container尝试尽可能小。

2· 如果Container没有子Widget,没有alignment,而是一个height,width或 constraints提供,Container试图给出这些限制和父Widget的约束相结合,以尽可能小。

3· 如果Container没有子Widget,没有height,没有width,没有constraints,没有alignment,但是父窗口提供了有界约束,那么Container会扩展以适应父窗口提供 的约束。

4· 如果Container具有alignment,并且父窗口提供无限制约束,则constraints会尝试围绕子Widget的alignment自身大小。

5· 如果Container具有alignment,并且父窗口提供有界约束,则constraints会尝试展开以适合父窗口,然后根据alignment将子项置于其自身内。

6· Container具有子Widget,但没有height,没有width,没有constraints,没有alignment,将父级constraints传递给子级,自身调整大小。

2 构造方法讲解
Container({
Key key,
this.alignment,
this.padding,
Color color,
Decoration decoration,
this.foregroundDecoration,
double width,
double height,
BoxConstraints constraints,
this.margin,
this.transform,
this.child,
}) : assert(margin == null || margin.isNonNegative),
assert(padding == null || padding.isNonNegative),
assert(decoration == null || decoration.debugAssertIsValid()),
assert(constraints == null || constraints.debugAssertIsValid()),
assert(color == null || decoration == null,
'Cannot provide both a color and a decoration\n'
'The color argument is just a shorthand for "decoration: new BoxDecoration(color: color)".'
)
各参数详解

2.1 Key key:
您可以使用key来控制框架将在widget重建时与哪些其他widget匹配。默认情况下,框架根据它们的runtimeType和它们的显示顺序来匹配。 使用key时,框架要求两个widget具有相同的key和runtimeType。

Key在构建相同类型widget的多个实例时很有用。例如,List构建足够的ListItem实例以填充其可见区域:

如果没有key,当前构建中的第一个条目将始终与前一个构建中的第一个条目同步,即使在语义上,列表中的第一个条目如果滚动出屏幕,那么它将不会再在窗口中可见。

通过给列表中的每个条目分配为“语义” key,无限列表可以更高效,因为框架将同步条目与匹配的语义key并因此具有相似(或相同)的可视外观。 此外,语义上同步条目意味着在有状态子widget中,保留的状态将附加到相同的语义条目上,而不是附加到相同数字位置上的条目。

2.2 AlignmentGeometry alignment:
如果父Widget尺寸大于child Widget尺寸,这个属性设置会起作用,有很多种对齐方式。

AlignmentGeometry 是个抽象类

Alignment与AlignmentDirectional一样的,只是命名不一样,如Alignment中的topLeft跟AlignmentDirectional中的topStart一致。

Alignment主要以下对齐方式:

/// The top left corner.
  static const Alignment topLeft = Alignment(-1.0, -1.0);

/// The center point along the top edge.
  static const Alignment topCenter = Alignment(0.0, -1.0);

/// The top right corner.
  static const Alignment topRight = Alignment(1.0, -1.0);

/// The center point along the left edge.
  static const Alignment centerLeft = Alignment(-1.0, 0.0);

/// The center point, both horizontally and vertically.
  static const Alignment center = Alignment(0.0, 0.0);

/// The center point along the right edge.
  static const Alignment centerRight = Alignment(1.0, 0.0);

/// The bottom left corner.
  static const Alignment bottomLeft = Alignment(-1.0, 1.0);

/// The center point along the bottom edge.
  static const Alignment bottomCenter = Alignment(0.0, 1.0);
明显看出构造方法中的二个参数范围在[-1.0,1.0] 表示从顶部到底部或者从左到右

比如,想让child在居中Alignment(0.0, 0.0)

FractionalOffset继承Alignment

FractionalOffset构造方法可以看出,方法中的二个参数范围在[0.0,1.0]表示从顶部到底部或者从左到右

const FractionalOffset(double dx, double dy)
: assert(dx != null),
assert(dy != null),
super(dx * 2.0 - 1.0, dy * 2.0 - 1.0);
2.3 EdgeInsetsGeometry padding:
内边距:本Widget边框和内容区之间距离。

EdgeInsetsGeometry 是个抽象类:

同理:EdgeInsetsDirectional与EdgeInsets功能一致!

EdgeInsets比EdgeInsetsDirectional多了一个方法:

const EdgeInsets.all(double value)

: left = value, top = value, right = value, bottom = value;

他们的常用方法

const EdgeInsets.fromLTRB(this.left, this.top, this.right, this.bottom);

const EdgeInsetsDirectional.fromSTEB(this.start, this.top, this.end, this.bottom);

注意的是,四个参数都不能有负数

因为:限定了 assert(padding == null || padding.isNonNegative)

2.4 Color color:
Container背景色

一般通过 const Color(int value) : value = value & 0xFFFFFFFF; 创建对象,也可以使用Colors类中的静态成员,从value & 0xFFFFFFFF可看出,默认值会不透明的。

注意:

用来设置container背景色,如果foregroundDecoration设置的话,可能会遮盖color效果。

container背景色和decoration不能同时设置,

因为:Container构造方法限定了assert(color == null || decoration == null,

'Cannot provide both a color and a decoration\n'

'The color argument is just a shorthand for "decoration: new BoxDecoration(color: color)".'

)

2.5 Decoration decoration:
绘制背景图案。

将在下一篇文章中详细讲解Decoration (边框、圆角、阴影、形状、渐变、背景图像)

注意:

container背景色和decoration不能同时设置

2.6 Decoration  foregroundDecoration:
decoration是背景,foregroundDecoration是前景。设置了foregroundDecoration可能会遮盖child内容,一般半透明遮盖(蒙层)效果使用!

将在下一篇文章中详细讲解Decoration (边框、圆角、阴影、形状、渐变、背景图像等等)

2.7 width、height
width:container的宽度,设置为double.infinity可以强制在宽度上撑满,不设置,则根据child和父节点两者一起布局。

height:container的高度,设置为double.infinity可以强制在高度上撑满。

2.8 BoxConstraints constraints:
添加到child上额外的约束条件。

const BoxConstraints({
this.minWidth = 0.0,
this.maxWidth = double.infinity,
this.minHeight = 0.0,
this.maxHeight = double.infinity
});
明显是来设置child的宽高范围值。还有很多静态方法,不一一列出,看源码即可!

注意:

0.0表示最小,double.infinity为无限大

2.9 EdgeInsetsGeometry margin:
外边距:本Widget与父边框的距离。

值与padding一致。

2.10 Matrix4 transform:
4D Matrix(矩阵)后面文章会详解

3.0 Widget child:
控件内容widget。

3 效果及示例

代码:

import 'package:flutter/material.dart';

void main() {
runApp(new MaterialApp(
title: 'My app', // used by the OS task switcher
home: new MyStartUI(),
));
}

class MyStartUI extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Container(
constraints: new BoxConstraints.expand(
height:Theme.of(context).textTheme.display1.fontSize * 1.1 + 200.0,
),
decoration: new BoxDecoration(
border: new Border.all(width: 2.0, color: Colors.red),
color: Colors.grey,
borderRadius: new BorderRadius.all(new Radius.circular(20.0)),
image: new DecorationImage(
image: new NetworkImage('https://avatar.csdn.net/8/9/A/3_chenlove1.jpg'),
centerSlice: new Rect.fromLTRB(270.0, 180.0, 1360.0, 730.0),
),
),
padding: const EdgeInsets.all(5.0),
margin: const EdgeInsets.all(10.0),
alignment: Alignment.center,
child: new Text('Ying You Learning',
style: Theme.of(context).textTheme.display1.copyWith(color: Colors.red)),
transform: new Matrix4.rotationZ(0.0),
);
}

原文:https://blog.csdn.net/chenlove1/article/details/83032767

Flutter之Container详解的更多相关文章

  1. Flutter 布局(一)- Container详解

    本文主要介绍Flutter中非常常见的Container,列举了一些实际例子介绍如何使用. 1. 简介 A convenience widget that combines common painti ...

  2. Container详解

    Container是一个拥有绘制.定位.调整大小的widget. padding和margin padding和margin分别设置Container的内边距和外边距.可取值包括下面四个: EdgeI ...

  3. 从零学习Fluter(七):Flutter打包apk详解

    写一个win上 flutter 打包apk的教程 这篇文档介绍一下flutter打包发布正式版apk 整体来看,和命令行打包rn的方法相差不大 打包前先做检查工作&查看构建配置 Android ...

  4. 一起看 I/O | Flutter 3 更新详解

    作者 / Kevin Jamaul Chisholm, Technical Program Manager for Dart and Flutter at Google 又到了 Flutter 稳定版 ...

  5. Flutter 布局详解

    本文主要介绍了Flutter布局相关的内容,对相关知识点进行了梳理,并从实际例子触发,进一步讲解该如何去进行布局. 1. 简介 在介绍Flutter布局之前,我们得先了解Flutter中的一些布局相关 ...

  6. flutter系列之:flutter中常用的Stack layout详解

    [toc] 简介 对于现代APP的应用来说,为了更加美观,通常会需要用到不同图像的堆叠效果,比如在一个APP用户背景头像上面添加一个按钮,表示可以修改用户信息等. 要实现这样的效果,我们需要在一个Im ...

  7. Flutter完整开发实战详解

    Flutter完整开发实战详解(一.Dart语言和Flutter基础) Flutter完整开发实战详解(二. 快速开发实战篇) Flutter完整开发实战详解(三. 打包与填坑篇)

  8. Flutter学习笔记(15)--MaterialApp应用组件及routes路由详解

    如需转载,请注明出处:Flutter学习笔记(15)--MaterialApp应用组件及routes路由详解 最近一段时间生病了,整天往医院跑,也没状态学东西了,现在是好了不少了,也该继续学习啦!!! ...

  9. 【Flutter】功能型组件之对话框详解

    前言 对话框本质上也是UI布局,通常一个对话框会包含标题.内容,以及一些操作按钮,为此,Material库中提供了一些现成的对话框组件来用于快速的构建出一个完整的对话框. 接口描述 // 1. Ale ...

随机推荐

  1. php文件管理,能够点击依照时间,大小,名称排序

    php文件管理.能够点击依照时间.大小,名称排序  本例没实用到jquery 演示   PHP Code <?php    $rootdir="./";    $spacen ...

  2. tensorflow中的name_scope, variable_scope

    在训练深度网络时,为了减少需要训练参数的个数(比如LSTM模型),或者是多机多卡并行化训练大数据.大模型等情况时,往往就需要共享变量.另外一方面是当一个深度学习模型变得非常复杂的时候,往往存在大量的变 ...

  3. 002_pip安装失败

    一.安装cmdb的驱动遇到了如下问题 [root@localhost 003_pyenv]# pip2.7 install cmdb_sdk==0.3.1 -i http://cmdb.elenet. ...

  4. Python框架学习之Flask中的数据库操作

    数据库操作在web开发中扮演着一个很重要的角色,网站中很多重要的信息都需要保存到数据库中.如用户名.密码等等其他信息.Django框架是一个基于MVT思想的框架,也就是说他本身就已经封装了Model类 ...

  5. class文件魔数CAFEBABE的由来

    https://blog.csdn.net/ustcyy91/article/details/78462378 https://blog.csdn.net/renfufei/article/detai ...

  6. CRC-16 (Modbus)

    typedef unsigned char uint8_t; typedef unsigned short uint16_t; typedef unsigned int uint32_t; #defi ...

  7. [Flume][Kafka]Flume 与 Kakfa结合例子(Kakfa 作为flume 的sink 输出到 Kafka topic)

    Flume 与 Kakfa结合例子(Kakfa 作为flume 的sink 输出到 Kafka topic) 进行准备工作: $sudo mkdir -p /flume/web_spooldir$su ...

  8. 内联函数:static inline 和 extern inline 的含义

    引入内联函数的目的是为了解决程序中函数调用的效率问题. 函数是一种更高级的抽象.它的引入使得编程者只关心函数的功能和使用方法,而不必关心函数功能的具体实现:函数的引入可以减少程序的目标代码,实现程序代 ...

  9. CSS实现树形结构 + js加载数据

    看到一款树形结构,比较喜欢它的样式,就参照它的外观自己做了一个,练习一下CSS. 做出来的效果如下: li { position: relative; padding: 5px 0; margin:0 ...

  10. MyBatis动态SQL(认真看看, 以后写SQL就爽多了)

    目录 0 一起来学习 mybatis 1 数据准备 2 if 标签 2.1 在 WHERE 条件中使用 if 标签 2.1.1 查询条件 2.1.2 动态 SQL 2.1.3 测试 2.2 在 UPD ...