标签:text contex rri 没有 nsf inf aci tle bool
把超出屏幕显示范围会自动折行的布局称为流式布局。Flutter中通过Wrap和Flow来支持流式布局,将Row换成Wrap后溢出部分则会自动折行。
Wrap({
Key key,
this.direction = Axis.horizontal,
this.alignment = WrapAlignment.start,
// 主轴方向子widget的间距
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>[],
})
class WrapTest extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('流式布局-Wrap'),
),
body: Container(
child: Wrap(
// 主轴(水平)方向间距
spacing: 8.0,
// 纵轴(垂直)方向间距
runSpacing: 4.0,
// 沿主轴方向居中
alignment: WrapAlignment.center,
children: <Widget>[
Chip(
avatar: CircleAvatar(backgroundColor: Colors.blue, child: Text('A'),),
label: Text('HelloWorld'),
),
Chip(
avatar: CircleAvatar(backgroundColor: Colors.blue, child: Text('B'),),
label: Text('WorldHello'),
),
Chip(
avatar: CircleAvatar(backgroundColor: Colors.blue, child: Text('T'),),
label: Text('FlowWorld'),
),
Chip(
avatar: CircleAvatar(backgroundColor: Colors.blue, child: Text('J'),),
label: Text('WarpHello'),
),
Chip(
avatar: CircleAvatar(backgroundColor: Colors.blue, child: Text('F'),),
label: Text('Yes i do'),
),
],
),
),
);
}
}
一般很少会使用Flow,因为其过于复杂,需要自己实现子widget的位置转换,在很多场景下首先要考虑的是Wrap是否满足需求。Flow主要用于一些需要自定义布局策略或性能要求较高(如动画中)的场景。
Flow有如下优点:
缺点:
Flow({
Key key,
@required this.delegate,
List<Widget> children = const <Widget>[],
})
class FlowTest extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('流式布局-Flow'),
),
body: Container(
child: Flow(
delegate: FlowDelegateTest(margin: EdgeInsets.all(10.0)),
children: <Widget>[
Container(width: 80.0, height: 80.0, color: Colors.red,),
Container(width: 80.0, height: 80.0, color: Colors.green,),
Container(width: 80.0, height: 80.0, color: Colors.blue,),
Container(width: 80.0, height: 80.0, color: Colors.yellow,),
Container(width: 80.0, height: 80.0, color: Colors.brown,),
Container(width: 80.0, height: 80.0, color: Colors.purple,),
],
),
),
);
}
}
class FlowDelegateTest extends FlowDelegate {
EdgeInsets margin = EdgeInsets.zero;
FlowDelegateTest({this.margin});
// Flow主要要重载这个函数,它的主要任务是确定每个子widget位置。
// 由于Flow不能自适应子widget的大小,通过在getSize返回一个固定大小来指定Flow的大小。
@override
void paintChildren(FlowPaintingContext context) {
var x = margin.left;
var y = margin.right;
// 计算每一个子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: 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: Matrix4.translationValues(x, y, 0.0)
);
x += context.getChildSize(i).width + margin.left + margin.right;
}
}
}
@override
Size getSize(BoxConstraints constraints) {
// 指定Flow的大小
return Size(double.infinity, 200.0);
}
@override
bool shouldRepaint(FlowDelegate oldDelegate) {
return oldDelegate != this;
}
}
标签:text contex rri 没有 nsf inf aci tle bool
原文地址:https://www.cnblogs.com/parzulpan/p/12071664.html