搜尋套件、儲存庫與 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.

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。