Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 🐛 Handle null RenderBox in AnchoredOverlay and provide fallback values(#464) #487

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions lib/src/layout_overlays.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,26 @@ class AnchoredOverlay extends StatelessWidget {
overlayBuilder: (overlayContext) {
// To calculate the "anchor" point we grab the render box of
// our parent Container and then we find the center of that box.
final box = context.findRenderObject() as RenderBox;

final topLeft = box.size.topLeft(
box.localToGlobal(
const Offset(0.0, 0.0),
ancestor: rootRenderObject,
),
);
final bottomRight = box.size.bottomRight(
box.localToGlobal(
const Offset(0.0, 0.0),
ancestor: rootRenderObject,
),
);
Rect anchorBounds;
anchorBounds = (topLeft.dx.isNaN ||
final box = context.findRenderObject() as RenderBox?;

/// Handle null RenderBox safely.
final topLeft = box?.size.topLeft(
box.localToGlobal(
Offset.zero,
ancestor: rootRenderObject,
),
) ??
Offset.zero;
final bottomRight = box?.size.bottomRight(
box.localToGlobal(
Offset.zero,
ancestor: rootRenderObject,
),
) ??
Offset.zero;

/// Provide a default anchorBounds if box is null.
Rect anchorBounds = (topLeft.dx.isNaN ||
topLeft.dy.isNaN ||
bottomRight.dx.isNaN ||
bottomRight.dy.isNaN)
Expand All @@ -91,7 +95,11 @@ class AnchoredOverlay extends StatelessWidget {
bottomRight.dx,
bottomRight.dy,
);
final anchorCenter = box.size.center(topLeft);

/// Calculate the anchor center or default to Offset.zero.
final anchorCenter = box?.size.center(topLeft) ?? Offset.zero;

/// Pass the anchor details to the overlay builder.
return overlayBuilder!(overlayContext, anchorBounds, anchorCenter);
},
child: child,
Expand Down
Loading