搜索插件、代码仓与 APP 分类

插件详情v0.8.1

keyboard_detection

一个 Flutter 插件,可使用底部视图插入来轻松检测键盘的可见性。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1.6万
喜欢数
29
Pub 分数
160

安装插件

flutter pub add keyboard_detection

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

keyboard_detection 包是一个 Flutter 插件,通过监视底部视图插入来简化键盘可见性的检测。它提供了一种可靠的方法,用于确定在 Android、iOS、Web、macOS、Windows 和 Linux 等各种平台上键盘何时出现或消失。这有助于开发者在输入交互期间动态调整 UI 布局,从而提升用户体验。该插件可无缝集成到现有的 Flutter 应用中,设置简单。在表单、聊天界面以及任何需要根据键盘状态进行响应式布局调整的场景中尤为有用。

适合场景

  • 动态表单布局调整
  • 聊天界面响应性
  • 输入字段焦点管理
  • 适用于移动设备和桌面的响应式用户界面
  • 处理键盘重叠
  • 自动滚动到聚焦字段

主要特点

  • 通过底部视图插入检测键盘可见性
  • 跨平台支持(Android、iOS、Web、macOS、Windows、Linux)
  • 简单的 API 集成
  • 实时键盘状态更新
  • 所需配置最少
  • 适用于物理键盘和屏幕键盘

适合对象

  • Flutter 开发人员构建输入密集型应用
  • 需要自适应UI布局的应用程序
  • 面向多平台的开发者
  • 具有动态表单或聊天界面的项目
  • 寻求轻量级键盘检测的团队

使用建议

  • 可能需要对复杂的嵌套滚动视图进行额外处理
  • 不适合直接检测键盘高度
  • 行为可能在不同平台实现之间略有差异
  • 最好与 Scaffold 或类似的布局结构一起使用
  • 仅限键盘可见;无法检测键盘类型或语言

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Keyboard Detection

Easily detect keyboard visibility in your Flutter app with this plugin. It leverages the resizing of the bottom view inset to determine keyboard visibility, ensuring a native Flutter experience.

Introduction

Keyboard Detection Plugin

Features

  • Detect keyboard visibility changes (unknown, visibling, visible, hiding, hidden).
  • Access keyboard visibility state as an enum or boolean.
  • Listen to keyboard visibility changes via callbacks or streams.
  • Retrieve keyboard size when it is fully loaded.

Simple Usage

Wrap your Scaffold with KeyboardDetection and listen to the onChanged value:

@override
Widget build(BuildContext context) {
  return MaterialApp(
    home: KeyboardDetection(
      controller: KeyboardDetectionController(
        onChanged: (value) {
          print('Keyboard visibility changed: $value');
          setState(() {
            keyboardState = value;
            stateAsBool = keyboardDetectionController.stateAsBool();
            stateAsBoolWithParamTrue =
                keyboardDetectionController.stateAsBool(true);
          });
        },
      ),
      child: Scaffold(
        appBar: AppBar(
          title: const Text('Keyboard Detection'),
        ),
        body: Center(
          child: Column(
            children: [
              Text('State: $keyboardState'),
              Text('State as bool (includeTransitionalState = false): $stateAsBool'),
              Text('State as bool (includeTransitionalState = true): $stateAsBoolWithParamTrue'),
              const TextField(),
            ],
          ),
        ),
      ),
    ),
  );
}

The onChanged callback returns a KeyboardState enum (unknown, visibling, visible, hiding, hidden).

Advanced Usage with Controller

Declare the KeyboardDetectionController outside the build method for more control:

late KeyboardDetectionController keyboardDetectionController;

@override
void initState() {
  keyboardDetectionController = KeyboardDetectionController(
    onChanged: (value) {
      print('Keyboard visibility changed: $value');
      keyboardState = value;
    },
  );

  // Listen to the stream
  _sub = keyboardDetectionController.stream.listen((state) {
    print('Stream update: $state');
  });

  // Add one-time callback
  keyboardDetectionController.addCallback((state) {
    print('One-time callback: $state');
    return false;
  });

  // Add looped callback
  keyboardDetectionController.addCallback((state) {
    print('Looped callback: $state');
    return true;
  });

  // Add looped future callback
  keyboardDetectionController.addCallback((state) async {
    await Future.delayed(const Duration(milliseconds: 100));
    print('Looped future callback: $state');
    return true;
  });

  super.initState();
}

Use the controller in the build method:

@override
Widget build(BuildContext context) {
  return KeyboardDetection(
    controller: keyboardDetectionController,
    child: Scaffold(
      appBar: AppBar(
        title: const Text('Keyboard Detection'),
      ),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(12.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text('State: ${keyboardDetectionController.state}'),
              FutureBuilder(
                future: keyboardDetectionController.ensureSizeLoaded,
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    return Text('Keyboard size loaded: ${keyboardDetectionController.size}');
                  }
                  return const Text('Loading keyboard size...');
                },
              ),
              const TextField(),
              ElevatedButton(
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (_) => const MyApp()),
                  );
                },
                child: const Text('Navigate to another page'),
              ),
              ElevatedButton(
                onPressed: () {
                  Navigator.pushAndRemoveUntil(
                    context,
                    MaterialPageRoute(
                      builder: (_) => const MyApp(),
                    ),
                    (_) => false,
                  );
                },
                child: const Text('Move to another page'),
              ),
            ],
          ),
        ),
      ),
    ),
  );
}
Controller Methods
  • keyboardDetectionController.state: Get the current keyboard visibility state as a KeyboardState enum.
  • keyboardDetectionController.stateAsBool([bool includeTransitionalState = false]): Get the keyboard visibility as a bool?. If includeTransitionalState is true, transitional states (visibling, hiding) are included.
  • keyboardDetectionController.addCallback(callback): Add a callback for state changes. Return true for repeated calls, false to stop.
  • keyboardDetectionController.stream: Listen to keyboard visibility changes as a stream.
  • keyboardDetectionController.size: Get the keyboard size. Use keyboardDetectionController.ensureSizeLoaded to ensure the size is loaded.

Limitations

  • This package uses the bottom inset to detect keyboard visibility, so it doesn't work with floating keyboards (Issue #1).

Contributions

Contributions and feedback are welcome! Feel free to open issues or submit pull requests.

所属分类

源码仓库

使用此插件的项目

暂无相关数据。