当前位置:网站首页>Flutter keyboard visibility
Flutter keyboard visibility
2022-07-31 12:44:00 【HUAWEI CLOUD】
运行此命令:
使用Flutter:
```
$ flutter pub add flutter_keyboard_visibility
```
这将在你的包的 pubspec.yaml 中添加这样的一行(并运行一个隐式`flutter pub get`):
```
dependencies:
flutter_keyboard_visibility: ^5.3.0
```
或者,您的编辑器可能支持`flutter pub get`.
React to keyboard visibility changes.
### 关于 Flutter Web 支持的注意事项
Web 支持[在这里](https://github.com/MisterJimson/flutter_keyboard_visibility/issues/10)是一个悬而未决的问题.Currently this library will only return`false`Keyboard visibility on the web.
## 安装
[安装包](https://pub.dev/packages/flutter_keyboard_visibility/install)
## 用法:React to keyboard visibility changes
### 1:在您的`Widget`Use builders in the tree
使用 . Build yours based on whether the keyboard is visible or not Widget 树`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'}',
);
}
);
```
### 2:在您的`Widget`树中使用
`Widget`Depending on whether the keyboard is visible or not,`KeyboardVisibilityProvider`Add one near the top of the tree to build the 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'}',
);
}
```
### 3.Direct query subscription
Use the class query and directly/Or subscribe to keyboard visibility `KeyboardVisibilityController`.
```
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();
}
```
## Click to close the keyboard
在树顶`KeyboardDismissOnTap`Put one nearby.`Widget`When the user clicks outside the current focus`Widget`,`Widget`will lose 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(),
);
}
```
默认情况下`KeyboardDismissOnTap`,will just close other interactions`Widget`s Uncaptured clicks,例如按钮.If you want to close the keyboard for any clicks,included in the interaction`Widget`s 上的点击,请设置`dismissOnCapturedTaps`为 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(),
);
}
```
## 测试
### Use mock tests
```
@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);
});
}
```
\
边栏推荐
- IDEA版Postman插件Restful Fast Request,细节到位,功能好用
- sqlalchemy determines whether a field of type array has at least one consistent data with an array
- 给你一个大厂面试的机会,你能面试上吗?进来看看!
- Centos7 install mysql5.7 steps (graphical version)
- ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
- Markdown编辑器语法
- 深度学习基本概念
- 集群的安全模式
- Use docker to build mysql master-slave
- 使用docker搭建mysql主从
猜你喜欢

CentOS7 —— yum安装mysql

PyQt5快速开发与实战 9.7 UI层的自动化测试

【CPU设计实战】简单流水线CPU设计

Google Chrome(谷歌浏览器)安装使用

MySQL面试八股文(2022最新整理)

ERROR 1819 (HY000) Your password does not satisfy the current policy requirements

SAP 电商云 Spartacus UI 和 Accelerator UI 里的 ASM 模块

WebGL给Unity传递参数问题1: Cannot read properties of undefined (reading ‘SendMessage‘)

Full GC (Ergonomics)排查分析

busybox之reboot命令流程分析
随机推荐
365天挑战LeetCode1000题——Day 044 最大层内元素和 层次遍历
Structural controllability of switched linear systems with symmetry constraints
IDEA版Postman插件Restful Fast Request,细节到位,功能好用
Architecture Camp | Module 8
Optimization of five data submission methods
golang中使用泛型
[core]-ARMV7-A、ARMV8-A、ARMV9-A 架构简介「建议收藏」
PAT考试总结(考试心得)
字符函数和字符串函数
Adding data nodes and decommissioning data nodes in the cluster
Basic use of dosbox [easy to understand]
Indoor real-time laser SLAM control method based on biological excitation neural network
Anaconda安装labelImg图像标注软件
【核心概念】图像分类和目标检测中的正负样本划分以及架构理解
Wearing detection and action recognition of protective gear based on pose estimation
【Shader】Shader官方示例[通俗易懂]
最长算术(暑假每日一题 11)
dosbox基础使用[通俗易懂]
Use docker to build mysql master-slave
想吃菌子,当然是自己上山找了