作者 | 弗拉德

来源 | 弗拉德(公众号: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. mq中nio

    MappedFile#appendMessagesInner

  2. 移动端和web端的性能指标

    移动端的性能指标: 1.内存:80% 2.CPU 3.流量 4.电量 5.启动速度 6.滑动速度.界面切换速度 7.与服务器交互的网络速度 web端的性能指标: 1.CPU 2.内存 3.网络 4.I ...

  3. 等待多线程完成的CountDownLatch(带示例)

    开始磨刀霍霍向多线程了,这期是 CountDownLatch 的一个小示例. 定义:CountDownLatch 允许一个或多个线程等待其他线程完成操作. 应用需求举例:假设有4个线程,A.B.C.D ...

  4. MySQL重做日志(redo log)

    前面介绍了三种日志:error log.slow log.binlog,这三种都是 Server 层的.今天的 redo log 是 InnoDB引擎专有的日志文件. 为什么要有 redo log 用 ...

  5. Python自动发射弹幕

    Python自动发射弹幕,弹幕护体 - 环境: Python3+Windows- 开发工具: PyCharm 学习效果:1. 学会使用Python刷弹幕2. 配置INI文件信息3. 掌握网络请求知识4 ...

  6. pytorch 实践中遇到的问题

    1. SGD训练时,初始化学习率为0.05时,loss出现了 nan (百度: pytorch loss nan, 但是目前暂未看懂解释,大概是loss出现了inf,学习率偏大?)

  7. Jquery返回顶部插件

    自己jquery开发的返回顶部,当时只为了自己用一下,为了方便,修改成了插件... 自己的博客现在用的也是这个插件..使用方便!! <!DOCTYPE html> <html> ...

  8. 响应式网站css reset

    响应式网站 css reset /* core.css v1.1 | MIT License | corecss.io */ html { font-family: sans-serif; font- ...

  9. 使用darkarmour免杀mimikatz

    darkarmour是一个可用来免杀exe的项目,github地址:https://github.com/bats3c/darkarmour 我们使用darkarmour来免杀mimikatz. ./ ...

  10. P7077 函数调用

    我好蠢啊... 考试的时候不会写,现在看了这么多篇题解还是似懂非懂,所以决定写一下草稿... 草稿 和 题解 就是首先,题目保证了函数不会间接的调用其本身,所以可以直接知道这是一个 \(\text{D ...