forked from AppFlowy-IO/appflowy-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor keyboard height observer to support multiple listeners (…
- Loading branch information
Showing
2 changed files
with
58 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
lib/src/editor/toolbar/mobile/utils/keyboard_height_observer.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import 'package:keyboard_height_plugin/keyboard_height_plugin.dart'; | ||
|
||
typedef KeyboardHeightCallback = void Function(double height); | ||
|
||
// the KeyboardHeightPlugin only accepts one listener, so we need to create a | ||
// singleton class to manage the multiple listeners. | ||
class KeyboardHeightObserver { | ||
KeyboardHeightObserver._() { | ||
_keyboardHeightPlugin.onKeyboardHeightChanged((height) { | ||
notify(height); | ||
}); | ||
} | ||
|
||
static final KeyboardHeightObserver instance = KeyboardHeightObserver._(); | ||
|
||
final List<KeyboardHeightCallback> _listeners = []; | ||
final KeyboardHeightPlugin _keyboardHeightPlugin = KeyboardHeightPlugin(); | ||
|
||
void addListener(KeyboardHeightCallback listener) { | ||
_listeners.add(listener); | ||
} | ||
|
||
void removeListener(KeyboardHeightCallback listener) { | ||
_listeners.remove(listener); | ||
} | ||
|
||
void dispose() { | ||
_listeners.clear(); | ||
_keyboardHeightPlugin.dispose(); | ||
} | ||
|
||
void notify(double height) { | ||
for (final listener in _listeners) { | ||
listener(height); | ||
} | ||
} | ||
} |