Flutter入门笔记系列文章部分内容来源于《Flutter 实战》,如有侵权请联系删除!
考虑这样一种情况:使用Row对组件进行水平布局,如果有组件超出屏幕之外,会发生什么?我们先来验证一下这个假设。
body: Container(
color: Colors.blue,
child: Row(
children: <Widget>[
RaisedButton(
child: Text("AAAAAAAA"),
onPressed: () {},
),RaisedButton(
child: Text("BBBBBBBBB"),
onPressed: () {},
),RaisedButton(
child: Text("CCCCCCCC"),
onPressed: () {},
),
RaisedButton(
child: Text("DDDDDDDDDD"),
onPressed: () {},
),
],
))
运行效果

我们可以看到溢出部分会报错误,解决这个问题的方案就是当组件快要超出屏幕的时候,自动换到下一行。Flutter为我们提供了专门的布局处理这种异常情况,我们把超出屏幕显示范围会自动折行的布局称为Wrap流式布局。
Wrap
首先看看Wrap有哪些属性
Wrap({
Key key,
this.direction = Axis.horizontal,
this.alignment = WrapAlignment.start,
this.spacing = 0.0,
this.runAlignment = WrapAlignment.start,
this.runSpacing = 0.0,
this.crossAxisAlignment = WrapCrossAlignment.start,
this.textDirection,
this.verticalDirection = VerticalDirection.down,
List<Widget> children = const <Widget>[],
})
是不是感觉有很多熟悉的属性?没错,很多属性在Flutter布局组件<线性布局Row&Column>一文中都有介绍过,而且作用也都是相同的,本文不再重复介绍。那我们就重点分析Wrap特有的属性:
- spacing:主轴方向子widget的间距
- runSpacing:纵轴方向的间距
- runAlignment:纵轴方向的对齐方式
我们把上面的代码稍作修改,看看能否达到我们所期待的效果。
body: Container(
color: Colors.blue,
child: Wrap(
direction: Axis.horizontal,
spacing: 16, //主轴方向间距
runSpacing: 8, //纵轴方向间距
alignment: WrapAlignment.center, //主轴方式对齐方式
runAlignment: WrapAlignment.center, //纵轴方式对齐方式
children: <Widget>[
RaisedButton(
child: Text("AAAAAAAAAA"),
onPressed: () {},
),RaisedButton(
child: Text("BBBBBBBB"),
onPressed: () {},
),RaisedButton(
child: Text("CCCCCCCCCCCCCCC"),
onPressed: () {},
),
RaisedButton(
child: Text("DDDDDDDDDD"),
onPressed: () {},
),
RaisedButton(
child: Text("AAAAAAAA"),
onPressed: () {},
),RaisedButton(
child: Text("BBBBBBBBB"),
onPressed: () {},
),RaisedButton(
child: Text("CCCCCCCC"),
onPressed: () {},
),
RaisedButton(
child: Text("DDDDDDDDDD"),
onPressed: () {},
),
],
))
运行效果

Flow
我们一般很少会使用Flow
,因为其过于复杂,需要自己实现子widget的位置转换,在很多场景下首先要考虑的是Wrap
是否满足需求。Flow
主要用于一些需要自定义布局策略或性能要求较高(如动画中)的场景。Flow
有如下优点:
- 性能好;
Flow
是一个对子组件尺寸以及位置调整非常高效的控件,Flow
用转换矩阵在对子组件进行位置调整的时候进行了优化:在Flow
定位过后,如果子组件的尺寸或者位置发生了变化,在FlowDelegate
中的paintChildren()
方法中调用context.paintChild
进行重绘,而context.paintChild
在重绘时使用了转换矩阵,并没有实际调整组件位置。 - 灵活;由于我们需要自己实现
FlowDelegate
的paintChildren()
方法,所以我们需要自己计算每一个组件的位置,因此,可以自定义布局策略。
缺点:
- 使用复杂。
- 不能自适应子组件大小,必须通过指定父容器大小或实现
TestFlowDelegate
的getSize
返回固定大小。
示例:
我们对六个色块进行自定义流式布局:
Flow(
delegate: TestFlowDelegate(margin: EdgeInsets.all(10.0)),
children: <Widget>[
new Container(width: 80.0, height:80.0, color: Colors.red,),
new Container(width: 80.0, height:80.0, color: Colors.green,),
new Container(width: 80.0, height:80.0, color: Colors.blue,),
new Container(width: 80.0, height:80.0, color: Colors.yellow,),
new Container(width: 80.0, height:80.0, color: Colors.brown,),
new Container(width: 80.0, height:80.0, color: Colors.purple,),
],
)
实现TestFlowDelegate:
class TestFlowDelegate extends FlowDelegate {
EdgeInsets margin = EdgeInsets.zero;
TestFlowDelegate({this.margin});
@override
void paintChildren(FlowPaintingContext context) {
var x = margin.left;
var y = margin.top;
//计算每一个子widget的位置
for (int i = 0; i < context.childCount; i++) {
var w = context.getChildSize(i).width + x + margin.right;
if (w < context.size.width) {
context.paintChild(i,
transform: new Matrix4.translationValues(
x, y, 0.0));
x = w + margin.left;
} else {
x = margin.left;
y += context.getChildSize(i).height + margin.top + margin.bottom;
//绘制子widget(有优化)
context.paintChild(i,
transform: new Matrix4.translationValues(
x, y, 0.0));
x += context.getChildSize(i).width + margin.left + margin.right;
}
}
}
@override
getSize(BoxConstraints constraints){
//指定Flow的大小
return Size(double.infinity,200.0);
}
@override
bool shouldRepaint(FlowDelegate oldDelegate) {
return oldDelegate != this;
}
}
运行效果见图4-8:

可以看到我们主要的任务就是实现paintChildren
,它的主要任务是确定每个子widget位置。由于Flow不能自适应子widget的大小,我们通过在getSize
返回一个固定大小来指定Flow的大小。
网友评论