Flutter跨平台方案的横空出世, 其接近原生的体验, 吸引了一大批的开发者纷纷入坑; 某些知名大厂也已经在原有项目中引入了Flutter; 再不学习就晚了(可能现在已经晚了); 那就让我们开始吧
对于Flutter来说 万物皆Widget, 让我想起来那句经典的万物皆对象 今天就来聊聊一个基础的Widget --- Container
源码中可以看到Container中包含以下属性, 是时候揭开它神秘的面纱了
Container({
this.alignment,
this.padding,
Color color,
Decoration decoration,
Decoration foregroundDecoration,
double width,
double height,
BoxConstraints constraints,
this.margin,
this.transform,
this.child,
})
alignmentalignment是用来控制Container中子组件的排列方式, 常用的值Alignment.topLeft左上Alignment.topCenter上中Alignment.topRight右上Alignment.centerLeft左中Alignment.center居中Alignment.centerRight右中Alignment.bottomLeft左下Alignment.bottomCenter下中Alignment.bottomRight右下
//示例代码
Container(
alignment: Alignment.center,
color: Colors.red,
width: 100.0,
height: 100.0,
child: Container(
width: 30.0,
height: 30.0,
color: Colors.green,
)
)
marginpadding设置内外边距
EdgeInsets.all(value)用于设置四个方向上一样的值EdgeInsets.only(left:leftV, right: rightV, top: topV, bottom: bottomV)可以单独设置某个方向的值EdgeInsets.symmetric(horizontal: horV, vertical: verV)用于设置水平/垂直方向上的值EdgeInsets.fromLTRB(left, top, right, bottom)按照左上右下的顺序设置4个方向的值
//示例代码
Container(
margin: EdgeInsets.only(top: 10.0, left: 10.0),
padding: EdgeInsets.only(left: 10,top: 10),
width: 30.0,
height: 30.0,
color: Colors.green,
)
-
color用来设置Container的背景颜色, 设置颜色用到Color类, color属性和decoration属性不能同时存在, 如果同时存在会报错The following assertion was thrown building HomeController(dirty): Cannot provide both a color and a decoration The color argument is just a shorthand for "decoration: new BoxDecoration(color: color)".在使用decoration属性的时候想设置Container的背景颜色, 可以设置decoration的color属性Colors.redColor(0xFFFF0000)
-
decoration背景装饰 decoration的功能非常强大, 可以设置边框阴影渐变圆角通常会用BoxDecoration示例来设置这些属性边框 Border
Container(
decoration: BoxDecoration(
color: Colors.red,
border: Border.all(width: 2, color: Colors.blue, style: BorderStyle.solid),
borderRadius: BorderRadius.all(new Radius.circular(4.0)),
)
)
)
//同时设置4条边框
border: Border.all(color: Colors.orange, width: 2, style: BorderStyle.solid)
// 设置单边框
border:Border(
top: BorderSide(color: Colors.red, width: 2, style: BorderStyle.solid),
bottom: BorderSide(color: Colors.blue, width: 2, style: BorderStyle.solid)
)
阴影 BoxShadowOffset(double dx, double dy)dx 阴影水平偏移量,如果为正值,则阴影在对象右边, 反之在对象左边. dy 阴影的垂直偏移量, 如果为正值,则阴影在对象底部, 反之在对象顶部blurRadius阴影模糊半径, 只能是正数, 其值为0时,表示阴影不具有模糊效果, 值越大阴影的边缘就越模糊spreadRadius阴影扩展半径, 其值可正可负, 如果是正值, 则整个阴影都延展扩大, 反之则缩小color阴影颜色
Container(
decoration: BoxDecoration(
color: Colors.red,
boxShadow: [
BoxShadow(
color: Colors.black45,
offset: Offset(2.0, 2.0),
blurRadius: 4.0
)
],
),
)
BoxShadow(
offset: Offset(-10, -10),
blurRadius: 6,
spreadRadius: 10,
color: Colors.purple
)
渐变flutter支持线性渐变和径向渐变
Container(
decoration: BoxDecoration(
color: Colors.red,
gradient: RadialGradient(
colors: [Colors.red, Colors.green],
center: Alignment.topLeft,
radius: .98
),
),
width: 100.0,
height: 100.0,
)
//线性渐变
gradient: LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
colors: [Colors.red, Colors.green]
)
//从中心向四周扩散
gradient: RadialGradient(
center: Alignment.center,
colors: [Colors.green, Colors.red]
)
圆角BorderRadius类用来设置圆角
//同时设置四个角的圆角
borderRadius: BorderRadius.circular(10)
//设置单个角的圆角
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10),
bottomLeft: Radius.circular(20)
)
-
foregroundDecoration
前景装饰, 用法和decoration 一样 -
constraintsconstraints可以用来限制容器的宽高 minWidth maxWidth minHeight maxHeight 使用如下
容器的大小可以通过width、height属性来指定,也可以通过constraints来指定,
如果同时存在时,width、height优先。实际上Container内部会根据width、height来生成一个constraints。
constraints: BoxConstraints(
minHeight: 100,
maxHeight: 200,
minWidth: 100,
maxWidth: 200
),
transformtransform主要包括平移缩放旋转倾斜, 使用矩阵变换类Matrix4来实现变换Matrix4.translationValues(x, y, z),平移x y zMatrix4.rotationZ(radians),z轴旋转radiaMatrix4.rotationX(radians),x轴旋转radians弧度Matrix4.rotationY(radians),y轴旋转radians弧度Matrix4.skew(alpha, beta)x轴倾斜alpha度, y轴倾斜beta度skewX(alpha)x轴倾斜alpha度skewY(alpha)Y轴倾斜alpha度
Container(
color: Colors.red,
width: 200,
height: 200,
constraints: BoxConstraints.tightFor(width: 200.0, height: 150.0),
transform: Matrix4.rotationZ(.2),
)