【Flutter学习】基本组件之容器组件Container
一,前言
Flutter控件本身通常由许多小型、单用途的控件组成,结合起来产生强大的效果,例如,Container是一种常用的控件,由负责布局、绘画、定位和大小调整的几个控件组成,具体来说,Container是由LimitedBox、ConstrainedBox、 Align、Padding、DecoratedBox和Transform控件组成,而不是将Container子类化来产生自定义效果,您可以用这种新颖的方式组合这些以及其他简单的控件。
二,基本组件 -- Container
容器,一个常用的控件,由基本的绘制、位置和大小控件组成。负责创建矩形的可视元素,可以用BoxDecoration来设计样式,比如背景、边框和阴影,Container也有边距、填充和大小限制,另外,还可以在三维空间利用矩阵进行变换。
没有子控件的容器尽可能大,除非传入的大小约束是无限的,在这种情况下,它们尽可能小。有子控件的容器将自己的尺寸给他们的孩子。我们可以通过width、height和 constraints属性控制size。
- 继承关系
Object > Diagnosticable > DiagnosticableTree > Widget > StatelessWidget > Container
- 行为
由于Container结合了许多其他Widget,每个Widget都有自己的布局行为,因此Container的布局行为有点复杂。
依次是:
.采用alignment
.以child调整自身大小
.采用了width,height和constraints
.扩大以适应父Widget
.要尽可能小具体情况来说:
· 如果Container没有子Widget,没有height,没有width,没有constraints,并且父窗口提供无限制约束,则Container尝试尽可能小。
· 如果Container没有子Widget,没有alignment,而是一个height,width或 constraints提供,Container试图给出这些限制和父Widget的约束相结合,以尽可能小。
· 如果Container没有子Widget,没有height,没有width,没有constraints,没有alignment,但是父窗口提供了有界约束,那么Container会扩展以适应父窗口提供 的约束。
· 如果Container具有alignment,并且父窗口提供无限制约束,则constraints会尝试围绕子Widget的alignment自身大小。
· 如果Container具有alignment,并且父窗口提供有界约束,则constraints会尝试展开以适合父窗口,然后根据alignment将子项置于其自身内。
· Container具有子Widget,但没有height,没有width,没有constraints,没有alignment,将父级constraints传递给子级,自身调整大小。 构造方法讲解
Container({
Key key,
this.alignment,//控制child的对齐方式
this.padding, //设置内边距
Color color, //设置背景颜色
Decoration decoration,//绘制在child下层的装饰,不能与color同时使用
this.foregroundDecoration,//绘制在child上层的装饰
double width, //宽
double height, //高
BoxConstraints constraints,添加到child上额外的约束条件
this.margin,//外边距
this.transform,//设置container的变换矩阵,类型为Matrix4
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)".')/**
const BoxDecoration({
this.color,
this.image,
this.border,
this.borderRadius,
this.boxShadow,//border四周添加阴影效果
this.gradient,//装饰器的过度效果,比如可以用来给组件添加一个蒙层
this.backgroundBlendMode,
this.shape = BoxShape.rectangle,
})
*/- 参数详解
alignment(这个属性是针对容器中的child的对其方式)
- topCenter:顶部居中对齐
- topLeft:顶部左对齐
- topRight:顶部右对齐
- center:水平垂直居中对齐
- centerLeft:垂直居中水平居左对齐
- centerRight:垂直居中水平居右对齐
- bottomCenter底部居中对齐
- bottomLeft:底部居左对齐
- bottomRight:底部居右对齐
- decoration(容器的修饰器,用于背景或者border)
如果在decoration中用了color,就把容器中设置的color去掉,不要重复设置color,设置的边框样式是让四条边框的宽度为8px,颜色为黑色
- decoration(容器的修饰器,用于背景或者border)
- margin(margin属性是表示Container与外部其他组件的距离)
- padding(指Container边缘与Child之间的距离)
padding: EdgeInsets.all(20.0),
- transform(可以根据自己的需要调整旋转的角度)
transform: Matrix4.rotationZ(0.2)
三,示例demo
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Container组件demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new CenterDemoPage() ,
);
}
} class CenterDemoPage extends StatefulWidget{
@override
createState() => new CenterDemoPageState();
} class CenterDemoPageState extends State<CenterDemoPage>{
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Container Widget Demo'),
),
body:_buildDemo() ,
);
} Widget _buildDemo(){
return new Center(
child: Container(
child: new Text('Hello Wolrd !'),
alignment: Alignment.center, //设置对齐方式
width: , //宽
height:, //高
// color: Colors.redAccent,
//设置边框
decoration: new BoxDecoration(
gradient: new LinearGradient(
begin: Alignment.topLeft,
end: Alignment(0.8, ),
colors: [const Color(0xFFFFFFEE), const Color(0xFF999999)]
),
color: Colors.blue,
border: Border.all(
color: Colors.black,
width: 8.0,
),
),
//内边距
padding: EdgeInsets.all(20.0),
margin: EdgeInsets.all(10.0),
//旋转
transform: Matrix4.rotationZ(0.4),
),
);
}
}
效果:
四,官网用法
【Flutter学习】基本组件之容器组件Container的更多相关文章
- Flutter学习笔记(9)--组件Widget
如需转载,请注明出处:Flutter学习笔记(9)--组件Widget 在Flutter中,所有的显示都是Widget,Widget是一切的基础,我们可以通过修改数据,再用setState设置数据(调 ...
- React-UI组件和容器组件
UI组件负责页面的渲染,又叫傻瓜组件. 容器组件负责逻辑,又叫聪明组件. 当一个组件只有render函数的时候,就可以用无状态组件的形式来定义这个组件.无状态组件怎么定义呢?其实就是一个函数,接受pr ...
- 15. react UI组件和容器组件的拆分 及 无状态组件
1.组件的拆分 组件拆分的前提 当所有的逻辑都出现在一个组件内时 组件会变得非常复杂 不便与代码的维护 所以对组件进行拆分 IU组件 进行页面渲染 容器组件 进行逻辑操作 UI组件的拆分 新建一个 ...
- Flutter——Container组件(容器组件)
名称 功能 alignment topCenter:顶部居中对齐 topLeft:顶部左对齐 topRight:顶部右对齐 center:水平垂直居中对齐 centerLeft:垂直居中水平居左对齐 ...
- Flutter学习笔记(10)--容器组件、图片组件
如需转载,请注明出处:Flutter学习笔记(10)--容器组件.图片组件 上一篇Flutter学习笔记(9)--组件Widget我们说到了在Flutter中一个非常重要的理念"一切皆为组件 ...
- 展示组件(Presentational component)和容器组件(Container component)之间有何不同
展示组件关心组件看起来是什么.展示专门通过 props 接受数据和回调,并且几乎不会有自身的状态,但当展示组件拥有自身的状态时,通常也只关心 UI 状态而不是数据的状态.(子组件)容器组件则更关心组件 ...
- Vue之合理划分容器组件与展示组件
Vue之合理划分容器组件与展示组件 时间 2019-06-02 00:30:32 Poetry's Blog 原文 http://blog.poetries.top/2019/06/01/vue- ...
- Flutter学习笔记(8)--Dart面向对象
如需转载,请注明出处:Flutter学习笔记(7)--Dart异常处理 Dart作为高级语言,支持面向对象的很多特性,并且支持基于mixin的继承方式,基于mixin的继承方式是指:一个类可以继承自多 ...
- Flutter Container容器组件、Text文本组件详解
import 'package:flutter/material.dart'; void main(){ runApp(MyApp()); } class MyApp extends Stateles ...
随机推荐
- <自动化测试>之<Selenium API 的用法1>
今天,简单,举例说一下在用python+selenium中元素定位的主要方法,第一部分是单个元素的操作,第二部分是一类元素的操作,实际操作中注意区分 #!/usr/bin/env python # - ...
- taomcat中catalina.out文件
项目中发现在linux环境下布署的tomcat所占用的磁盘空间越来越大,是catalina.out 文件,每天几乎是2个G,发现可能会影响到tomcat服务(没确定)正常访问.文件这么大,其实已经无法 ...
- Losing session data in ASP.NET
Losing session data in ASP.NET By default Response.Redirect terminates thread execution and there mi ...
- Oracle 用户概念与基本操作
目录 目录 Oracle的用户 通过系统用户来登陆SQLPlus system和sys的区别 查看登陆的用户 启用和锁定一个用户 启用用户 锁定用户 创建用户 修改用户 删除用户 角色权限 常用的用户 ...
- kali开启禁止或删除ssh 开机启动
开启禁止或删除ssh 开机启动 # update-rc.d ssh enable #//开机启动 # update-rc.d ssh disable #//禁止开机启动 # update-rc.d - ...
- 27. Unittest单元测试框架的介绍与使用
unittest单元测试框架 先贴一下unittest官网地址.unittest文档开头介绍了四个重要的概念:test fixture,test case, test suite, test runn ...
- Error configuring application listener of class org.springframework.web.context.
1.java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor 缺少asm-3.3.jar 2.java.lang.NoClassDe ...
- VS2008的使用
文章转载自:http://www.cnblogs.com/aduck/archive/2011/11/11/2245460.html 1.如何在vc2008中显示行号 中文版: 菜单-工具-选项 在新 ...
- 关于禁止微信端webapp内部下拉出现QQ浏览器的问题
<!DOCTYPE html> <html> <head> <title></title> </head> <style ...
- mysql中BLACKHOOL的作用
MySQL在5.x系列提供了Blackhole引擎–"黑洞". 其作用正如其名字一样:任何写入到此引擎的数据均会被丢弃掉, 不做实际存储:Select语句的内容永远是空. 和Lin ...