Flutter——Container
简介
故名思义,Container是Flutter中的布局容器,类似HTML中的DIV。它是一个组合的widget,内部有绘制widget、定位widget、尺寸widget。
Container是一个StatelessWidget。Container并不是一个最基础的widget,它是由一系列的基础widget组合而成。
属性介绍
alignment
控制child的对齐方式,如果container或者container父节点尺寸大于child的尺寸,这个属性设置会起作用,有很多种对齐方式。
/// 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);/// The bottom right corner.static const Alignment bottomRight = Alignment(1.0, 1.0);
padding
decoration内部的空白区域,如果有child的话,child位于padding内部
padding与margin的不同之处在于,padding是包含在content内,而margin则是外部边界,设置点击事件的话,padding区域会响应,而margin区域不会响应。
padding的设置枚举在EdgeInsets类,edge_insets.dart中,使用时可以自行查阅代码及注释。
color
用来设置container背景色,如果foregroundDecoration设置的话,可能会遮盖color效果。
color的设置枚举:
/// Construct a color from the lower 8 bits of four integers.////// * `a` is the alpha value, with 0 being transparent and 255 being fully/// opaque./// * `r` is [red], from 0 to 255./// * `g` is [green], from 0 to 255./// * `b` is [blue], from 0 to 255.////// Out of range values are brought into range using modulo 255.////// See also [fromRGBO], which takes the alpha value as a floating point/// value.const Color.fromARGB(int a, int r, int g, int b) :value = (((a & 0xff) << 24) |((r & 0xff) << 16) |((g & 0xff) << 8) |((b & 0xff) << 0)) & 0xFFFFFFFF;
decoration
绘制在child后面的装饰,设置了decoration的话,就不能设置color属性,否则会报错,此时应该在decoration中进行颜色的设置。
Decoration是一个相对复杂的属性,允许我们用比较复杂的结果作为背景。我们可以设置指定颜色和圆角的边框、背景颜色等等属性。
示例代码:
decoration: 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('http://h.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=0d023672312ac65c67506e77cec29e27/9f2f070828381f30dea167bbad014c086e06f06c.jpg'),centerSlice: new Rect.fromLTRB(270.0, 180.0, 1360.0, 730.0),),)
foregroundDecoration
foregroundDecoration与decoration相对,只不过它是绘制在child前面的decoration。其他与decoration属性相同。
width
container的宽度,设置为double.infinity可以强制在宽度上撑满,不设置,则根据child和父节点两者一起布局。
height
container的高度,设置为double.infinity可以强制在高度上撑满。
constraints
添加到child上额外的约束条件。这个会在专门的博文中进行讲解。
margin
围绕在decoration和child之外的空白区域,不属于内容区域。
transform
设置container的变换矩阵,类型为Matrix4。
child
container中的内容widget。
一个例子
class TestContainer extends State {@overrideWidget build(BuildContext context) {return Center(child: Container(alignment: Alignment.bottomCenter,padding: EdgeInsets.only(left: 1, top: 1, right: 1, bottom: 1),color: Color.fromARGB(255, 148, 120, 31),decoration: BoxDecoration(border: Border.all(width: 2.0, color: Colors.red),color: Colors.grey,borderRadius: BorderRadius.all(new Radius.circular(20.0)),image: DecorationImage(image: NetworkImage('http://h.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=0d023672312ac65c67506e77cec29e27/9f2f070828381f30dea167bbad014c086e06f06c.jpg'),centerSlice: Rect.fromLTRB(270.0, 180.0, 1360.0, 730.0),),),constraints: BoxConstraints.expand(height:Theme.of(context).textTheme.display1.fontSize * 1.1 + 200.0,),margin: EdgeInsets.only(left: 1, top: 1, right: 1, bottom: 1),transform: Matrix4.rotationZ(0.3),),);}}
以上就是Flutter中Container的基本使用。如有问题,欢迎指正。
