パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細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アプリにシームレスに統合でき、設定も最小限ですみます。フォームやチャットインターフェース、キーボード状態に基づいてレイアウトを応答的に調整する必要があるすべてのシナリオにおいて特に役立ちます。

用途

  • 動的フォームレイアウトの調整
  • チャットインターフェースの応答性
  • 入力フィールドのフォーカス管理
  • モバイルおよびデスクトップ用のレスポンシブUI
  • キーボードのオーバーラップの処理
  • フォーカスされたフィールドに自動スクロール

主な機能

  • 下部ビューインセットを介してキーボードの表示状態を検出します
  • クロスプラットフォーム対応(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.

カテゴリー

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。