@@ -24,93 +24,7 @@ import 'package:flutter/material.dart';
2424
2525import 'showcase_widget.dart' ;
2626
27- typedef OverlayBuilderCallback = Widget Function (
28- BuildContext context,
29- Rect anchorBounds,
30- Offset anchor,
31- );
32-
33- /// Displays an overlay Widget anchored directly above the center of this
34- /// [AnchoredOverlay] .
35- ///
36- /// The overlay Widget is created by invoking the provided [overlayBuilder] .
37- ///
38- /// The [anchor] position is provided to the [overlayBuilder] , but the builder
39- /// does not have to respect it. In other words, the [overlayBuilder] can
40- /// interpret the meaning of "anchor" however it wants - the overlay will not
41- /// be forced to be centered about the [anchor] .
42- ///
43- /// The overlay built by this [AnchoredOverlay] can be conditionally shown
44- /// and hidden by settings the [showOverlay] property to true or false.
45- ///
46- /// The [overlayBuilder] is invoked every time this Widget is rebuilt.
47- ///
48- class AnchoredOverlay extends StatelessWidget {
49- final bool showOverlay;
50- final OverlayBuilderCallback ? overlayBuilder;
51- final Widget ? child;
52- final RenderObject ? rootRenderObject;
53-
54- const AnchoredOverlay ({
55- super .key,
56- this .showOverlay = false ,
57- this .overlayBuilder,
58- this .child,
59- this .rootRenderObject,
60- });
61-
62- @override
63- Widget build (BuildContext context) {
64- return LayoutBuilder (
65- builder: (context, constraints) {
66- return OverlayBuilder (
67- showOverlay: showOverlay,
68- overlayBuilder: (overlayContext) {
69- // To calculate the "anchor" point we grab the render box of
70- // our parent Container and then we find the center of that box.
71- final box = context.findRenderObject () as RenderBox ? ;
72-
73- /// Handle null RenderBox safely.
74- final topLeft = box? .size.topLeft (
75- box.localToGlobal (
76- Offset .zero,
77- ancestor: rootRenderObject,
78- ),
79- ) ??
80- Offset .zero;
81- final bottomRight = box? .size.bottomRight (
82- box.localToGlobal (
83- Offset .zero,
84- ancestor: rootRenderObject,
85- ),
86- ) ??
87- Offset .zero;
88-
89- /// Provide a default anchorBounds if box is null.
90- final anchorBounds = (topLeft.dx.isNaN ||
91- topLeft.dy.isNaN ||
92- bottomRight.dx.isNaN ||
93- bottomRight.dy.isNaN)
94- ? const Rect .fromLTRB (0.0 , 0.0 , 0.0 , 0.0 )
95- : Rect .fromLTRB (
96- topLeft.dx,
97- topLeft.dy,
98- bottomRight.dx,
99- bottomRight.dy,
100- );
101-
102- /// Calculate the anchor center or default to Offset.zero.
103- final anchorCenter = box? .size.center (topLeft) ?? Offset .zero;
104-
105- /// Pass the anchor details to the overlay builder.
106- return overlayBuilder !(overlayContext, anchorBounds, anchorCenter);
107- },
108- child: child,
109- );
110- },
111- );
112- }
113- }
27+ typedef OverlayUpdateCallback = void Function (VoidCallback updateOverlay);
11428
11529/// Displays an overlay Widget as constructed by the given [overlayBuilder] .
11630///
@@ -128,9 +42,11 @@ class OverlayBuilder extends StatefulWidget {
12842 final bool showOverlay;
12943 final WidgetBuilder ? overlayBuilder;
13044 final Widget ? child;
45+ final OverlayUpdateCallback updateOverlay;
13146
13247 const OverlayBuilder ({
13348 super .key,
49+ required this .updateOverlay,
13450 this .showOverlay = false ,
13551 this .overlayBuilder,
13652 this .child,
@@ -150,12 +66,20 @@ class _OverlayBuilderState extends State<OverlayBuilder> {
15066 if (widget.showOverlay) {
15167 WidgetsBinding .instance.addPostFrameCallback ((_) => showOverlay ());
15268 }
69+ widget.updateOverlay.call (updateOverlay);
70+ }
71+
72+ void updateOverlay () {
73+ buildOverlay ();
74+ WidgetsBinding .instance.addPostFrameCallback ((_) => syncWidgetAndOverlay ());
15375 }
15476
15577 @override
15678 void didUpdateWidget (OverlayBuilder oldWidget) {
15779 super .didUpdateWidget (oldWidget);
158- WidgetsBinding .instance.addPostFrameCallback ((_) => syncWidgetAndOverlay ());
80+ if (oldWidget.showOverlay != widget.showOverlay && widget.showOverlay) {
81+ WidgetsBinding .instance.addPostFrameCallback ((_) => showOverlay ());
82+ }
15983 }
16084
16185 @override
@@ -221,8 +145,6 @@ class _OverlayBuilderState extends State<OverlayBuilder> {
221145
222146 @override
223147 Widget build (BuildContext context) {
224- buildOverlay ();
225-
226148 return widget.child! ;
227149 }
228150}
0 commit comments