패키지, 저장소 및 앱 카테고리 검색

패키지 상세v0.8.1

keyboard_detection

하단 뷰 인셋을 사용하여 키보드의 가시성을 쉽게 감지할 수 있는 플러터 플러그인입니다.

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.

카테고리

소스 저장소

이 패키지를 사용하는 프로젝트

아직 데이터가 없습니다.