init
This commit is contained in:
47
ios/Classes/KeyboardHeightPlugin.swift
Normal file
47
ios/Classes/KeyboardHeightPlugin.swift
Normal file
@@ -0,0 +1,47 @@
|
||||
import Flutter
|
||||
import UIKit
|
||||
|
||||
public class KeyboardHeightPlugin: NSObject, FlutterPlugin, FlutterStreamHandler {
|
||||
private var eventSink: FlutterEventSink?
|
||||
private var eventChannel: FlutterEventChannel?
|
||||
|
||||
public static func register(with registrar: FlutterPluginRegistrar) {
|
||||
let instance = KeyboardHeightPlugin()
|
||||
instance.eventChannel = FlutterEventChannel(name: "keyboardHeightEventChannel", binaryMessenger: registrar.messenger())
|
||||
instance.eventChannel?.setStreamHandler(instance)
|
||||
}
|
||||
|
||||
public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
|
||||
eventSink = events
|
||||
registerKeyboardObservers()
|
||||
return nil
|
||||
}
|
||||
|
||||
public func onCancel(withArguments arguments: Any?) -> FlutterError? {
|
||||
eventSink = nil
|
||||
unregisterKeyboardObservers()
|
||||
return nil
|
||||
}
|
||||
|
||||
private func registerKeyboardObservers() {
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
|
||||
}
|
||||
|
||||
private func unregisterKeyboardObservers() {
|
||||
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
|
||||
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
|
||||
}
|
||||
|
||||
@objc private func keyboardWillShow(notification: NSNotification) {
|
||||
if let userInfo = notification.userInfo,
|
||||
let keyboardFrame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect {
|
||||
eventSink?(NSNumber(value: keyboardFrame.height))
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func keyboardWillHide(notification: NSNotification) {
|
||||
eventSink?(NSNumber(value: 0.0))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user