Flutter——Row&Column

1. 简述

RowColumn分别是横向布局和纵向布局,对标Android中的LinearLayout

    <!--  Row  -->
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content">

    </LinearLayout>
    
    <!--  Column  -->
    <LinearLayout
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:layout_height="match_parent">

    </LinearLayout>

Row和Column默认会在主轴方向取最大,即上面XML代码中展示的,Row相对应LinearLayout的layout_widthmatch_parent,Column相对应LinearLayout的layout_heightmatch_parent

2. 使用

Row和Column的使用相似,以Column为例看一下构造方法

Column({
    Key key,
    MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
    MainAxisSize mainAxisSize = MainAxisSize.max,
    CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
    TextDirection textDirection,
    VerticalDirection verticalDirection = VerticalDirection.down,
    TextBaseline textBaseline,
    List<Widget> children = const <Widget>[],
  })

a) mainAxisAlignment

主轴的对齐方式,默认为MainAxisAlignment.start,即在布局的开始进行children的排列,下面列下所有的主轴对中mode:

  • start
  • end
  • center
  • spaceBetween
  • spaceAround
  • spaceEvenly

start、end、center这里不需要多说,Android转过来的同学了若指掌。

spaceBetween

Place the free space evenly between the children.
将空闲空间均匀地放在孩子之间

将空间均匀的分布在children之间,并且布局两头的child会抵住布局的两端,不留空间。

Column(
    mainAxisAlignment: MainAxisAlignment.spaceBetween,
    children: <Widget>[
        Icon(Icons.ac_unit),
        Icon(Icons.ac_unit),
        Icon(Icons.ac_unit),
    ],
)

以上面的Column代码为例,展现的效果图为

<img src="https://upload-images.jianshu.io/upload_images/4025850-8973ee9114cc26aa.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="screenshot-1569483899301.jpg" style="zoom: 25%;" />

spaceAround

Place the free space evenly between the children as well as half of that space before and after the first and last child.

在第一个和最后一个孩子之前和之后的子对象之间以及剩余空间的一半之间均匀地分配可用空间

通俗来讲,就是将空间按children的数量进行等分,并且child在分配的空间居中显示,代码与上面相同,仅改变MainAxisAlignment.spaceBetweenMainAxisAlignment.spaceAround

<img src="https://upload-images.jianshu.io/upload_images/4025850-9b26808e12a868cd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="screenshot-1569484302267.jpg" style="zoom:25%;" />

可以明显看出,第一个child的上面空间,是其下面空间的一半>。

spaceEvenly

Place the free space evenly between the children as well as before and after the first and last child.

在第一个和最后一个孩子之前和之后的孩子之间均匀地分配可用空间。

均匀的分配空间,这里可以看做将空间减去children所占空间后,按children的count+1等分,并且将child放在两个空间的中间:

<img src="https://upload-images.jianshu.io/upload_images/4025850-03891bd6d0b765f7.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="screenshot-1569484756442.jpg" style="zoom:25%;" />

child的上下空间一样,并且与两端的child相距边缘的空间也一致,可以数到,被child分割的大小相同的空间,共有children.count+1份。

b) mainAxisSize

主轴的尺寸,即我们前面说的,Column和Row默认下主轴方向取最大。

这里可选择的属性只有两种:

  • max
  • min

相对应的,即为Android的match_parentwrap_content

c) crossAxisAlignment

副轴对齐方式,Column的主轴是纵轴,相对应的副轴就是横轴。

依然列出所有mode:

  • start
  • end
  • center
  • stretch
  • baseline

因为baseline的特殊性,这次用Row作为示例。依然对前三个予以蔑视。

Row(
    children: <Widget>[
        Container(
            color: Colors.greenAccent,
            child: Icon(Icons.accessibility),
        ),
        Container(
            color: Colors.amberAccent,
            child: Icon(Icons.accessibility),
        ),
        Container(
            color: Colors.blue,
            child: Icon(Icons.accessibility),
        ),
    ],
)

<img src="https://upload-images.jianshu.io/upload_images/4025850-8afe990cf13bcdfc.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="screenshot-1569488114614.jpg" style="zoom:25%;" />

stretch

Require the children to fill the cross axis.

This causes the constraints passed to the children to be tight in the cross axis.

要求孩子填充交叉轴。

这导致传递给子代的约束在交叉轴上很紧。

就是让child在副轴的方向取最大

<img src="https://upload-images.jianshu.io/upload_images/4025850-b02fb1d8eaf249da.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="screenshot-1569488186976.jpg" style="zoom:25%;" />

baseline & TextBaseline

Place the children along the cross axis such that their baselines match.
If the main axis is vertical, then this value is treated like [start]
(since baselines are always horizontal).
沿十字轴放置孩子,使其基线匹配。
如果主轴是垂直的,则将此值视为[start](因为基线始终是水平的)。

这个属性对应的是文字基线对齐,要配合TextBaseline属性来使用。

TextBaseline包含两个属性:alphabetic(字母)、ideographic(表意)

<img src="https://upload-images.jianshu.io/upload_images/4025850-77747d4517063e48.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="screenshot-1569548619278.jpg" style="zoom:25%;" />

不过,修改两个属性,是真的没有发现文字对齐有什么不一样,抱歉。

d) textDirection

A direction in which text flows.

文字流动的方向

可以理解为横向的排布方向,这里有两个属性可选:

  • ltr(left to right)
  • rtl(right to left)

正常情况下,系统默认为ltr,两者对比如下:

ltr

<img src="https://upload-images.jianshu.io/upload_images/4025850-f65b631bcf350028.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png" style="zoom: 67%;" />

rtl

<img src="https://upload-images.jianshu.io/upload_images/4025850-cd62f7c88102194f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png" style="zoom:67%;" />

e) verticalDirection

A direction in which boxes flow vertically.

垂直流动的方向

就是纵向的排布方向,也是有两个属性可选:

  • up
  • down

系统默认为down,即向下流动(从上往下排列),因为和textDirection属性太相似,这里不单独截图做对比。

f) children

最后的关键,孩子们。

children的属性是List<Widget>,使用方式前面代码里面已经展示,需要注意的一点就是children总大小不要超过了Column&Row的长宽,不然会报错,这点和Android还是有点差别的。

3. 总结

从Android转到Flutter的同学可以放心的食用Column&Row,脑中就想着在使用LinearLayout就可以了。

  • Column为纵向布局,Row为横向布局
  • 主轴方向和副轴方向上的对齐方式都有多重选择
  • 主轴方向默认取最大,可以设置为取最小
  • 涉及到文字排布时,可以使用baseline&textBaseline来使排版更好看
  • 主轴方向的排列顺序也可以设置
    • Column对应verticalDirection
    • Row对应textDirection
  • 布置孩子时,需要注意总长宽不可以大于Row&Column的长宽,不然会报异常
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,651评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,468评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,931评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,218评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,234评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,198评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,084评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,926评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,341评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,563评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,731评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,430评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,036评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,676评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,829评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,743评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,629评论 2 354

推荐阅读更多精彩内容