Flutter键盘可见性

举报
坚果的博客 发表于 2022/07/31 11:57:51 2022/07/31
【摘要】 运行此命令:使用Flutter:``` $ flutter pub add flutter_keyboard_visibility```这将在你的包的 pubspec.yaml 中添加这样的一行(并运行一个隐式`flutter pub get`):```dependencies:  flutter_keyboard_visibility: ^5.3.0```或者,您的编辑器可能支持`flut...


运行此命令:

使用Flutter:

```
 $ flutter pub add flutter_keyboard_visibility
```

这将在你的包的 pubspec.yaml 中添加这样的一行(并运行一个隐式`flutter pub get`):

```
dependencies:
  flutter_keyboard_visibility: ^5.3.0
```

或者,您的编辑器可能支持`flutter pub get`。

对键盘可见性变化做出反应。

### 关于 Flutter Web 支持的注意事项

Web 支持[在这里](https://github.com/MisterJimson/flutter_keyboard_visibility/issues/10)是一个悬而未决的问题。目前这个库将只返回`false`网络上的键盘可见性。

## 安装

[安装包](https://pub.dev/packages/flutter_keyboard_visibility/install)

## 用法:对键盘可见性更改做出反应

### 1:在您的`Widget`树中使用构建器

使用 . 根据键盘是否可见构建您的 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`根据键盘是否可见,`KeyboardVisibilityProvider`在树的顶部附近添加一个来构建树。

```
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.直接查询订阅

直接使用类查询和/或订阅键盘可见性 `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();
}
```

## 点击关闭键盘

在树顶`KeyboardDismissOnTap`附近放置一个。`Widget`当用户在当前焦点之外点击时`Widget`,`Widget`将失去焦点并且键盘将被关闭。

```
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`,只会关闭其他交互`Widget`s 未捕获的点击,例如按钮。如果您想关闭任何点击的键盘,包括在交互`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(),
  );
}
```

## 测试

### 使用模拟测试

```
@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);
  });
}
```

\

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。