chore: add spacing between a popover and the edge of the window (#1625)

This commit is contained in:
Richard Shiue 2023-01-01 22:26:52 +08:00 committed by GitHub
parent 436291c01a
commit eed6c753dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 3 deletions

View File

@ -7,11 +7,13 @@ class PopoverLayoutDelegate extends SingleChildLayoutDelegate {
PopoverLink link; PopoverLink link;
PopoverDirection direction; PopoverDirection direction;
final Offset offset; final Offset offset;
final EdgeInsets windowPadding;
PopoverLayoutDelegate({ PopoverLayoutDelegate({
required this.link, required this.link,
required this.direction, required this.direction,
required this.offset, required this.offset,
required this.windowPadding,
}); });
@override @override
@ -35,9 +37,21 @@ class PopoverLayoutDelegate extends SingleChildLayoutDelegate {
return false; return false;
} }
@override
Size getSize(BoxConstraints constraints) {
return Size(
constraints.maxWidth - windowPadding.left - windowPadding.right,
constraints.maxHeight - windowPadding.top - windowPadding.bottom,
);
}
@override @override
BoxConstraints getConstraintsForChild(BoxConstraints constraints) { BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
return constraints.loosen(); return BoxConstraints(
maxWidth: constraints.maxWidth - windowPadding.left - windowPadding.right,
maxHeight:
constraints.maxHeight - windowPadding.top - windowPadding.bottom,
);
// assert(link.leaderSize != null); // assert(link.leaderSize != null);
// // if (link.leaderSize == null) { // // if (link.leaderSize == null) {
// // return constraints.loosen(); // // return constraints.loosen();
@ -274,8 +288,14 @@ class PopoverLayoutDelegate extends SingleChildLayoutDelegate {
throw UnimplementedError(); throw UnimplementedError();
} }
return Offset( return Offset(
math.max(0.0, math.min(size.width - childSize.width, position.dx)), math.max(
math.max(0.0, math.min(size.height - childSize.height, position.dy)), windowPadding.left,
math.min(
windowPadding.left + size.width - childSize.width, position.dx)),
math.max(
windowPadding.top,
math.min(
windowPadding.top + size.height - childSize.height, position.dy)),
); );
} }
} }

View File

@ -49,13 +49,19 @@ enum PopoverDirection {
class Popover extends StatefulWidget { class Popover extends StatefulWidget {
final PopoverController? controller; final PopoverController? controller;
/// The offset from the [child] where the popover will be drawn
final Offset? offset; final Offset? offset;
/// Amount of padding between the edges of the window and the popover
final EdgeInsets? windowPadding;
final Decoration? maskDecoration; final Decoration? maskDecoration;
/// The function used to build the popover. /// The function used to build the popover.
final Widget? Function(BuildContext context) popupBuilder; final Widget? Function(BuildContext context) popupBuilder;
/// Specify how the popover can be triggered when interacting with the child
/// by supplying a bitwise-OR combination of one or more [PopoverTriggerFlags]
final int triggerActions; final int triggerActions;
/// If multiple popovers are exclusive, /// If multiple popovers are exclusive,
@ -84,6 +90,7 @@ class Popover extends StatefulWidget {
this.triggerActions = 0, this.triggerActions = 0,
this.direction = PopoverDirection.rightWithTopAligned, this.direction = PopoverDirection.rightWithTopAligned,
this.mutex, this.mutex,
this.windowPadding,
this.onClose, this.onClose,
this.asBarrier = false, this.asBarrier = false,
}) : super(key: key); }) : super(key: key);
@ -126,6 +133,7 @@ class PopoverState extends State<Popover> {
direction: widget.direction, direction: widget.direction,
popoverLink: popoverLink, popoverLink: popoverLink,
offset: widget.offset ?? Offset.zero, offset: widget.offset ?? Offset.zero,
windowPadding: widget.windowPadding ?? EdgeInsets.zero,
popupBuilder: widget.popupBuilder, popupBuilder: widget.popupBuilder,
onClose: () => close(), onClose: () => close(),
onCloseAll: () => _removeRootOverlay(), onCloseAll: () => _removeRootOverlay(),
@ -194,6 +202,7 @@ class PopoverContainer extends StatefulWidget {
final PopoverDirection direction; final PopoverDirection direction;
final PopoverLink popoverLink; final PopoverLink popoverLink;
final Offset offset; final Offset offset;
final EdgeInsets windowPadding;
final void Function() onClose; final void Function() onClose;
final void Function() onCloseAll; final void Function() onCloseAll;
@ -203,6 +212,7 @@ class PopoverContainer extends StatefulWidget {
required this.direction, required this.direction,
required this.popoverLink, required this.popoverLink,
required this.offset, required this.offset,
required this.windowPadding,
required this.onClose, required this.onClose,
required this.onCloseAll, required this.onCloseAll,
}) : super(key: key); }) : super(key: key);
@ -228,6 +238,7 @@ class PopoverContainerState extends State<PopoverContainer> {
direction: widget.direction, direction: widget.direction,
link: widget.popoverLink, link: widget.popoverLink,
offset: widget.offset, offset: widget.offset,
windowPadding: widget.windowPadding,
), ),
child: widget.popupBuilder(context), child: widget.popupBuilder(context),
); );

View File

@ -15,6 +15,7 @@ class AppFlowyPopover extends StatelessWidget {
final Offset? offset; final Offset? offset;
final bool asBarrier; final bool asBarrier;
final EdgeInsets margin; final EdgeInsets margin;
final EdgeInsets windowPadding;
const AppFlowyPopover({ const AppFlowyPopover({
Key? key, Key? key,
@ -29,6 +30,7 @@ class AppFlowyPopover extends StatelessWidget {
this.controller, this.controller,
this.asBarrier = false, this.asBarrier = false,
this.margin = const EdgeInsets.all(6), this.margin = const EdgeInsets.all(6),
this.windowPadding = const EdgeInsets.all(8.0),
}) : super(key: key); }) : super(key: key);
@override @override
@ -40,6 +42,7 @@ class AppFlowyPopover extends StatelessWidget {
mutex: mutex, mutex: mutex,
asBarrier: asBarrier, asBarrier: asBarrier,
triggerActions: triggerActions, triggerActions: triggerActions,
windowPadding: windowPadding,
popupBuilder: (context) { popupBuilder: (context) {
final child = popupBuilder(context); final child = popupBuilder(context);
return _PopoverContainer( return _PopoverContainer(