一. 单子布局组件

单子布局组件的含义是其只有一个子组件,可以通过设置一些属性设置该子组件所在的位置信息等; 比较常用的单子布局组件有:Align、Center、Padding、Container;

1.1. Align组件

1.1.1. Align介绍

iOS、Android、前端中Align通常只是一个属性而已,Flutter中Align也是一个组件;

  1. const Align({
  2. Key key,
  3. this.alignment: Alignment.center, // 对齐方式,默认居中对齐
  4. this.widthFactor, // 宽度因子,不设置的情况,会尽可能大
  5. this.heightFactor, // 高度因子,不设置的情况,会尽可能大
  6. Widget child // 要布局的子Widget
  7. })

widthFactor和heightFactor:

  • 子组件在父组件中的对齐方式必须有一个前提,就是父组件得知道自己的范围(宽度和高度);
  • widthFactor和heightFactor不设置,默认Align会尽可能的大(尽可能占据自己所在的父组件);
  • 可以进行设置,widthFactor设置为3,相对于Align的宽度是子组件跨度的3倍;

    1.1.2. Align

    1. class MyHomeBody extends StatelessWidget {
    2. @override
    3. Widget build(BuildContext context) {
    4. return Align(
    5. child: Icon(Icons.pets, size: 36, color: Colors.red),
    6. alignment: Alignment.bottomRight,
    7. widthFactor: 3,
    8. heightFactor: 3,
    9. );
    10. }
    11. }

    1.2. Center组件

    1.2.1. Center

    Center组件继承自Align,只是将alignment设置为Alignment.center。
    1. class Center extends Align {
    2. const Center({
    3. Key key,
    4. double widthFactor,
    5. double heightFactor,
    6. Widget child
    7. }) : super(key: key, widthFactor: widthFactor, heightFactor: heightFactor, child: child);
    8. }

    1.2.2. Center

    1. class MyHomeBody extends StatelessWidget {
    2. @override
    3. Widget build(BuildContext context) {
    4. return Center(
    5. child: Icon(Icons.pets, size: 36, color: Colors.red),
    6. widthFactor: 3,
    7. heightFactor: 3,
    8. );
    9. }
    10. }

    1.3. Padding组件

    1.3.1. Padding

    Padding也是一个Widget,但Flutter没有Margin-Widget,因为外边距也可以通过Padding来完成; Padding通常用于设置子Widget到父Widget的边距(父组件的内边距或子Widget的外边距);

  1. const Padding({
  2. Key key,
  3. @required this.padding, // EdgeInsetsGeometry类型(抽象类),使用EdgeInsets
  4. Widget child,
  5. })

1.3.2. Padding

  1. class MyHomeBody extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return Padding(
  5. padding: EdgeInsets.all(20),
  6. child: Text(
  7. "莫听穿林打叶声,何妨吟啸且徐行。竹杖芒鞋轻胜马,谁怕?一蓑烟雨任平生。",
  8. style: TextStyle(
  9. color: Colors.redAccent,
  10. fontSize: 18
  11. ),
  12. ),
  13. );
  14. }
  15. }

1.4. Container组件

Container组件类似于其他Android中的View,iOS中的UIView。 如果需要一个视图,有一个背景颜色、图像、有固定的尺寸、需要一个边框、圆角等效果,那么就可以使用Container组件。

14.1. Container介绍

Container经常作为容器组件。

  1. Container({
  2. this.alignment,
  3. this.padding, //容器内补白,属于decoration的装饰范围
  4. Color color, // 背景色
  5. Decoration decoration, // 背景装饰
  6. Decoration foregroundDecoration, //前景装饰
  7. double width,//容器的宽度
  8. double height, //容器的高度
  9. BoxConstraints constraints, //容器大小的限制条件
  10. this.margin,//容器外补白,不属于decoration的装饰范围
  11. this.transform, //变换
  12. this.child,
  13. })
  • 容器的大小可以通过width、height属性来指定,也可以通过constraints来指定,如果同时存在时,width、height优先。实际上Container内部会根据width、height来生成一个constraints;
  • color和decoration是互斥的,实际上,当指定color时,Container内会自动创建一个decoration;
  • decoration;

    1.4.2. Container

    1. class MyHomeBody extends StatelessWidget {
    2. @override
    3. Widget build(BuildContext context) {
    4. return Center(
    5. child: Container(
    6. color: Color.fromRGBO(3, 3, 255, .5),
    7. width: 100,
    8. height: 100,
    9. child: Icon(Icons.pets, size: 32, color: Colors.white),
    10. ),
    11. );
    12. }
    13. }

    1.4.3. BoxDecoration

    Container有一个非常重要的属性 decoration:

  • 对应的类型是Decoration类型,是一个抽象类;

  • 开发中,经常使用它的实现类BoxDecoration来进行实例化;
    1. const BoxDecoration({
    2. this.color, // 颜色,会和Container中的color属性冲突
    3. this.image, // 背景图片
    4. this.border, // 边框,对应类型是Border类型,里面每一个边框使用BorderSide
    5. this.borderRadius, // 圆角效果
    6. this.boxShadow, // 阴影效果
    7. this.gradient, // 渐变效果
    8. this.backgroundBlendMode, // 背景混合
    9. this.shape = BoxShape.rectangle, // 形变
    10. })
    部分效果演示:
    1. class MyHomeBody extends StatelessWidget {
    2. @override
    3. Widget build(BuildContext context) {
    4. return Center(
    5. child: Container(
    6. // color: Color.fromRGBO(3, 3, 255, .5),
    7. width: 150,
    8. height: 150,
    9. child: Icon(Icons.pets, size: 32, color: Colors.white),
    10. decoration: BoxDecoration(
    11. color: Colors.amber, // 背景颜色
    12. border: Border.all(
    13. color: Colors.redAccent,
    14. width: 3,
    15. style: BorderStyle.solid
    16. ), // 这里也可以使用Border.all统一设置
    17. // top: BorderSide(
    18. // color: Colors.redAccent,
    19. // width: 3,
    20. // style: BorderStyle.solid
    21. // ),
    22. borderRadius: BorderRadius.circular(20), // 这里也可以使用.only分别设置
    23. boxShadow: [
    24. BoxShadow(
    25. offset: Offset(5, 5),
    26. color: Colors.purple,
    27. blurRadius: 5
    28. )
    29. ],
    30. // shape: BoxShape.circle, // 会和borderRadius冲突
    31. gradient: LinearGradient(
    32. colors: [
    33. Colors.green,
    34. Colors.red
    35. ]
    36. )
    37. ),
    38. ),
    39. );
    40. }
    41. }

    1.4.4. 实现圆角图像

    Container+BoxDecoration来实现圆角图像。
    1. class HomeContent extends StatelessWidget {
    2. @override
    3. Widget build(BuildContext context) {
    4. return Center(
    5. child: Container(
    6. width: 200,
    7. height: 200,
    8. decoration: BoxDecoration(
    9. borderRadius: BorderRadius.circular(20),
    10. image: DecorationImage(
    11. image: NetworkImage("https://tva1.sinaimg.cn/large/006y8mN6gy1g7aa03bmfpj3069069mx8.jpg"),
    12. )
    13. ),
    14. ),
    15. );
    16. }
    17. }

    二. 多子布局组件

    开发中,经常需要将多个Widget放在一起进行布局,比如水平方向、垂直方向排列,甚至有时候需要他们进行层叠,比如图片上面放一段文字等; 使用多子布局组件(Multi-child layout widgets); 常用的多子布局组件是Row、Column、Stack;

2.1. Flex组件

Row组件和Column组件继承自Flex组件

  • Flex组件和Row、Column属性主要的区别就是多一个direction;
  • 当direction的值为Axis.horizontal的时候,则是Row;
  • 当direction的值为Axis.vertical的时候,则是Column;

    2.1. Row组件

    2.1.1. Row

    Row组件用于将所有的子Widget排成一行

  1. Row({
  2. Key key,
  3. MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start, // 主轴对齐方式
  4. MainAxisSize mainAxisSize = MainAxisSize.max, // 水平方向尽可能大
  5. CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center, // 交叉处对齐方式
  6. TextDirection textDirection, // 水平方向子widget的布局顺序(默认为系统当前Locale环境的文本方向(如中文、英语都是从左往右,而阿拉伯语是从右往左))
  7. VerticalDirection verticalDirection = VerticalDirection.down, // 表示Row纵轴(垂直)的对齐方向
  8. TextBaseline textBaseline, // 如果上面是baseline对齐方式,那么选择什么模式(有两种可选)
  9. List<Widget> children = const <Widget>[],
  10. })

部分属性详细解析:
mainAxisSize:

  • 表示Row在主轴(水平)方向占用的空间,默认是MainAxisSize.max,表示尽可能多的占用水平方向的空间,此时无论子widgets实际占用多少水平空间,Row的宽度始终等于水平方向的最大宽度
  • 而MainAxisSize.min表示尽可能少的占用水平空间,当子widgets没有占满水平剩余空间,则Row的实际宽度等于所有子widgets占用的的水平空间;

mainAxisAlignment:表示子Widgets在Row所占用的水平空间内对齐方式

  • 如果mainAxisSize值为MainAxisSize.min,则此属性无意义,因为子widgets的宽度等于Row的宽度
  • 只有当mainAxisSize的值为MainAxisSize.max时,此属性才有意义
  • MainAxisAlignment.start表示沿textDirection的初始方向对齐,
  • 如textDirection取值为TextDirection.ltr时,则MainAxisAlignment.start表示左对齐,textDirection取值为TextDirection.rtl时表示从右对齐。
  • 而MainAxisAlignment.end和MainAxisAlignment.start正好相反;
  • MainAxisAlignment.center表示居中对齐。

crossAxisAlignment:表示子Widgets在纵轴方向的对齐方式

  • Row的高度等于子Widgets中最高的子元素高度
  • 它的取值和MainAxisAlignment一样(包含start、end、 center三个值)
  • 不同的是crossAxisAlignment的参考系是verticalDirection,即verticalDirection值为VerticalDirection.down时crossAxisAlignment.start指顶部对齐,verticalDirection值为VerticalDirection.up时,crossAxisAlignment.start指底部对齐;而crossAxisAlignment.end和crossAxisAlignment.start正好相反;

    2.1.2. Row

    1. class MyHomeBody extends StatelessWidget {
    2. @override
    3. Widget build(BuildContext context) {
    4. return Row(
    5. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
    6. crossAxisAlignment: CrossAxisAlignment.end,
    7. mainAxisSize: MainAxisSize.max,
    8. children: <Widget>[
    9. Container(color: Colors.red, width: 60, height: 60),
    10. Container(color: Colors.blue, width: 80, height: 80),
    11. Container(color: Colors.green, width: 70, height: 70),
    12. Container(color: Colors.orange, width: 100, height: 100),
    13. ],
    14. );
    15. }
    16. }

    2.1.3. mainAxisSize

    默认Row会尽可能占据多的宽度,让子Widget在其中进行排布,因为mainAxisSize属性默认值是MainAxisSize.max。

    2.1.4. TextBaseline

    关于TextBaseline的取值解析
    Flutter-布局组件 - 图1

    2.1.5. Expanded

    在Row中如果希望Container Widget不要设置固定的宽度,而是占据剩余的部分?
    可以使用Expanded来包裹 Container Widget,并且将它的宽度不设置值;

  • flex属性,弹性系数,Row会根据两个Expanded的弹性系数来决定它们占据剩下空间的比例

    1. class MyHomeBody extends StatelessWidget {
    2. @override
    3. Widget build(BuildContext context) {
    4. return Row(
    5. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
    6. crossAxisAlignment: CrossAxisAlignment.end,
    7. mainAxisSize: MainAxisSize.min,
    8. children: <Widget>[
    9. Expanded(
    10. flex: 1,
    11. child: Container(color: Colors.red, height: 60),
    12. ),
    13. Container(color: Colors.blue, width: 80, height: 80),
    14. Container(color: Colors.green, width: 70, height: 70),
    15. Expanded(
    16. flex: 1,
    17. child: Container(color: Colors.orange, height: 100),
    18. )
    19. ],
    20. );
    21. }
    22. }

    2.2. Column组件

    Column组件用于将所有的子Widget排成一列;

2.2.1. Column

  1. Column({
  2. Key key,
  3. MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
  4. MainAxisSize mainAxisSize = MainAxisSize.max,
  5. CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
  6. TextDirection textDirection,
  7. VerticalDirection verticalDirection = VerticalDirection.down,
  8. TextBaseline textBaseline,
  9. List<Widget> children = const <Widget>[],
  10. })

2.2.2. Column

  1. class MyHomeBody extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return Column(
  5. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  6. crossAxisAlignment: CrossAxisAlignment.end,
  7. mainAxisSize: MainAxisSize.min,
  8. children: <Widget>[
  9. Expanded(
  10. flex: 1,
  11. child: Container(color: Colors.red, width: 60),
  12. ),
  13. Container(color: Colors.blue, width: 80, height: 80),
  14. Container(color: Colors.green, width: 70, height: 70),
  15. Expanded(
  16. flex: 1,
  17. child: Container(color: Colors.orange, width: 100),
  18. )
  19. ],
  20. );
  21. }
  22. }

2.3. Stack组件

开发中,多个组件很有可能需要重叠显示,比如在一张图片上显示文字或者一个按钮等; Flutter中需要使用层叠布局Stack;

2.3.1. Stack

  1. Stack({
  2. Key key,
  3. this.alignment = AlignmentDirectional.topStart,
  4. this.textDirection,
  5. this.fit = StackFit.loose,
  6. this.overflow = Overflow.clip,
  7. List<Widget> children = const <Widget>[],
  8. })

参数解析:

  • alignment:此参数决定如何去对齐没有定位(没有使用Positioned)或部分定位的子widget。所谓部分定位,在这里特指没有在某一个轴上定位:**left、right为横轴,top、bottom为纵轴,只要包含某个轴上的一个定位属性就算在该轴上有定位。
  • textDirection:和Row、Wrap的textDirection功能一样,都用于决定alignment对齐的参考系即:textDirection的值为TextDirection.ltr,则alignment的start代表左,end代表右;textDirection的值为TextDirection.rtl,则alignment的start代表右,end代表左。
  • fit:此参数用于决定没有定位的子widget如何去适应Stack的大小。StackFit.loose表示使用子widget的大小,StackFit.expand表示扩伸到Stack的大小。
  • overflow:此属性决定如何显示超出Stack显示空间的子widget,值为Overflow.clip时,超出部分会被剪裁(隐藏),值为Overflow.visible 时则不会。

    2.3.2. Stack

    Stack会经常和Positioned一起使用,Positioned决定组件在Stack中的位置,实现类似绝对定位效果。
    1. class MyHomeBody extends StatelessWidget {
    2. @override
    3. Widget build(BuildContext context) {
    4. return Stack(
    5. children: <Widget>[
    6. Container(
    7. color: Colors.purple,
    8. width: 300,
    9. height: 300,
    10. ),
    11. Positioned(
    12. left: 20,
    13. top: 20,
    14. child: Icon(Icons.favorite, size: 50, color: Colors.white)
    15. ),
    16. Positioned(
    17. bottom: 20,
    18. right: 20,
    19. child: Text("hello world", style: TextStyle(fontSize: 20, color: Colors.white)),
    20. )
    21. ],
    22. );
    23. }
    24. }