CustomSingleChildLayout
Container(
child: CustomSingleChildLayout(
delegate: ChildLayoutDelegate(),
child: Text("hello world"),
),
)
class ChildLayoutDelegate extends SingleChildLayoutDelegate {
@override
Offset getPositionForChild(Size size, Size childSize) {
var dx = (size.width - childSize.width) / 2;
var dy = (size.height - childSize.height) / 2;
return Offset(dx, dy);
}
@override
bool shouldRelayout(SingleChildLayoutDelegate oldDelegate) {
return oldDelegate != this;
}
}
CustomMultiChildLayout
Container(
child: CustomMultiChildLayout(
delegate: MutilChildDelegate(),
children: <Widget>[
LayoutId(id: "topId", child: Text("top text")),
LayoutId(
id: "bottomId",
child: Text("bottom text"),
)
],
),
)
class MutilChildDelegate extends MultiChildLayoutDelegate {
@override
void performLayout(Size size) {
Size topSize;
Size bottomSize;
if (hasChild("topId")) {
topSize = layoutChild("topId", BoxConstraints.loose(size));
}
if (hasChild("bottomId")) {
bottomSize = layoutChild("bottomId", BoxConstraints.loose(size));
}
if (topSize != null && bottomSize != null) {
var totalHeight = topSize.height + bottomSize.height;
var tdy = (size.height - totalHeight) / 2;
var tdx = (size.width - topSize.width) / 2;
positionChild("topId", Offset(tdx, tdy));
var bdy = tdy + topSize.height;
var bdx = (size.width - bottomSize.width) / 2;
positionChild("bottomId", Offset(bdx, bdy));
return;
}
if (topSize != null) {
_positionCenter(size, topSize, "topId");
}
if (bottomSize != null) {
_positionCenter(size, bottomSize, "bottomId");
}
}
_positionCenter(Size size, Size childSize, String id) {
var dx = (size.width - childSize.width) / 2;
var dy = (size.height - childSize.height) / 2;
positionChild(id, Offset(dx, dy));
}
@override
bool shouldRelayout(MultiChildLayoutDelegate oldDelegate) {
return oldDelegate != this;
}
}
Flow
Container(
child: Flow(
delegate: FlowLayoutDelegate(),
children: <Widget>[
Text("hello world 1"),
Text("hello world 2"),
Text("hello world 3"),
Text("hello world 4")
],
),
)
class FlowLayoutDelegate extends FlowDelegate {
@override
void paintChildren(FlowPaintingContext context) {
var childCount = context.childCount;
Offset position = Offset.zero;
for (int i = 0; i < childCount; i++) {
Size size = context.getChildSize(i);
context.paintChild(i,
transform: Matrix4.translationValues(position.dx, position.dy, 0));
position += Offset(size.width, size.height);
}
}
@override
bool shouldRepaint(FlowDelegate oldDelegate) {
return oldDelegate != this;
}
}