我们在 Flutter 中布局 和 HTML 和 Css 书写还是有一点区别的。

官方组件链接🔗

常用组件片面讲解链接🔗

简介:

Container 在 Flutter 中使用的也是比较多的,Container就像是web中的Div, 大家可想而至div的强大之处。所以Container 也是在页面布局中常用的容器之一

【Container】饰【DIV】

web 页面写法。

  1. <div class="greybox">
  2. Lorem ipsum
  3. </div>
  4. .greybox {
  5. background-color: #e0e0e0; /* grey 300 */
  6. width: 320px;
  7. height: 240px;
  8. font: 900 24px Georgia;
  9. }

Flutter 页面写法。

  1. Container(
  2. child: Text(
  3. "Lorem ipsum",
  4. style: TextStyle(
  5. fontSize: 24,
  6. fontWeight: FontWeight.w900,
  7. fontFamily: "Georgia",
  8. ),
  9. ),
  10. width: 320,
  11. height: 240,
  12. color: Colors.grey[300],
  13. )

WechatIMG38.png

Container中的属性都有。

  1. Container({
  2. Key key,
  3. this.alignment, // 对其方式:alignment: Alignment.center, 居中对齐。
  4. this.padding, // 内边距:padding: EdgeInsets.only(top : 40.0, left : 30.0),属于修饰的范畴。
  5. this.color, // 背景色的设置:Colors.white || Color(0xffFFFFFF); 在修饰Decoration中设置了则外侧就不能设置。
  6. this.decoration, // 修饰容器:景后装饰,比如说背景图片 中设置color 和 外面的color 二者不能共存
  7. this.foregroundDecoration, // 景前修饰:比如说,蒙层,在child前面添加装饰
  8. double width, // 宽
  9. double height, // 高
  10. BoxConstraints constraints, // 容器大小的限制条件。
  11. this.margin, // 外边距
  12. this.transform, // 变换
  13. this.child, // 子元素
  14. this.clipBehavior = Clip.none,
  15. })

【foregroundDecoration】前景装饰,就是遮住子极z-index=1000;

  1. foregroundDecoration: new BoxDecoration(
  2. border: new Border.all(width: 2.0, color: Colors.red), //边框
  3. color: Colors.black87, //背景色
  4. // 四个 radius 一起设置。
  5. // borderRadius: new BorderRadius.all(new Radius.circular(20.0)),
  6. // 单个 圆角设置。
  7. borderRadius: BorderRadius.only(
  8. topLeft: new Radius.circular(40.0),
  9. bottomRight: new Radius.circular(40.0),
  10. ),
  11. image: new DecorationImage(
  12. // image: new NetworkImage(""), // 加载网络图片链接
  13. image:AssetImage("lib/image/img1.jpg"), // 背景图片
  14. ),
  15. )

WechatIMG42.png

【decoration】容器修饰

  1. constraints: new BoxConstraints.expand(
  2. height:Theme.of(context).textTheme.headline1.fontSize * 1.1 + 300.0,
  3. ),
  4. decoration: BoxDecoration(
  5. color : Colors.black87,
  6. border: Border.all(width : 2.0, color : Colors.red),
  7. borderRadius: BorderRadius.only(
  8. topLeft: new Radius.circular(40.0),
  9. bottomRight: new Radius.circular(40.0),
  10. ),
  11. image: DecorationImage(
  12. image: AssetImage("lib/image/img1.jpg"),
  13. centerSlice: new Rect.fromLTRB(270.0, 180.0, 1360.0, 730.0), // 让图片撑满。
  14. )
  15. ) ,
  16. // 要是想单个边框来设置的话,则下面这种方式添加,但是这样设置的话,【则添加圆角(borderRadius)则会错误】。
  17. // decoration 里面添加。
  18. border: Border(
  19. bottom : BorderSide(
  20. width: 3.0,
  21. color: Colors.red
  22. )
  23. ),
  24. // 设置背景渐变色 decoration里面添加。 下面为了更好的掩饰渐变,吧图片去掉了。
  25. gradient: LinearGradient(
  26. begin : Alignment.topLeft,
  27. end : Alignment.bottomCenter,
  28. colors : [
  29. Colors.pinkAccent,
  30. Colors.white,
  31. ],
  32. )
  33. // 添加 shape 让Container 变成圆, 但是这样的话,不能添加border | borderRadius 影响绘画圆
  34. shape: BoxShape.circle,

WechatIMG43.pngWechatIMG46.pngWechatIMG50.png

【padding】【margin】

  1. padding: EdgeInsets.only(top : 40.0, left : 30.0), // 设置内边距
  2. margin: EdgeInsets.all(10.0), // 设置外边距

WechatIMG45.png

【alignment】

文字居中的时候我把 padding 和 margin 去掉了。

  1. alignment: Alignment.center,

WechatIMG47.png

【transform】

这就简单的展示一下,后续再详细讲解。

  1. // 变换
  2. transform: new Matrix4.rotationX(0.8)

WechatIMG55.pngWechatIMG52.png
【constraints】约束

上面的代码我是加了约束的,不然我设置的高没那么大。

约束代码:

  1. constraints: new BoxConstraints.expand(
  2. height:Theme.of(context).textTheme.headline1.fontSize * 1.1 + 300.0,
  3. ),
  4. // 但是一般我们约束约束最小高度,最大高度等。
  5. const BoxConstraints({
  6. this.minWidth = 0.0,
  7. this.maxWidth = double.infinity,
  8. this.minHeight = 0.0,
  9. this.maxHeight = double.infinity,
  10. }) : assert (minWidth != null),
  11. assert (maxWidth != null),

添加了就象上面那样,增加了高度等,没添加的则如下图。

是否需要添加,看需求而定。
WechatIMG54.jpeg