Created
January 23, 2020 10:37
-
-
Save Norbert515/86c0625c2a6e8a603cb44aebbafe8e02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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