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. C#事件の事件聚合器

    事件聚合器用于集中管理事件的订阅(Subscribe)和处理(Handle),要使用事件聚合器,首先要理解:事件(event)本质上是一个类. 传统的+=和-=不足: 1.管理很麻烦:2.不方便扩展. ...

  2. numpy中矩阵乘法,星乘(*)和点乘(.dot)的区别

    import numpy a = numpy.array([[,], [,]]) b = numpy.array([[,], [,]]) 星乘表示矩阵内各对应位置相乘,矩阵a*b下标(0,0)=矩阵a ...

  3. 转://SQLNET.EXPIRE_TIME参数

    DCD: Dead Connection Detection ,可以用于检测.标记僵死而没有断开会session,再由PMON进行清理,释放资源.开启DCD,只需要在服务端的sqlnet.ora文件中 ...

  4. 小a与黄金街道 (欧拉函数)

    题意:a, b两个人在长度为n的一维数轴上(从1开始).a在1,b在n.每个人以1m/s的速度相向而行,则每一时刻存在坐标x,y,当cgd(n, x)==1,gcd(n, y)==1时,t1=k^x, ...

  5. 深度学习PyTorch环境安装——mac

    参考:http://python.jobbole.com/87522/ 1.首先要安装Anaconda 1)什么是Anaconda Anaconda是Python的包管理器和环境管理器,是一个包含18 ...

  6. Python脱产8期 Day04 2019/4/16

    流程控制 1.宏观一定是自上而下(逻辑上方代码一定比逻辑下方代码先执行):顺序结构2.遇到需要条件判断选择不同执行路线的执行方式:分支结构3.有些事情需要重复不断的去执行(当满足某种条件或不满足某种条 ...

  7. jupyter notebook 代码补全插件工具-nbextensions(并修改默认的工作目录)

    # conda install -c conda-forge jupyter_contrib_nbextensionsCollecting package metadata: doneSolving ...

  8. WCF无.SVC文件服务激活,及不添加服务引用调用WCF

    一,新建WCF服务引用程序 1,删除.svc文件,全部删除. 2,新建 IService 类 namespace TestWcf { [ServiceContract] public interfac ...

  9. BAT美团滴滴java面试大纲(带答案版)之三:多线程synchronized

    继续面试大纲系列文章. 从这一篇开始,我们进入ava编程中的一个重要领域---多线程!多线程就像武学中对的吸星大法,理解透了用好了可以得道成仙,俯瞰芸芸众生:而滥用则会遭其反噬. 在多线程编程中要渡的 ...

  10. python学习之第八篇——字典嵌套之字典中嵌套字典

    cities = { 'shanghai':{'country':'china','population':10000,'fact':'good'}, 'lendon':{'country':'eng ...