Flutter——Container

简介

故名思义,Container是Flutter中的布局容器,类似HTML中的DIV。它是一个组合的widget,内部有绘制widget、定位widget、尺寸widget。

Container是一个StatelessWidget。Container并不是一个最基础的widget,它是由一系列的基础widget组合而成。

属性介绍

alignment

控制child的对齐方式,如果container或者container父节点尺寸大于child的尺寸,这个属性设置会起作用,有很多种对齐方式。

  1. /// The top left corner.
  2. static const Alignment topLeft = Alignment(-1.0, -1.0);
  3. /// The center point along the top edge.
  4. static const Alignment topCenter = Alignment(0.0, -1.0);
  5. /// The top right corner.
  6. static const Alignment topRight = Alignment(1.0, -1.0);
  7. /// The center point along the left edge.
  8. static const Alignment centerLeft = Alignment(-1.0, 0.0);
  9. /// The center point, both horizontally and vertically.
  10. static const Alignment center = Alignment(0.0, 0.0);
  11. /// The center point along the right edge.
  12. static const Alignment centerRight = Alignment(1.0, 0.0);
  13. /// The bottom left corner.
  14. static const Alignment bottomLeft = Alignment(-1.0, 1.0);
  15. /// The center point along the bottom edge.
  16. static const Alignment bottomCenter = Alignment(0.0, 1.0);
  17. /// The bottom right corner.
  18. 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的设置枚举:

  1. /// Construct a color from the lower 8 bits of four integers.
  2. ///
  3. /// * `a` is the alpha value, with 0 being transparent and 255 being fully
  4. /// opaque.
  5. /// * `r` is [red], from 0 to 255.
  6. /// * `g` is [green], from 0 to 255.
  7. /// * `b` is [blue], from 0 to 255.
  8. ///
  9. /// Out of range values are brought into range using modulo 255.
  10. ///
  11. /// See also [fromRGBO], which takes the alpha value as a floating point
  12. /// value.
  13. const Color.fromARGB(int a, int r, int g, int b) :
  14. value = (((a & 0xff) << 24) |
  15. ((r & 0xff) << 16) |
  16. ((g & 0xff) << 8) |
  17. ((b & 0xff) << 0)) & 0xFFFFFFFF;

decoration

绘制在child后面的装饰,设置了decoration的话,就不能设置color属性,否则会报错,此时应该在decoration中进行颜色的设置。

Decoration是一个相对复杂的属性,允许我们用比较复杂的结果作为背景。我们可以设置指定颜色和圆角的边框、背景颜色等等属性。

示例代码:

  1. decoration: BoxDecoration(
  2. border: new Border.all(width: 2.0, color: Colors.red),
  3. color: Colors.grey,
  4. borderRadius: new BorderRadius.all(new Radius.circular(20.0)),
  5. image: new DecorationImage(
  6. image: new NetworkImage('http://h.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=0d023672312ac65c67506e77cec29e27/9f2f070828381f30dea167bbad014c086e06f06c.jpg'),
  7. centerSlice: new Rect.fromLTRB(270.0, 180.0, 1360.0, 730.0),
  8. ),
  9. )

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。

一个例子

  1. class TestContainer extends State {
  2. @override
  3. Widget build(BuildContext context) {
  4. return Center(
  5. child: Container(
  6. alignment: Alignment.bottomCenter,
  7. padding: EdgeInsets.only(left: 1, top: 1, right: 1, bottom: 1),
  8. color: Color.fromARGB(255, 148, 120, 31),
  9. decoration: BoxDecoration(
  10. border: Border.all(width: 2.0, color: Colors.red),
  11. color: Colors.grey,
  12. borderRadius: BorderRadius.all(new Radius.circular(20.0)),
  13. image: DecorationImage(
  14. image: NetworkImage('http://h.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=0d023672312ac65c67506e77cec29e27/9f2f070828381f30dea167bbad014c086e06f06c.jpg'),
  15. centerSlice: Rect.fromLTRB(270.0, 180.0, 1360.0, 730.0),
  16. ),
  17. ),
  18. constraints: BoxConstraints.expand(
  19. height:Theme.of(context).textTheme.display1.fontSize * 1.1 + 200.0,
  20. ),
  21. margin: EdgeInsets.only(left: 1, top: 1, right: 1, bottom: 1),
  22. transform: Matrix4.rotationZ(0.3),
  23. ),
  24. );
  25. }
  26. }

以上就是Flutter中Container的基本使用。如有问题,欢迎指正。