From defef552ed3a88278dfbfe8630b505d0e7b51f76 Mon Sep 17 00:00:00 2001 From: Vincent Chan Date: Mon, 29 Aug 2022 15:29:39 +0800 Subject: [PATCH] feat: add exclusive --- .../example/lib/example_button.dart | 29 +++++++++++-------- .../lib/appflowy_popover.dart | 17 ++++++++++- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/frontend/app_flowy/packages/appflowy_popover/example/lib/example_button.dart b/frontend/app_flowy/packages/appflowy_popover/example/lib/example_button.dart index 8f1a6997d5..df8b83f349 100644 --- a/frontend/app_flowy/packages/appflowy_popover/example/lib/example_button.dart +++ b/frontend/app_flowy/packages/appflowy_popover/example/lib/example_button.dart @@ -7,8 +7,17 @@ class PopoverMenu extends StatefulWidget { } class _PopoverMenuState extends State { - final AppFlowyPopoverController popover = AppFlowyPopoverController(); - final AppFlowyPopoverController hoverPopover = AppFlowyPopoverController(); + final AppFlowyPopoverExclusive exclusive = AppFlowyPopoverExclusive(); + late AppFlowyPopoverController firstPopover; + late AppFlowyPopoverController secondPopover; + + @override + void initState() { + firstPopover = AppFlowyPopoverController(exclusive: exclusive); + secondPopover = AppFlowyPopoverController(exclusive: exclusive); + super.initState(); + } + @override Widget build(BuildContext context) { return Container( @@ -18,7 +27,7 @@ class _PopoverMenuState extends State { child: ListView(children: [ const Text("App"), AppFlowyPopover( - controller: popover, + controller: firstPopover, offset: const Offset(10, 0), targetAnchor: Alignment.topRight, followerAnchor: Alignment.topLeft, @@ -27,20 +36,18 @@ class _PopoverMenuState extends State { }, child: TextButton( onPressed: () { - popover.show(); + firstPopover.show(); }, onHover: (value) { if (value) { - popover.show(); - } else { - popover.close(); + firstPopover.show(); } }, child: const Text("First"), ), ), AppFlowyPopover( - controller: hoverPopover, + controller: secondPopover, offset: const Offset(10, 0), targetAnchor: Alignment.topRight, followerAnchor: Alignment.topLeft, @@ -49,13 +56,11 @@ class _PopoverMenuState extends State { }, child: TextButton( onPressed: () { - hoverPopover.show(); + secondPopover.show(); }, onHover: (value) { if (value) { - hoverPopover.show(); - } else { - hoverPopover.close(); + secondPopover.show(); } }, child: const Text("Second"), diff --git a/frontend/app_flowy/packages/appflowy_popover/lib/appflowy_popover.dart b/frontend/app_flowy/packages/appflowy_popover/lib/appflowy_popover.dart index a28c03f5d2..02672e2a04 100644 --- a/frontend/app_flowy/packages/appflowy_popover/lib/appflowy_popover.dart +++ b/frontend/app_flowy/packages/appflowy_popover/lib/appflowy_popover.dart @@ -1,14 +1,29 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +class AppFlowyPopoverExclusive { + AppFlowyPopoverController? controller; +} + class AppFlowyPopoverController { AppFlowyPopoverState? state; + AppFlowyPopoverExclusive? exclusive; + + AppFlowyPopoverController({this.exclusive}); close() { state?.close(); + if (exclusive != null && exclusive!.controller == this) { + exclusive!.controller = null; + } } show() { + if (exclusive != null) { + debugPrint("show popover"); + exclusive!.controller?.close(); + exclusive!.controller = this; + } state?.showOverlay(); } } @@ -110,7 +125,7 @@ class AppFlowyPopoverState extends State { _overlayEntry?.remove(); _overlayEntry = null; if (hasMask) { - debugPrint("len: ${_globalPopovers.length}"); + debugPrint("popover len: ${_globalPopovers.length}"); } super.dispose(); }