作者 | 弗拉德

来源 | 弗拉德(公众号:fulade_me)

Container

我们先来看一下Container初始化的参数:

  Container({
Key key,
// 位置 居左、居右、居中
this.alignment,
// EdgeInsets Container的内边距
this.padding,
// 背景颜色
this.color,
// 背景装饰器
this.decoration,
// 前景装饰器
this.foregroundDecoration,
// 宽度
double width,
// 告诉
double height,
// 约束
BoxConstraints constraints,
// EdgeInsets Container的外边距
this.margin,
// 旋转
this.transform,
// 子控件
this.child,
// 裁剪Widget的模式
this.clipBehavior = Clip.none,
})

注意:

  • Containercolor属性与属性 decorationcolor存在冲突,如果两个color都做了设置,默认会以decorationcolor为准。
  • 如果我们没有给Container设置widthheightContainer会跟child的大小一样;假如我们没有设置child的时候,它的尺寸会极大化,尽可能的充满它的父Widget

1. 最简单的Container

Container(
child: Text("Fulade"),
color: Colors.red,
)

Container接收一个child参数,我们可以传入Text作为child参数,然后传入是一个颜色。

2. Padding

Container(
child: Text("Pading 10"),
padding: EdgeInsets.all(10),
color: Colors.blue,
)

Padding是内边距,我们在这里设置了padding: EdgeInsets.all(10),也就是说Text距离Container的四条边的边距都是10。

3. Margin

Container(
child: Text("Margin 10"),
margin: EdgeInsets.all(10),
color: Colors.green,
)

Margin是外边距,我们在这里设置了margin: EdgeInsets.all(10)Container在原有大小的基础上,又被包围了一层宽度为10的矩形。

需要注意,绿色外围的白色区域也是属于Container的一部分。

4. transform

Container(
padding: EdgeInsets.symmetric(horizontal: 15),
margin: EdgeInsets.all(10),
child: Text("transform"),
transform: Matrix4.rotationZ(0.1),
color: Colors.red,
)

transform可以帮助我们做旋转,Matrix4给我们提供了很多的变换样式。

5. decoration

decoration可以帮助我们实现更多的效果。例如形状、圆角、边界、边界颜色等。

Container(
child: Text("Decoration"),
padding: EdgeInsets.symmetric(horizontal: 15),
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.red,
shape: BoxShape.rectangle,
borderRadius: BorderRadius.all(Radius.circular(5)),
),
)



这里就是设置了一个圆角的示例,同样我们对BoxDecorationcolor属性设置颜色,对整个Container的也是有效的。

6. 显示 Image

Container(
height: 40,
width: 100,
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("images/flutter_icon_100.png"),
fit: BoxFit.contain,
),
),
)

BoxDecoration可以传入一个Image对象,这样就灵活了很多,Image可以来自本地也可以来自网络。

7. Border

Container(
child: Text('BoxDecoration with border'),
padding: EdgeInsets.symmetric(horizontal: 15),
margin: EdgeInsets.all(5),
decoration: BoxDecoration(
borderRadius: BorderRadius.circula(12),
border: Border.all(
color: Colors.red,
width: 3,
),
),
)

使用border可以帮助我们做边界效果,还可以设置圆角borderRadius,也可以设置border的宽度,颜色等。

8. 渐变色

Container(
padding: EdgeInsets.symmetric(horizontal: 20),
margin: EdgeInsets.all(20), //容器外填充
decoration: BoxDecoration(
gradient: RadialGradient(
colors: [Colors.blue, Colors.black, Colors.red],
center: Alignment.center,
radius: 5
),
),
child: Text(
//卡片文字
"RadialGradient",
style: TextStyle(color: Colors.white),
),
)

BoxDecoration的属性gradient可以接收一个颜色的数组,Alignment.center是渐变色开始的位置,可以从左上角右上角中间等位置开始颜色变化。

想体验以上的Container的示例的运行效果,可以到我的Github仓库项目flutter_app->lib->routes->container_page.dart查看,并且可以下载下来运行并体验。


【Flutter 1-16】Flutter手把手教程UI布局和Widget——容器控件Container的更多相关文章

  1. Flutter学习指南:UI布局和控件

    Flutter学习指南:UI布局和控件 - IT程序猿  https://www.itcodemonkey.com/article/11041.html

  2. 聊聊flutter的UI布局

    UI布局多半是套路,熟悉套路的规则. Flutter的UI布局也有一套规则 center center可以让任何元素在屏幕中居中,既是水平居中又是垂直居中,如果想让元素从上而下排列要怎么办呢?那就得使 ...

  3. Flutter实战视频-移动电商-64.会员中心_顶部头像UI布局

    64.会员中心_顶部头像UI布局 会员中心的样式 member.dart 清除原来的代码生成一个基本的结构 默认返回一个scaffold脚手架工具,body里面布局使用ListView,这样不会出现纵 ...

  4. Flutter实战视频-移动电商-65.会员中心_订单区域UI布局

    65.会员中心_订单区域UI布局 我的订单区域 member.dart写我的标题的方法 布局使用瓦片布局 先做修饰,decoration颜色的背景,下边线的样式 //我的订单标题 Widget _or ...

  5. Flutter免费(视频)教程汇总

    Flutter学习导航 Flutter简介: Flutter可以轻松快速地构建漂亮的移动应用程序. Flutter是谷歌的移动应用SDK,用于短时间内在iOS和Android上制作高质量的原生界面应用 ...

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

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

  7. Flutter学习笔记(22)--单个子元素的布局Widget(Container、Padding、Center、Align、FittedBox、Offstage、LimitedBox、OverflowBox、SizedBox)

    如需转载,请注明出处:Flutter学习笔记(22)--单个子元素的布局Widget(Container.Padding.Center.Align.FittedBox.Offstage.Limited ...

  8. [Flutter] Windows平台Flutter开发环境搭建(Andorid Studio)

    前两天网友在群里说起了Flutter,就了解了一下,在手机上跑了它的demo,直接就被打动了. 虽然网上有很多教程,但真正开始的时候,还是会碰到很多坑.下面详细的讲解Flutter + Android ...

  9. flutter系列之:flutter中常用的container layout详解

    目录 简介 Container的使用 旋转Container Container中的BoxConstraints 总结 简介 在上一篇文章中,我们列举了flutter中的所有layout类,并且详细介 ...

随机推荐

  1. mysql GTID主从复制故障后不停机恢复同步流程

    GTID实现主从复制数据同步 GTID是一个基于原始mysql服务器生成的一个已经被成功执行的全局事务ID,它由服务器ID以及事务ID组成,这个全局事务ID不仅仅在原始服务器上唯一,在所有主从关系的m ...

  2. 数论之prufer序列

    定义 \(Prufer\) 数列是无根树的一种数列. 在组合数学中,\(Prufer\) 数列由有一个对于顶点标过号的树转化来的数列,点数为 \(n\) 的树转化来的 \(Prufer\) 数列长度为 ...

  3. 天池新闻推荐比赛1:赛题理解+baseline

    天池新闻推荐比赛1:赛题理解+baseline 一.比赛信息 比赛链接: ​ https://tianchi.aliyun.com/competition/entrance/531842/inform ...

  4. LeetCode 032 Longest Valid Parentheses

    题目描述:Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the l ...

  5. 微信小程序里如何使用npm?小程序集成友盟举例

    1.执行npm初始化指令 小程序根目录,命令执行如下指令: npm init 执行后会让加载项目初始信息,具体截图如下: 2.执行安装npm包指令 在这我们举个例子,以接入友盟统计SDK为例,执行命令 ...

  6. 安装spyder记录

    sudo apt-get install spyder 报错:ERROR: Could not find a version that satisfies the requirement pyqt5& ...

  7. 音视频处理基础知识扫盲:数字视频YUV像素表示法以及视频帧和编解码概念介绍

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt+moviepy音视频剪辑实战 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一. ...

  8. 第11.14节 正则表达式转义符和Python转义符相同引发问题的解决办法

    正则表达式使用反斜杠('\')来把特殊字符转义成普通字符(为了方便称为"正则表达式转义"),而反斜杠在普通的 Python 字符串里也是转义符(称为"字符串转义" ...

  9. 第12.2节 Python sys模块导览

    sys模块包括一些用于系统处理的功能,常用的成员包括: sys.argv:当前执行进程的命令参数列表,不含执行程序本身的名字: sys.stdin .sys.stdout 和 stderr :分别对应 ...

  10. 问题:PyCharm调试方法Force run to cursor与run to cursor的区别

    Force run to cursor与run to cursor的差别是,后者在执行到光标的代码行前,如果有代码中设置了断点,会在该断点处暂停,等待进一步调试指令,而Force run to cur ...