KeyboardUtil class

键盘工具类

使用示例:


class DemoController extends BaseController {
  @override
  void onInit() {
    super.onInit();
    // 初始化键盘管理器
    KeyboardManager().init(Get.context!);
    // 监听键盘状态
    ever(KeyboardManager()._isKeyboardVisible, (isVisible) {
      if (isVisible) {
        // 键盘显示时的处理
        handleKeyboardShow();
      } else {
        // 键盘隐藏时的处理
        handleKeyboardHide();
      }
    });
  }
  void handleKeyboardShow() {
    // 例如:调整UI布局
    // 滚动到特定位置
    // 显示或隐藏某些组件
  }
  void handleKeyboardHide() {
    // 键盘隐藏时的处理逻辑
  }
  // 在需要时主动隐藏键盘
  void hideKeyboard() {
    KeyboardManager().hideKeyboard();
  }
}

// 在页面的 onInit 中
@override
void onInit() {
  super.onInit();
  // 方式1:安全的调用(不传参数,内部会尝试自动获取 context)
  KeyboardUtil.instance.init();
  // 方式2:在 onReady 中调用更安全
  // KeyboardUtil.instance.init(context);
}
// 或者在 build 中更新状态(最可靠)
@override
Widget build(BuildContext context) {
  // 在 build 中更新,确保键盘状态准确
  KeyboardUtil.instance.updateState(context);
  return ...;
}

Constructors

KeyboardUtil()
factory

Properties

hashCode int
The hash code for this object.
no setterinherited
isKeyboardVisible ↔ RxBool
getter/setter pair
keyboardHeight ↔ RxDouble
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

dispose() → void
清理资源
hideKeyboard() → void
init([BuildContext? context]) → void
带可选 context 参数的安全初始化
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onKeyboardHide() → void
onKeyboardShow() → void
toString() String
A string representation of this object.
inherited
updateState(BuildContext context) → void
更新键盘状态(在页面 build 时调用可确保状态准确)

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Properties

instance KeyboardUtil
no setter