新建项目-2.png

Container 是一个拥有绘制、定位、调整大小的 Widget.

Container


  1. Container({
  2. Key key,
  3. this.alignment, //容器内 child 的对齐方式 (9个位置)
  4. this.padding, //容器内边距
  5. Color color,
  6. Decoration decoration, //容器的背景装饰
  7. this.foregroundDecoration, //容器的前景装饰
  8. double width,
  9. double height,
  10. BoxConstraints constraints,//容器的大小限制
  11. this.margin, //容器外边距
  12. this.transform, //容器的变化
  13. this.child, //容器里显示的 Widget
  14. })

1. 绘制过程

Container的绘制的过程如下:

  • 首先会绘制transform效果;
  • 接着绘制decoration;
  • 然后绘制child;
  • 最后绘制foregroundDecoration。

    2. alignment 对齐方式. 9个方位对齐方式.

3. padding、margin 内容边距

padding: 内边距
margin: 外边距

4. decoration 容器的背景装饰

  1. const BoxDecoration({
  2. this.color, //颜色
  3. this.image, //图片
  4. this.border, //边框
  5. this.borderRadius, //边框圆角
  6. this.boxShadow, //阴影
  7. this.gradient, //渐变,如果指定了渐变色,color属性就无效
  8. this.backgroundBlendMode, //混合模式应用于框的[颜色]或[渐变]背景,如果没有颜色或者渐变色,这个属性就没有效果
  9. this.shape = BoxShape.rectangle, // 这个有两个值,一个是方形,一个是圆形(circle),
  10. BoxShape.rectangleRoundedRectangleBorder是一样的,CircleBorderBoxShape.circle是一样的效果
  11. 但是Containershape只能用BoxShape.rectangleBoxShape.circle是这两种,
  12. RoundedRectangleBorderCircleBorder目前是用在Material上的,
  13. 因为Containershape是继承自 BoxBorder的,而Materialshape是继承自ShapeBorder
  14. 虽然BoxBorder也是继承ShapeBorder
  15. })
  1. body: Container(
  2. alignment: Alignment.center,
  3. color: Colors.white,
  4. child: Column(
  5. children: <Widget>[
  6. Container(
  7. //对齐方式
  8. alignment: Alignment.center,
  9. //容器内间距
  10. margin: EdgeInsets.only(top: 20 , left: 20),
  11. //卡片大小
  12. constraints: BoxConstraints.tightFor(width: 200, height: 150),
  13. //背景装饰
  14. decoration: BoxDecoration(
  15. gradient: RadialGradient(
  16. //这里设置渐变设外面就不能设置背景色,不然会报错误
  17. colors: [Colors.green, Colors.blue],
  18. center: Alignment.topLeft,
  19. radius: .98
  20. ),
  21. //加阴影
  22. boxShadow: [
  23. BoxShadow(
  24. color: Colors.black54,
  25. offset: Offset(2.0, 2.0),
  26. blurRadius: 4.0
  27. )
  28. ]
  29. ),
  30. transform: Matrix4.rotationZ(.2), //卡片倾斜变换
  31. child: Text(
  32. "Hello Flutter",
  33. style: TextStyle(
  34. fontSize: 30,
  35. color: Colors.red
  36. ),
  37. ),
  38. ),
  39. ],
  40. ),
  41. ),

image.png

5. borderRadius 边框半径

  1. new BorderRadius.all(Radius.circular(4)) // 四个圆角都是半径为4
  2. new BorderRadius.circular(4), // 四个圆角都是半径为4,和上面的效果是一样的
  3. new BorderRadius.horizontal( left: Radius.circular(10)), //左边的两个角的半径为10
  4. new BorderRadius.horizontal(left: Radius.circular(10), right: Radius.circular(4)),//左边的两个角半径为10,右侧两个角半径为4
  5. new BorderRadius.vertical( top: Radius.circular(6)), //上边两个角半径为6
  6. new BorderRadius.only(
  7. topLeft: Radius.circular(10),
  8. topRight: Radius.circular(4),
  9. bottomLeft: Radius.circular(6),
  10. bottomRight: Radius.circular(20)
  11. ), //坐上角半径为10,右上角4,左下角为6,右下角为20