Skip to content

Instantly share code, notes, and snippets.

@Norbert515
Created January 23, 2020 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Norbert515/86c0625c2a6e8a603cb44aebbafe8e02 to your computer and use it in GitHub Desktop.
Save Norbert515/86c0625c2a6e8a603cb44aebbafe8e02 to your computer and use it in GitHub Desktop.
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
class MyCenter extends SingleChildRenderObjectWidget {
MyCenter({Key key, Widget child}): super(key: key, child: child);
@override
RenderObject createRenderObject(BuildContext context) {
return _RenderMyCenter();
}
}
class _RenderMyCenter extends RenderBox with RenderObjectWithChildMixin<RenderBox> {
@override
void performLayout() {
if(child != null) {
size = constraints.biggest;
child.layout(constraints.loosen(), parentUsesSize: true);
double dx = (constraints.maxWidth - child.size.width) / 2;
double dy = (constraints.maxHeight - child.size.height) / 2;
BoxParentData childParentData = child.parentData;
childParentData.offset = Offset(dx, dy);
} else {
size = Size(0,0);
}
}
@override
void paint(PaintingContext context, Offset offset) {
if(child != null) {
var parentData = child.parentData as BoxParentData;
context.paintChild(child, offset + parentData.offset);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment