From 8f0ac0a788d5baf46388ed3a2b0f5e2aba5d21e4 Mon Sep 17 00:00:00 2001 From: Sahil-Simform <122270155+Sahil-Simform@users.noreply.github.com> Date: Tue, 3 Dec 2024 10:25:43 +0530 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9BFixed=20null=20check=20issue?= =?UTF-8?q?=20in=20'RenderBox'=20at'`layout=5Foverlay.dart`(#464)=20(#487)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/src/layout_overlays.dart | 42 +++++++++++++++++++++--------------- lib/src/showcase_widget.dart | 2 +- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/lib/src/layout_overlays.dart b/lib/src/layout_overlays.dart index d8516b70..130912af 100644 --- a/lib/src/layout_overlays.dart +++ b/lib/src/layout_overlays.dart @@ -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. + final anchorBounds = (topLeft.dx.isNaN || topLeft.dy.isNaN || bottomRight.dx.isNaN || bottomRight.dy.isNaN) @@ -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, diff --git a/lib/src/showcase_widget.dart b/lib/src/showcase_widget.dart index ad9f91ab..cef0730a 100644 --- a/lib/src/showcase_widget.dart +++ b/lib/src/showcase_widget.dart @@ -158,7 +158,7 @@ class ShowCaseWidgetState extends State { int? activeWidgetId; RenderBox? rootRenderObject; Size? rootWidgetSize; - final Key anchoredOverlayKey = UniqueKey(); + final anchoredOverlayKey = UniqueKey(); late final TooltipActionConfig? globalTooltipActionConfig;