Flutter之Container详解
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详解的更多相关文章
- Flutter 布局(一)- Container详解
本文主要介绍Flutter中非常常见的Container,列举了一些实际例子介绍如何使用. 1. 简介 A convenience widget that combines common painti ...
- Container详解
Container是一个拥有绘制.定位.调整大小的widget. padding和margin padding和margin分别设置Container的内边距和外边距.可取值包括下面四个: EdgeI ...
- 从零学习Fluter(七):Flutter打包apk详解
写一个win上 flutter 打包apk的教程 这篇文档介绍一下flutter打包发布正式版apk 整体来看,和命令行打包rn的方法相差不大 打包前先做检查工作&查看构建配置 Android ...
- 一起看 I/O | Flutter 3 更新详解
作者 / Kevin Jamaul Chisholm, Technical Program Manager for Dart and Flutter at Google 又到了 Flutter 稳定版 ...
- Flutter 布局详解
本文主要介绍了Flutter布局相关的内容,对相关知识点进行了梳理,并从实际例子触发,进一步讲解该如何去进行布局. 1. 简介 在介绍Flutter布局之前,我们得先了解Flutter中的一些布局相关 ...
- flutter系列之:flutter中常用的Stack layout详解
[toc] 简介 对于现代APP的应用来说,为了更加美观,通常会需要用到不同图像的堆叠效果,比如在一个APP用户背景头像上面添加一个按钮,表示可以修改用户信息等. 要实现这样的效果,我们需要在一个Im ...
- Flutter完整开发实战详解
Flutter完整开发实战详解(一.Dart语言和Flutter基础) Flutter完整开发实战详解(二. 快速开发实战篇) Flutter完整开发实战详解(三. 打包与填坑篇)
- Flutter学习笔记(15)--MaterialApp应用组件及routes路由详解
如需转载,请注明出处:Flutter学习笔记(15)--MaterialApp应用组件及routes路由详解 最近一段时间生病了,整天往医院跑,也没状态学东西了,现在是好了不少了,也该继续学习啦!!! ...
- 【Flutter】功能型组件之对话框详解
前言 对话框本质上也是UI布局,通常一个对话框会包含标题.内容,以及一些操作按钮,为此,Material库中提供了一些现成的对话框组件来用于快速的构建出一个完整的对话框. 接口描述 // 1. Ale ...
随机推荐
- ContentTypes 的应用
ContentTypes django 中的一个应用程序(app),可以跟踪Django项目中安装的所有模型(Model),提供用于处理模型的高级通用接口. Contenttypes应用的核心是Con ...
- (5)Python字典
- [Algorithm] 如何正确撸<算法导论>CLRS
其实算法本身不难,第一遍可以只看伪代码和算法思路.如果想进一步理解的话,第三章那些标记法是非常重要的,就算要花费大量时间才能理解,也不要马马虎虎略过.因为以后的每一章,讲完算法就是这样的分析,精通的话 ...
- (四)JavaScript 语句
JavaScript 语句 JavaScript 语句是发给浏览器的命令. 这些命令的作用是告诉浏览器要做的事情. 下面的 JavaScript 语句向 id="demo" 的 H ...
- Spring Security(十三):5.2 HttpSecurity
Thus far our WebSecurityConfig only contains information about how to authenticate our users. How do ...
- BZOJ 5306 [HAOI2018] 染色
BZOJ 5306 [HAOI2018] 染色 首先,求出$N$个位置,出现次数恰好为$S$的颜色至少有$K$种. 方案数显然为$a_i=\frac{n!\times (m-i)^{m-i\times ...
- java 变量 final 小结
通过查看hashCode发现,变量声明final后,不能修改,上级修改时候,重新获得对象hashCode变化 public static void main(String[] args) { // T ...
- 开源工具 DotnetRSA 快速生成和转换RSA秘钥
一.简介 DotnetRSA 是一个利用 .NET Core 2.1 开发的 .NET Global Tool,是可以想npm全局安装一样,安装在你的系统中,只需敲一行命令便可以快速生成RSA加密算法 ...
- [翻译] ASP.NET Core 2.1.0 发布
原文: ASP.NET Core 2.1.0 now available 今天,我们很高兴可以发布 ASP.NET Core 2.1.0!这是我们 .NET平台下开源的.跨平台的 Web 框架的最新版 ...
- Groovy语言学习--语法基础(5)
至此groovy语言学习-语法基础就结束了,新的工作已经安排下来,要干活了. 对groovy了解到一定程度之后就可以在java项目中对其进行引入了.为此新建了一个微型的项目,个人觉得千言万语不如代码实 ...