Flutter自定义布局排列:单个子View-爱代码爱编程
简单例子:该例子实现了子View居中对齐效果
class CenterLayout extends SingleChildRenderObjectWidget {
CenterLayout({
Key key,
Widget child
}): super(key: key , child: child);
@override
RenderObject createRenderObject(BuildContext context) => RenderLayout();
}
class RenderLayout extends RenderShiftedBox {
RenderLayout({RenderBox child}) : super(child);
@override
void performLayout() {
child.layout(BoxConstraints(
minHeight: 0.0,
//maxHeight: null,
minWidth: 0.0,
//maxWidth: null,
), parentUsesSize: true);
final BoxParentData childParentData = child.parentData;
//控制子View坐标,(0,0)是屏幕左上角
childParentData.offset = Offset((size.width - child.size.width) / 2, (size.height - child.size.height) / 2);
size = Size(this.constraints.maxWidth, constraints.maxHeight);
}
}
使用:
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xffffffff),
body: Container(
child: CenterLayout(
child: Text(
"我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我"
),
),
),
);
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接: https://blog.csdn.net/qq_16009381/article/details/111100218