keyboard_height_plugin/lib/keyboard_height_plugin.dart

29 lines
830 B
Dart
Raw Permalink Normal View History

2023-04-28 19:21:41 +00:00
import 'dart:async';
import 'package:flutter/services.dart';
typedef KeyboardHeightCallback = void Function(double height);
class KeyboardHeightPlugin {
2023-04-28 19:31:57 +00:00
static const EventChannel _keyboardHeightEventChannel = EventChannel('keyboardHeightEventChannel');
2023-04-28 19:21:41 +00:00
StreamSubscription? _keyboardHeightSubscription;
void onKeyboardHeightChanged(KeyboardHeightCallback callback) {
if (_keyboardHeightSubscription != null) {
_keyboardHeightSubscription!.cancel();
}
_keyboardHeightSubscription = _keyboardHeightEventChannel
.receiveBroadcastStream()
.listen((dynamic height) {
callback(height as double);
});
}
void dispose() {
if (_keyboardHeightSubscription != null) {
_keyboardHeightSubscription!.cancel();
}
}
}