import 'package:flutter/services.dart';
import 'dart:async';

import 'package:keyboard/utils/default_keyboard_helper.dart';

class KeyboardDetectIOSUtil {

  static const EventChannel _eventChannel = EventChannel('keyboard_ios_events');

  // 流控制器
  static final StreamController<bool> _keyboardStreamController = StreamController<bool>.broadcast();

  // 公开的流，用于监听键盘变化
  static Stream<bool> get keyboardStream => _keyboardStreamController.stream;

  // 初始化
  static void initialize() {
    // 监听来自原生代码的事件
    _eventChannel.receiveBroadcastStream().listen((dynamic event) {
      final bool isCustomKeyboard = event as bool;
      _keyboardStreamController.add(isCustomKeyboard);
      DefaultKeyboardHelper.isDefaultKeyboard.value = isCustomKeyboard;
    });
  }

  // 释放资源
  static void dispose() {
    _keyboardStreamController.close();
  }
}