row 在水平方向上排列子widget的列表。
column 在垂直方向上排列子widget的列表。
center:将children放置在主轴的中心;
end:将children放置在主轴的末尾;
spaceAround:将主轴方向上的空白区域均分,使得children之间的空白区域相等,但是首尾child的空白区域为1/2;
spaceBetween:将主轴方向上的空白区域均分,使得children之间的空白区域相等,首尾child都靠近首尾,没有间隙;
spaceEvenly:将主轴方向上的空白区域均分,使得children之间的空白区域相等,包括首尾child;
start:将children放置在主轴的起点;
max:根据传入的布局约束条件,最大化主轴方向的可用空间;
min:与max相反,是最小化主轴方向的可用空间;
baseline:在交叉轴方向,使得children的baseline对齐;
center:children在交叉轴上居中展示;
end:children在交叉轴上末尾展示;
start:children在交叉轴上起点处展示;
stretch:让children填满交叉轴方向;
down:从top到bottom进行布局;
up:从bottom到top进行布局。
Widget row = new Row(
children: [
Expanded(
child: Text(
'Deliver features faster',
textAlign: TextAlign.center,
)),
Expanded(
child: Text(
'Craft beautiful UIs ',
textAlign: TextAlign.center)),
Expanded(
child: FittedBox(
fit: BoxFit.contain,
child: const FlutterLogo(),
)),
],
);
Widget column = new Column(
children: [
Text('Deliver features faster'),
Text('Craft beautiful UIs'),
Expanded(
child: FittedBox(
fit: BoxFit.contain, // otherwise the logo will be tiny
child: const FlutterLogo(),
),
),
],
);
Wrap是一个可以使子控件自动换行的控件,默认的方向是水平的
跟Android中FrameLayout很像,都是可以叠加的现实View
StackFit.loose 指的是子Widget 多大就多大,
StackFit.expand使子Widget 的大小和父组件一样大
Overflow.clip:子Widget超出Stack会被截断,
Overflow.visible超出部分还会显示的
TextDirection.rtl: 从右到左
TextDirection.ltr: 从左到右
Widget stack = new Stack(
textDirection: TextDirection.rtl,
children: [
Container(
width: 100,
height: 100,
color: Colors.red,
),
Container(
width: 90,
height: 90,
color: Colors.blue,
),
Container(
width: 80,
height: 80,
color: Colors.green,
)
],
);
IndexedStack和Stack一样,都是层布局控件, 可以在一个控件上面放置另一个控件,但唯一不同的是IndexedStack在同一时刻只能显示子控件中的一个控件,通过Index属性来设置显示的控件
拥有Stack属性外,还有index属性
一个实现流式布局算法的widget,
Flow用起来远比Wrap麻烦,但是它可以实现更加个性化的需求,我们可以通过delegate属性自己设置子控件排列规则。
Flowdelegate delegate :用于回调Flow大小,以及 计算childWidget的frame。
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.compose(Vector.Vector3(x,y,0.0), Vector.Quaternion(0.0,0.0,0.3,0.1), Vector.Vector3(1.0,1.0,1.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;
}
}
}
getSize(BoxConstraints constraints) {
//指定Flow的大小
return Size(double.infinity, double.infinity);
}
@override
bool shouldRepaint(FlowDelegate oldDelegate) {
return oldDelegate != this;
}
}
Flow flow = Flow(
delegate: TestFlowDelegate(margin: EdgeInsets.all(10.0)),
children: [
new Container(width: 80.0, height: 80.0, color: Colors.red),
new Container(width: 80.0, height: 80.0, color: Colors.yellow),
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.lightBlue),
new Container(width: 80.0, height: 80.0, color: Colors.black),
new Container(width: 80.0, height: 80.0, color: Colors.blueGrey),
new Container(width: 80.0, height: 80.0, color: Colors.brown),
new Container(width: 80.0, height: 80.0, color: Colors.black12),
],
);
为其子widget使用表格布局算法的widget
Table table = new Table(
border: TableBorder.all(
width: 2.0, color: Colors.red, style: BorderStyle.solid),
children: [
new TableRow(
children: [
new TableCell(child: new Center(child: new Text("Hello 1"),)),
new TableCell(child: new Center(child: new Text("Hello 2"),)),
new TableCell(child: new Center(child: new Text("Hello 3"),)),
new TableCell(child: new Center(child: new Text("Hello 4"),))
],
),
new TableRow(
children: [
new TableCell(child: new Center(child: new Text("Hello 1"),)),
new TableCell(child: new Center(child: new Text("Hello 2"),)),
new TableCell(child: new Center(child: new Text("Hello 3"),)),
new TableCell(child: new Center(child: new Text("Hello 4"),))
],
),
new TableRow(
children: [
new TableCell(child: new Center(child: new Text("Hello 1"),)),
new TableCell(child: new Center(child: new Text("Hello 2"),)),
new TableCell(child: new Center(child: new Text("Hello 3"),)),
new TableCell(child: new Center(child: new Text("Hello 4"),))
],
),
new TableRow(
children: [
new TableCell(child: new Center(child: new Text("Hello 1"),)),
new TableCell(child: new Center(child: new Text("Hello 2"),)),
new TableCell(child: new Center(child: new Text("Hello 3"),)),
new TableCell(child: new Center(child: new Text("Hello 4"),))
],
),
],
);
ListBody的作用是按给定的轴方向,按照顺序排列子节点。
Widget flex = new Flex(
direction: Axis.horizontal,
children: [
ListBody(
mainAxis: Axis.vertical,
reverse: false,
children: [
Container(color: Colors.red, width: 50.0, height: 50.0,),
Container(color: Colors.yellow, width: 50.0, height: 50.0,),
Container(color: Colors.green, width: 50.0, height: 50.0,),
Container(color: Colors.blue, width: 50.0, height: 50.0,),
Container(color: Colors.black, width: 50.0, height: 50.0,),
],
),
],
);
可滚动的列表控件。ListView是最常用的滚动widget,它在滚动方向上一个接一个地显示它的孩子。在纵轴上,孩子们被要求填充ListView。
List,
此构造函数适合于具有少量子元素的列表视图ListView.builder
利用IndexedWidgetBuilder
来按需构造,这个构造函数适合于具有大量(或无限)子视图的列表视图,因为构建器只对那些实际可见的子视图调用。ListView.separated
构造函数,采用两个IndexedWidgetBuilder:itemBuilder
根据需要构建子项separatorBuilder
类似地构建出现在子项之间的分隔符子项。此构造函数适用于具有固定数量的子控件的列表视图。ListView.custom
的SliverChildDelegate
构造,它提供了定制子模型的其他方面的能力。 例如,SliverChildDelegate
可以控制用于估计实际上不可见的孩子的大小的算法
//ListView.builder构建
ListView listView = new ListView.builder(
padding: EdgeInsets.all(10),
itemCount: 5,
itemExtent: 50.0,
itemBuilder: (BuildContext context,int index){
return new Text("Hello $index");
},
);
//ListView.separated构建
ListView listView = new ListView.separated(
itemCount: 5,
itemBuilder: (BuildContext context,int index){
return new Text("Hello $index");
},
separatorBuilder: (BuildContext context,int index){
return new Container(height: 10.0,color: Colors.red,);
},
);
//通过 ListView.custom构建
/**
* 继承SliverChildBuilderDelegate 可以对列表的监听
*/
class MySliverChildDelegate extends SliverChildBuilderDelegate{
/*这两种效果等价*/
//MyDelegate(IndexedWidgetBuilder builder) : super(builder);
//MyDelegate(Widget Function(BuildContext, int) builder) : super(builder);
MySliverChildDelegate(IndexedWidgetBuilder builder,{
int childCount,
}) : super(builder,childCount:childCount);
监听 在可见的列表中 显示的第一个位置和最后一个位置
@override
void didFinishLayout(int firstIndex, int lastIndex) {
// TODO: implement didFinishLayout
super.didFinishLayout(firstIndex, lastIndex);
}
//添加进来的实例与之前的实例是否相同 相同返回true 反之false
@override
bool shouldRebuild(SliverChildBuilderDelegate oldDelegate) {
// TODO: implement shouldRebuild
return super.shouldRebuild(oldDelegate);
}
}
ListView listView = new ListView.custom(
itemExtent: 40.0,
childrenDelegate: MySliverChildDelegate(
(BuildContext context, int i) {
return new Container(
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
new Text(
"张三 name",
style: new TextStyle(fontSize: 18.0, color: Colors.red),
),
new Text(
"11 age}",
style: new TextStyle(fontSize: 18.0, color: Colors.green),
),
new Text(
"信息 content}",
style: new TextStyle(fontSize: 18.0, color: Colors.blue),
),
],
));
},
childCount: 10,
),
cacheExtent: 0.0,
);
通过ListView.builder实现效果
ListView.separated构建
ListView.custom