当前位置:网站首页>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);
});
}
```
\
边栏推荐
- Using SQL Server FOR XML and FOR JSON syntax on other RDBMSs with jOOQ
- 栈和队列的基本概念
- 基本语法(一)
- Exploring Plain Vision Transformer Backbones for Object Detection 论文阅读笔记
- imx6ull看门狗使用
- Introduction to using NPM
- SAP message TK 248 solved
- Json和对象之间转换的封装(Gson)
- IDEA版Postman插件Restful Fast Request,细节到位,功能好用
- Encapsulation of conversion between Json and objects (Gson)
猜你喜欢
centos7安装mysql5.7步骤(图解版)
NameNode (NN) and SecondaryNameNode (2NN) working mechanism
How does the SAP ABAP OData service support the $filter (filter) operation trial version
荣耀手机参数写错,客服认为没错
手撕Verilog PWM呼吸灯
函数递归1.0
Introduction to using NPM
AMBA APB学习记录(AMBA 3/4)
Acwing第 62 场周赛【未完结】
365天挑战LeetCode1000题——Day 044 最大层内元素和 层次遍历
随机推荐
集群中增加数据节点与退役数据节点
三相PWM整流器预测直接功率控制
AMBA APB学习记录(AMBA 2.0)
函数递归1.0
PyQt5 rapid development and actual combat 10.2 compound interest calculation && 10.3 refresh blog clicks
vivado里那些看不懂的原语
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)
深度学习基本概念
Hybrid brain-computer interface system based on steady-state visual evoked potentials and attentional EEG
SAP ABAP OData 服务如何支持 $filter (过滤)操作试读版
jmeter性能测试步骤入门(性能测试工具jmeter)
WPF中报错:“未将对象引用设置到对象的实例。”
手撕Verilog PWM呼吸灯
Adding data nodes and decommissioning data nodes in the cluster
How does the SAP ABAP OData service support the $filter (filter) operation trial version
查看Mysql数据库版本
golang八股文整理(持续搬运)
五种数据提交方式的优化
sqlalchemy determines whether a field of type array has at least one consistent data with an array
Basic use of dosbox [easy to understand]