Преглед на файлове

[infra_ui][overlar] Implement overlay insertion interface

Jaylen Bian преди 3 години
родител
ревизия
58e4a4d5f1

+ 16 - 14
app_flowy/packages/flowy_infra_ui/example/lib/overlay/overlay_screen.dart

@@ -55,20 +55,22 @@ class OverlayScreen extends StatelessWidget {
               child: const Text('Show Overlay'),
             ),
             const SizedBox(height: 12.0),
-            ElevatedButton(
-              onPressed: () {
-                FlowyOverlay.of(context).insertWithAnchor(
-                  widget: const FlutterLogo(
-                    size: 200,
-                    textColor: Colors.orange,
-                  ),
-                  identifier: 'overlay_flutter_logo',
-                  delegate: null,
-                  anchorContext: context,
-                );
-              },
-              child: const Text('Show Anchored Overlay'),
-            ),
+            Builder(builder: (buttonContext) {
+              return ElevatedButton(
+                onPressed: () {
+                  FlowyOverlay.of(context).insertWithAnchor(
+                    widget: const FlutterLogo(
+                      size: 200,
+                      textColor: Colors.orange,
+                    ),
+                    identifier: 'overlay_flutter_logo',
+                    delegate: null,
+                    anchorContext: buttonContext,
+                  );
+                },
+                child: const Text('Show Anchored Overlay'),
+              );
+            }),
             const SizedBox(height: 12.0),
             ElevatedButton(
               onPressed: () {

+ 11 - 4
app_flowy/packages/flowy_infra_ui/lib/src/flowy_overlay/flowy_overlay.dart

@@ -201,17 +201,24 @@ class FlowyOverlayState extends State<FlowyOverlay> {
         anchorPosition != null || anchorContext != null,
         'Must provide `anchorPosition` or `anchorContext` to locating overlay.',
       );
-      var targetAnchorPosition = anchorPosition;
+      Offset targetAnchorPosition = anchorPosition ?? Offset.zero;
+      Size targetAnchorSize = anchorSize ?? Size.zero;
       if (anchorContext != null) {
         RenderObject renderObject = anchorContext.findRenderObject()!;
         assert(
           renderObject is RenderBox,
           'Unexpect non-RenderBox render object caught.',
         );
-        final localOffset = (renderObject as RenderBox).localToGlobal(Offset.zero);
-        targetAnchorPosition ??= localOffset;
+        final renderBox = renderObject as RenderBox;
+        targetAnchorPosition = renderBox.localToGlobal(Offset.zero);
+        targetAnchorSize = renderBox.size;
       }
-      final anchorRect = targetAnchorPosition! & (anchorSize ?? Size.zero);
+      final anchorRect = Rect.fromLTWH(
+        targetAnchorPosition.dx,
+        targetAnchorPosition.dy,
+        targetAnchorSize.width,
+        targetAnchorSize.height,
+      );
       overlay = CustomSingleChildLayout(
         delegate: OverlayLayoutDelegate(
           anchorRect: anchorRect,

+ 2 - 8
app_flowy/packages/flowy_infra_ui/lib/src/flowy_overlay/overlay_layout_delegate.dart

@@ -19,20 +19,14 @@ class OverlayLayoutDelegate extends SingleChildLayoutDelegate {
     return anchorRect != oldDelegate.anchorRect || anchorDirection != oldDelegate.anchorDirection;
   }
 
-  @override
-  Size getSize(BoxConstraints constraints) {
-    return super.getSize(constraints);
-  }
-
   @override
   BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
-    // TODO: junlin - calculate child constaints
-    return super.getConstraintsForChild(constraints);
+    return constraints.loosen();
   }
 
   @override
   Offset getPositionForChild(Size size, Size childSize) {
     // TODO: junlin - calculate child position
-    return Offset(size.width / 2, size.height / 2);
+    return Offset(anchorRect.width / 2, anchorRect.height / 2);
   }
 }