From ca89fd93f3a2437867b92680d7be5201f67c49c9 Mon Sep 17 00:00:00 2001 From: appflowy Date: Tue, 20 Sep 2022 16:57:51 +0800 Subject: [PATCH] fix: notify state changed after set state --- .../widgets/header/field_editor.dart | 4 +-- .../appflowy_popover/lib/src/mutex.dart | 35 +++++++++++-------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/frontend/app_flowy/lib/plugins/grid/presentation/widgets/header/field_editor.dart b/frontend/app_flowy/lib/plugins/grid/presentation/widgets/header/field_editor.dart index d1a749b18b..1b3e579029 100644 --- a/frontend/app_flowy/lib/plugins/grid/presentation/widgets/header/field_editor.dart +++ b/frontend/app_flowy/lib/plugins/grid/presentation/widgets/header/field_editor.dart @@ -201,9 +201,9 @@ class _FieldNameTextFieldState extends State<_FieldNameTextField> { void listenOnPopoverChanged(BuildContext context) { if (_popoverCallback != null) { - widget.popoverMutex.removePopoverStateListener(_popoverCallback!); + widget.popoverMutex.removePopoverListener(_popoverCallback!); } - _popoverCallback = widget.popoverMutex.listenOnPopoverStateChanged(() { + _popoverCallback = widget.popoverMutex.listenOnPopoverChanged(() { if (focusNode.hasFocus) { final node = FocusScope.of(context); node.unfocus(); diff --git a/frontend/app_flowy/packages/appflowy_popover/lib/src/mutex.dart b/frontend/app_flowy/packages/appflowy_popover/lib/src/mutex.dart index 2c6db3100a..8ab88e98e1 100644 --- a/frontend/app_flowy/packages/appflowy_popover/lib/src/mutex.dart +++ b/frontend/app_flowy/packages/appflowy_popover/lib/src/mutex.dart @@ -5,14 +5,14 @@ import 'popover.dart'; /// If multiple popovers are exclusive, /// pass the same mutex to them. class PopoverMutex { - final ValueNotifier _stateNotifier = ValueNotifier(null); + final _PopoverStateNotifier _stateNotifier = _PopoverStateNotifier(); PopoverMutex(); - void removePopoverStateListener(VoidCallback listener) { + void removePopoverListener(VoidCallback listener) { _stateNotifier.removeListener(listener); } - VoidCallback listenOnPopoverStateChanged(VoidCallback callback) { + VoidCallback listenOnPopoverChanged(VoidCallback callback) { listenerCallback() { callback(); } @@ -21,24 +21,31 @@ class PopoverMutex { return listenerCallback; } - void close() { - _stateNotifier.value?.close(); - } + void close() => _stateNotifier.state?.close(); - PopoverState? get state => _stateNotifier.value; + PopoverState? get state => _stateNotifier.state; - set state(PopoverState? newState) { - if (_stateNotifier.value != null && _stateNotifier.value != newState) { - _stateNotifier.value?.close(); - } - _stateNotifier.value = newState; - } + set state(PopoverState? newState) => _stateNotifier.state = newState; void removeState() { - _stateNotifier.value = null; + _stateNotifier.state = null; } void dispose() { _stateNotifier.dispose(); } } + +class _PopoverStateNotifier extends ChangeNotifier { + PopoverState? _state; + + PopoverState? get state => _state; + + set state(PopoverState? newState) { + if (_state != null && _state != newState) { + _state?.close(); + } + _state = newState; + notifyListeners(); + } +}