[infra_ui][overlar] Implement overlay insertion interface

This commit is contained in:
Jaylen Bian 2021-08-01 18:03:24 +08:00
parent a6a350a831
commit 58e4a4d5f1
3 changed files with 29 additions and 26 deletions

View File

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

View File

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

View File

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