容器类 Widget 不同于布局类 Widget:

  1. 首先,布局类 Widget 的子 Widget 一般都是数组,而容器类 Widget 的子 Widget 一般只有一个。
  2. 布局类 Widget 是按照一定的排列方式对其子 Widget 进行排列,而容器类 Widget 用于嵌套其他 Widget,对 Widget 添加一些修饰(补白或背景色等)、变换(旋转或剪裁等)、或限制(大小等)。

在前面使用 UI Widget 的时候,想必你已经发现,大部分 UI Widget 都不能指定宽高、设置内边距和外边距,这时候就需要使用容器类 Widget 了。

Padding

Padding 通过给自己指定内边距来添加子 Widget。

Padding 的 快速上手

Padding 的必填参数是 padding,然后其 child 参数就是你想要添加 padding 的 Widget,例如:

  1. Padding(
  2. padding: EdgeInsets.all(100),
  3. child: Text('Hello Flutter'),
  4. )

Padding 在一个页面使用的完整 Demo 如下:

  1. import 'package:flutter/material.dart';
  2. main() => runApp(new PaddingWidget());
  3. class PaddingWidget extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return new MaterialApp(
  7. title: 'Test',
  8. home: new Scaffold(
  9. appBar: new AppBar(title: new Text('Flutter 容器Widget -- Padding')),
  10. body: Padding(
  11. padding: EdgeInsets.all(100),
  12. child: Text('Hello Flutter'),
  13. )));
  14. }
  15. }

运行效果如下:

Flutter 学习(二十五)容器类 Widget - 图1

Padding 的构造函数及参数说明

Padding 的构造函数为:

  1. class Padding extends SingleChildRenderObjectWidget {
  2. const Padding({
  3. Key key,
  4. @required this.padding,
  5. Widget child,
  6. }) : assert(padding != null),
  7. super(key: key, child: child);
  8. ...
  9. }
参数名字 参数类型 意义 必选 or 可选
key Key Widget 的标识 可选
padding EdgeInsetsGeometry 容器内边距 必选
child Widget 容器里显示的 Widget 可选

padding:容器内边距

padding 的类型是 EdgeInsetsGeometry,EdgeInsetsGeometry 是抽象类,我们一般使用 EdgeInsets:

EdgeInsets 的值 含义
EdgeInsets.all(double value) 上、下、左、右 边距都一样
EdgeInsets.only({this.left = 0.0,this.top = 0.0,this.right = 0.0,this.bottom = 0.0}) 可以单独指定一个的边距
EdgeInsets.symmetric({double vertical = 0.0,double horizontal = 0.0,}) vertical 的值是上、下边距, horizontal 是左右边距的值

Container

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

Container 的快速上手

Container 可以为 Widget 添加大小、背景等各种参数,其 child 参数就是你想要添加 Container 的 Widget,例如:

  1. Container(
  2. child:
  3. ...
  4. )

Container 在一个页面使用的完整 Demo 如下:

  1. import 'package:flutter/material.dart';
  2. main() => runApp(new ContainerWidget());
  3. class ContainerWidget extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return new MaterialApp(
  7. title: 'Test',
  8. home: new Scaffold(
  9. appBar:
  10. new AppBar(title: new Text('Flutter 容器Widget -- Container')),
  11. body: Container(
  12. margin: EdgeInsets.only(top: 50.0, left: 120.0), //容器外补白
  13. constraints:
  14. BoxConstraints.tightFor(width: 200.0, height: 150.0), //卡片大小
  15. decoration: BoxDecoration(
  16. //背景装饰
  17. gradient: RadialGradient(
  18. //背景径向渐变
  19. colors: [Colors.green, Colors.blue],
  20. center: Alignment.topLeft,
  21. radius: .98),
  22. boxShadow: [
  23. //卡片阴影
  24. BoxShadow(
  25. color: Colors.black54,
  26. offset: Offset(2.0, 2.0),
  27. blurRadius: 4.0)
  28. ]),
  29. transform: Matrix4.rotationZ(.2), //卡片倾斜变换
  30. alignment: Alignment.center, //卡片内文字居中
  31. child: Text(
  32. //卡片文字
  33. "Hello Flutter",
  34. style: TextStyle(color: Colors.white, fontSize: 40.0),
  35. ),
  36. )));
  37. }
  38. }

运行效果如下:

Flutter 学习(二十五)容器类 Widget - 图2

Container 的构造函数及参数说明

Container 的构造函数为:

  1. class Container extends StatelessWidget {
  2. Container({
  3. Key key,
  4. this.alignment,
  5. this.padding,
  6. Color color,
  7. Decoration decoration,
  8. this.foregroundDecoration,
  9. double width,
  10. double height,
  11. BoxConstraints constraints,
  12. this.margin,
  13. this.transform,
  14. this.child,
  15. }) :
  16. ...
  17. }
参数名字 参数类型 意义 必选 or 可选
key Key Widget 的标识 可选
alignment AlignmentGeometry 容器内 child 的对齐方式 可选
padding EdgeInsetsGeometry 容器内边距 可选
color Color 容器的背景色 可选
decoration Decoration 容器的背景装饰 可选
foregroundDecoration Decoration 容器的前景装饰 可选
width double 容器的宽度 可选
height double 容器的高度 可选
constraints BoxConstraints 容器的大小限制 可选
margin EdgeInsetsGeometry 容器外边距 可选
transform Matrix4 容器的变化 可选
child Widget 容器里显示的 Widget 可选

Align

Align 可以控制其子 Widget 的对齐方式,并可以根据子 Widget 的大小自动调整自己的大小。

Align 的快速上手

Align 可以为 Widget 添加对齐方式,其 child 参数就是你想要添加 Align 的 Widget,例如:

  1. Align(
  2. alignment: Alignment.topRight,
  3. child: Text(
  4. 'Hello Flutter',
  5. style: TextStyle(color: Colors.red, fontSize: 50),
  6. ),
  7. )

Align 在一个页面使用的完整 Demo 如下:

  1. import 'package:flutter/material.dart';
  2. main() => runApp(new AlignWidget());
  3. class AlignWidget extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return new MaterialApp(
  7. title: 'Test',
  8. home: new Scaffold(
  9. appBar: new AppBar(title: new Text('Flutter 容器Widget -- Align')),
  10. body: Align(
  11. alignment: Alignment.topRight,
  12. child: Text(
  13. 'Hello Flutter',
  14. style: TextStyle(color: Colors.red, fontSize: 50),
  15. ),
  16. )));
  17. }
  18. }

运行效果为:

Flutter 学习(二十五)容器类 Widget - 图3

Align 的构造函数及参数说明

Align 的构造函数为:

  1. class Align extends SingleChildRenderObjectWidget {
  2. const Align({
  3. Key key,
  4. this.alignment = Alignment.center,
  5. this.widthFactor,
  6. this.heightFactor,
  7. Widget child
  8. }) : assert(alignment != null),
  9. assert(widthFactor == null || widthFactor >= 0.0),
  10. assert(heightFactor == null || heightFactor >= 0.0),
  11. super(key: key, child: child);
  12. ...
  13. }
参数名字 参数类型 意义 必选 or 可选
key Key Widget 的标识 可选
alignment Alignment 容器内 child 的对齐方式 可选
widthFactor double 宽度因子。如果没有设置,则 Align 的宽度就是match_parent;如果为 非null,则将容器的宽度设置为 子Widget的宽度 乘以此宽度因子
值必须>=0
可选
heightFactor double 高度因子。如果没有设置,则 Align 的高度就是match_parent;如果为 非null,则将容器的高度设置为 子Widget的高度 乘以此高度因子
值必须>=0
可选
child Widget 容器里显示的 Widget 可选

Center

Center 可以将其子 Widget 居中显示在自身内部。Center 继承自 Align,其实就是 alignment 为 Alignment.center 的 Align。

Center 的快速上手

Center 可以让 Widget 居中,其 child 参数就是你想要居中的 Widget,例如:

  1. Center(
  2. child: Text(
  3. 'Hello Flutter',
  4. style: TextStyle(color: Colors.red, fontSize: 50),
  5. ),
  6. )

Center 在一个页面使用的完整 Demo 如下:

  1. import 'package:flutter/material.dart';
  2. main() => runApp(new CenterWidget());
  3. class CenterWidget extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return new MaterialApp(
  7. title: 'Test',
  8. home: new Scaffold(
  9. appBar: new AppBar(title: new Text('Flutter 容器Widget -- Center')),
  10. body: Center(
  11. child: Text(
  12. 'Hello Flutter',
  13. style: TextStyle(color: Colors.red, fontSize: 50),
  14. ),
  15. )));
  16. }
  17. }

运行效果为:

Flutter 学习(二十五)容器类 Widget - 图4

Center 的构造函数及参数说明

Center 的构造函数为:

  1. class Center extends Align {
  2. /// Creates a widget that centers its child.
  3. const Center({ Key key, double widthFactor, double heightFactor, Widget child })
  4. : super(key: key, widthFactor: widthFactor, heightFactor: heightFactor, child: child);
  5. }
参数名字 参数类型 意义 必选 or 可选
key Key Widget 的标识 可选
widthFactor double 宽度因子。如果没有设置,则 Align 的宽度就是match_parent;如果为 非null,则将容器的宽度设置为 子Widget的宽度 乘以此宽度因子
值必须>=0
可选
heightFactor double 高度因子。如果没有设置,则 Align 的高度就是match_parent;如果为 非null,则将容器的高度设置为 子Widget的高度 乘以此高度因子
值必须>=0
可选
child Widget 容器里显示的 Widget 可选

参考

【1】Flutter 实战
【2】Flutter 中文文档
【3】Flutter 完全手册