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

패키지 상세v7.0.1

flutter_keyboard_visibility

Android 및 iOS에서 소프트 키보드 표시 상태를 확인하기 위한 Flutter 플러그인입니다.

AndroidiOSWebmacOSWindowsLinux
30일 다운로드
76.5만
좋아요
1.2천
Pub 점수
160

패키지 설치

flutter pub add flutter_keyboard_visibility

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

안드로이드, iOS 및 기타 지원 플랫폼에서 소프트 키보드의 가시성 상태를 감지하는 플러터 플러그인입니다. 키보드가 나타나거나 사라질 때 실시간 업데이트를 제공하여 개발자가 UI 레이아웃을 동적으로 조정할 수 있게 해줍니다. 폼, 채팅 인터페이스 및 키보드 존재 여부에 따라 반응형 디자인을 필요로 하는 앱에 이상적입니다. 모바일 및 데스크톱 플랫폼 모두에서 작동하며 일관된 동작을 보장합니다. 플러터의 상태 관리 시스템과 원활하게 통합됩니다.

사용 사례

  • 동적 폼 레이아웃 조정
  • 채팅 인터페이스 반응성
  • 포커스된 필드로 자동 스크롤
  • 키보드 입력 중 반응형 사용자 인터페이스
  • 크로스 플랫폼 키보드 감지

주요 기능

  • 실시간 키보드 가시성 이벤트
  • Android, iOS, Web, macOS, Windows 및 Linux 지원
  • 간단한 스트림 기반 API
  • 모든 플러터 상태 관리 접근 방식과 호환됩니다
  • 가볍고 신뢰할 수 있음

적합한 대상

  • 동적 UI 레이아웃을 갖춘 앱
  • 폼 중심 애플리케이션
  • 채팅 또는 메시징 앱
  • 다중 플랫폼을 대상으로 하는 개발자
  • UI/UX 최적화 팀

고려 사항

  • 스트림 구독을 올바르게 처리해야 합니다
  • 복잡한 레이아웃을 위해 추가 로직이 필요할 수 있습니다
  • 상호작용이 없는 UI 요소에 적합하지 않습니다
  • 키보드 표시에 제한되어 입력 방법 전환 불가
  • 스트림에서 적절한 오류 처리와 함께 사용하는 것이 가장 좋습니다

원본 README

연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.

Flutter Keyboard Visibility

pub package ci codecov

React to keyboard visibility changes.

Note about Flutter Web and Desktop support

Web support is an open issue here, desktop support is an open issue here. Currently this library will just return false for keyboard visibility on web and desktop.

Install

Install the package

Usage: React to Keyboard Visibility Changes

Option 1: Within your Widget tree using a builder

Build your Widget tree based on whether or not the keyboard is visible by using KeyboardVisibilityBuilder.

import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';

/// In any of your widgets...
@override
Widget build(BuildContext context) {
  return KeyboardVisibilityBuilder(
      builder: (context, isKeyboardVisible) {
        return Text(
          'The keyboard is: ${isKeyboardVisible ? 'VISIBLE' : 'NOT VISIBLE'}',
        );
      }
  );
Option 2: Within your Widget tree using a provider

Build your Widget tree based on whether or not the keyboard is visible by including a KeyboardVisibilityProvider near the top of your tree.

import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';

// Somewhere near the top of your tree...
@override
Widget build(BuildContext context) {
  return KeyboardVisibilityProvider(
    child: MyDemoPage(),
  );
}

// Within MyDemoPage...
@override
Widget build(BuildContext context) {
  final bool isKeyboardVisible = KeyboardVisibilityProvider.isKeyboardVisible(context);
  return Text(
    'The keyboard is: ${isKeyboardVisible ? 'VISIBLE' : 'NOT VISIBLE'}',
  );
}
Option 3: Direct query and subscription

Query and/or subscribe to keyboard visibility directly with the KeyboardVisibilityController class.

import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'dart:async';

late StreamSubscription<bool> keyboardSubscription;

@override
void initState() {
  super.initState();

  var keyboardVisibilityController = KeyboardVisibilityController();
  // Query
  print('Keyboard visibility direct query: ${keyboardVisibilityController.isVisible}');

  // Subscribe
  keyboardSubscription = keyboardVisibilityController.onChange.listen((bool visible) {
    print('Keyboard visibility update. Is visible: $visible');
  });
}

@override
void dispose() {
  keyboardSubscription.cancel();
  super.dispose();
}

Usage: Dismiss keyboard on tap

Place a KeyboardDismissOnTap near the top of your Widget tree. When a user taps outside of the currently focused Widget, the Widget will drop focus and the keyboard will be dismissed.

import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';

// Somewhere near the top of your tree...
@override
Widget build(BuildContext context) {
  return KeyboardDismissOnTap(
    child: MyDemoPage(),
  );
}

By default KeyboardDismissOnTap will only dismiss taps not captured by other interactive Widgets, like buttons. If you would like to dismiss the keyboard for any tap, including taps on interactive Widgets, set dismissOnCapturedTaps to true.

import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';

// Somewhere near the top of your tree...
@override
Widget build(BuildContext context) {
  return KeyboardDismissOnTap(
    dismissOnCapturedTaps: true,
    child: MyDemoPage(),
  );
}

The IgnoreKeyboardDismiss Widget can be used to further refine which taps do and do not dismiss the keyboard. Checkout the example app for more detail.

Testing

Testing using mocks

KeyboardVisibilityProvider and KeyboardVisibilityBuilder accept a controller parameter that allow you to mock or replace the logic for reporting keyboard visibility updates.

@GenerateMocks([KeyboardVisibilityController])
void main() {
  testWidgets('It reports true when the keyboard is visible', (WidgetTester tester) async {
    // Pretend that the keyboard is visible.
    var mockController = MockKeyboardVisibilityController();
    when(mockController.onChange)
        .thenAnswer((_) => Stream.fromIterable([true]));
    when(mockController.isVisible).thenAnswer((_) => true);

    // Build a Widget tree and query KeyboardVisibilityProvider
    // for the visibility of the keyboard.
    bool? isKeyboardVisible;

    await tester.pumpWidget(
      KeyboardVisibilityProvider(
        controller: mockController,
        child: Builder(
          builder: (BuildContext context) {
            isKeyboardVisible =
                KeyboardVisibilityProvider.isKeyboardVisible(context);
            return SizedBox();
          },
        ),
      ),
    );

    // Verify that KeyboardVisibilityProvider reported that the
    // keyboard is visible.
    expect(isKeyboardVisible, true);
  });
}
Testing with a global override

Call KeyboardVisibilityTesting.setVisibilityForTesting(false); to set a custom value to use during flutter test. This is set globally and will override the standard logic of the native platform.

void main() {
  testWidgets('My Test', (WidgetTester tester) async {
    KeyboardVisibilityTesting.setVisibilityForTesting(true);
    await tester.pumpWidget(MyApp());
  });
}

카테고리

의존성

의존성 목록 6 개

  • flutter_keyboard_visibility_linux^2.0.0
  • flutter_keyboard_visibility_macos^2.0.0
  • flutter_keyboard_visibility_platform_interface^3.0.0
  • flutter_keyboard_visibility_web^3.0.0
  • flutter_keyboard_visibility_windows^2.0.0
  • meta^1.18.0

소스 저장소

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

순위