feat: add exclusive

This commit is contained in:
Vincent Chan 2022-08-29 15:29:39 +08:00
parent 072d94ebe6
commit defef552ed
2 changed files with 33 additions and 13 deletions

View File

@ -7,8 +7,17 @@ class PopoverMenu extends StatefulWidget {
} }
class _PopoverMenuState extends State<PopoverMenu> { class _PopoverMenuState extends State<PopoverMenu> {
final AppFlowyPopoverController popover = AppFlowyPopoverController(); final AppFlowyPopoverExclusive exclusive = AppFlowyPopoverExclusive();
final AppFlowyPopoverController hoverPopover = AppFlowyPopoverController(); late AppFlowyPopoverController firstPopover;
late AppFlowyPopoverController secondPopover;
@override
void initState() {
firstPopover = AppFlowyPopoverController(exclusive: exclusive);
secondPopover = AppFlowyPopoverController(exclusive: exclusive);
super.initState();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Container( return Container(
@ -18,7 +27,7 @@ class _PopoverMenuState extends State<PopoverMenu> {
child: ListView(children: [ child: ListView(children: [
const Text("App"), const Text("App"),
AppFlowyPopover( AppFlowyPopover(
controller: popover, controller: firstPopover,
offset: const Offset(10, 0), offset: const Offset(10, 0),
targetAnchor: Alignment.topRight, targetAnchor: Alignment.topRight,
followerAnchor: Alignment.topLeft, followerAnchor: Alignment.topLeft,
@ -27,20 +36,18 @@ class _PopoverMenuState extends State<PopoverMenu> {
}, },
child: TextButton( child: TextButton(
onPressed: () { onPressed: () {
popover.show(); firstPopover.show();
}, },
onHover: (value) { onHover: (value) {
if (value) { if (value) {
popover.show(); firstPopover.show();
} else {
popover.close();
} }
}, },
child: const Text("First"), child: const Text("First"),
), ),
), ),
AppFlowyPopover( AppFlowyPopover(
controller: hoverPopover, controller: secondPopover,
offset: const Offset(10, 0), offset: const Offset(10, 0),
targetAnchor: Alignment.topRight, targetAnchor: Alignment.topRight,
followerAnchor: Alignment.topLeft, followerAnchor: Alignment.topLeft,
@ -49,13 +56,11 @@ class _PopoverMenuState extends State<PopoverMenu> {
}, },
child: TextButton( child: TextButton(
onPressed: () { onPressed: () {
hoverPopover.show(); secondPopover.show();
}, },
onHover: (value) { onHover: (value) {
if (value) { if (value) {
hoverPopover.show(); secondPopover.show();
} else {
hoverPopover.close();
} }
}, },
child: const Text("Second"), child: const Text("Second"),

View File

@ -1,14 +1,29 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
class AppFlowyPopoverExclusive {
AppFlowyPopoverController? controller;
}
class AppFlowyPopoverController { class AppFlowyPopoverController {
AppFlowyPopoverState? state; AppFlowyPopoverState? state;
AppFlowyPopoverExclusive? exclusive;
AppFlowyPopoverController({this.exclusive});
close() { close() {
state?.close(); state?.close();
if (exclusive != null && exclusive!.controller == this) {
exclusive!.controller = null;
}
} }
show() { show() {
if (exclusive != null) {
debugPrint("show popover");
exclusive!.controller?.close();
exclusive!.controller = this;
}
state?.showOverlay(); state?.showOverlay();
} }
} }
@ -110,7 +125,7 @@ class AppFlowyPopoverState extends State<AppFlowyPopover> {
_overlayEntry?.remove(); _overlayEntry?.remove();
_overlayEntry = null; _overlayEntry = null;
if (hasMask) { if (hasMask) {
debugPrint("len: ${_globalPopovers.length}"); debugPrint("popover len: ${_globalPopovers.length}");
} }
super.dispose(); super.dispose();
} }