안드로이드, iOS 및 기타 지원 플랫폼에서 소프트 키보드의 가시성 상태를 감지하는 플러터 플러그인입니다. 키보드가 나타나거나 사라질 때 실시간 업데이트를 제공하여 개발자가 UI 레이아웃을 동적으로 조정할 수 있게 해줍니다. 폼, 채팅 인터페이스 및 키보드 존재 여부에 따라 반응형 디자인을 필요로 하는 앱에 이상적입니다. 모바일 및 데스크톱 플랫폼 모두에서 작동하며 일관된 동작을 보장합니다. 플러터의 상태 관리 시스템과 원활하게 통합됩니다.
사용 사례
동적 폼 레이아웃 조정
채팅 인터페이스 반응성
포커스된 필드로 자동 스크롤
키보드 입력 중 반응형 사용자 인터페이스
크로스 플랫폼 키보드 감지
주요 기능
실시간 키보드 가시성 이벤트
Android, iOS, Web, macOS, Windows 및 Linux 지원
간단한 스트림 기반 API
모든 플러터 상태 관리 접근 방식과 호환됩니다
가볍고 신뢰할 수 있음
적합한 대상
동적 UI 레이아웃을 갖춘 앱
폼 중심 애플리케이션
채팅 또는 메시징 앱
다중 플랫폼을 대상으로 하는 개발자
UI/UX 최적화 팀
고려 사항
스트림 구독을 올바르게 처리해야 합니다
복잡한 레이아웃을 위해 추가 로직이 필요할 수 있습니다
상호작용이 없는 UI 요소에 적합하지 않습니다
키보드 표시에 제한되어 입력 방법 전환 불가
스트림에서 적절한 오류 처리와 함께 사용하는 것이 가장 좋습니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
Flutter Keyboard Visibility
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.
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...@overrideWidgetbuild(BuildContext context) {
returnKeyboardVisibilityBuilder(
builder: (context, isKeyboardVisible) {
returnText(
'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...@overrideWidgetbuild(BuildContext context) {
returnKeyboardVisibilityProvider(
child: MyDemoPage(),
);
}
// Within MyDemoPage...@overrideWidgetbuild(BuildContext context) {
finalbool isKeyboardVisible = KeyboardVisibilityProvider.isKeyboardVisible(context);
returnText(
'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.
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...@overrideWidgetbuild(BuildContext context) {
returnKeyboardDismissOnTap(
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...@overrideWidgetbuild(BuildContext context) {
returnKeyboardDismissOnTap(
dismissOnCapturedTaps: true,
child: MyDemoPage(),
);
}
The IgnoreKeyboardDismissWidget 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])
voidmain() {
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);
returnSizedBox();
},
),
),
);
// 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.