Writing custom Widgets in Flutter (Part 3.a) — SimpleOverlay (with helpers)

Intro

Code

class SimpleOverlay extends MultiChildRenderObjectWidget {
SimpleOverlay({
required Widget child,
required Widget overlay,
}) : super(children: [child, overlay]);

@override
RenderObject createRenderObject(BuildContext context) {
return RenderSimpleOverlay();
}
}
class _SimpleOverlayChild extends ContainerBoxParentData<RenderBox>
with ContainerParentDataMixin<RenderBox> {}
mixin ContainerParentDataMixin<ChildType extends RenderObject> on ParentData {
/// The previous sibling in the parent's child list.
ChildType? previousSibling;
/// The next sibling in the parent's child list.
ChildType? nextSibling;
}
class RenderSimpleOverlay extends RenderBox
with
ContainerRenderObjectMixin<RenderBox, _SimpleOverlayChild>,
RenderBoxContainerDefaultsMixin<RenderBox, _SimpleOverlayChild> {
// ...
}
class RenderSimpleOverlay {
// ...

@override
void setupParentData(covariant RenderObject child) {
child.parentData = _SimpleOverlayChild();
}
}
class RenderSimpleOverlay {
// ...

@override
void performLayout() {
var childConstraints = constraints;

final child = firstChild;
if (child != null) {
child.layout(constraints, parentUsesSize: true);
childConstraints = BoxConstraints.tight(child.size);
}

final overlay = (child == null) ? null : childAfter(child);
if (overlay != null) {
overlay.layout(childConstraints, parentUsesSize: true);
}

size = child?.size ?? overlay?.size ?? constraints.smallest;
}
}
class RenderSimpleOverlay {
// ...

@override
void paint(PaintingContext context, Offset offset) {
defaultPaint(context, offset);
}

@override
bool hitTestChildren(BoxHitTestResult result, {required Offset position}) {
return defaultHitTestChildren(result, position: position);
}
}

Other articles:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store