Flutter第三方键盘检测插件thirdparty_keyboard_checker的使用

Flutter第三方键盘检测插件thirdparty_keyboard_checker的使用

pub package

thirdparty_keyboard_checker

thirdparty_keyboard_checker 是一个用于检查应用程序是否在Android上使用第三方键盘的插件。同时,它还提供了简单的方法来禁用iOS上的第三方键盘。

安装

首先,在你的 pubspec.yaml 文件中添加 thirdparty_keyboard_checker 作为依赖项:

dependencies:
  thirdparty_keyboard_checker: "^1.0.0"

然后运行 flutter pub get 来获取依赖项。

使用方法

创建实例

首先创建 ThirdPartyKeyboardChecker 的实例:

final _thirdPartyKeyboardChecker = ThirdPartyKeyboardChecker.instance;
Android

你可以通过调用 check3rdKeyboard() 方法来检查当前是否正在使用第三方键盘。如果返回值为 true,则表示当前使用的是第三方键盘,可以弹出提示框告知用户。

final is3rdKeyboard = await _thirdPartyKeyboardChecker.check3rdKeyboard();
if (is3rdKeyboard) {
  // 弹出提示框,告知用户正在使用第三方键盘
  showDialog(context: context, builder: (BuildContext context) => AlertDialog(title: Text("您的应用正在使用第三方键盘")));
}
iOS

为了禁用iOS上的第三方键盘,你需要在 AppDelegate.swift 中添加以下代码:

Swift 5

override func application(_ application: UIApplication, shouldAllowExtensionPointIdentifier extensionPointIdentifier: UIApplication.ExtensionPointIdentifier) -> Bool {
    return extensionPointIdentifier != UIApplication.ExtensionPointIdentifier.keyboard
}

Swift 4.2

override func application(_ application: UIApplication, shouldAllowExtensionPointIdentifier extensionPointIdentifier: UIApplication.ExtensionPointIdentifier) -> Bool {
    return !extensionPointIdentifier == UIApplication.ExtensionPointIdentifier.keyboard
}

Swift 4

override func application(_ application: UIApplication, shouldAllowExtensionPointIdentifier extensionPointIdentifier: UIApplicationExtensionPointIdentifier) -> Bool {
    return !extensionPointIdentifier == UIApplicationExtensionPointIdentifier.keyboard
}

Swift 3

override func application(_ application: UIApplication, shouldAllowExtensionPointIdentifier extensionPointIdentifier: UIApplicationExtensionPointIdentifier) -> Bool {
    return !extensionPointIdentifier == UIApplicationExtensionPointIdentifier.keyboard
}

示例代码

以下是一个完整的示例代码,展示了如何在Flutter应用中使用 thirdparty_keyboard_checker 插件。

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:thirdparty_keyboard_checker/thirdparty_keyboard_checker.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _isCustomKeyboard = '未知';
  final _thirdPartyKeyboardChecker = ThirdPartyKeyboardChecker.instance;

  Future<void> check3rdKeyboard() async {
    final is3rdKeyboard = await _thirdPartyKeyboardChecker.check3rdKeyboard();
    setState(() {
      _isCustomKeyboard = is3rdKeyboard ? '使用第三方键盘' : '默认';
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('插件示例应用')),
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            children: [
              Center(
                child: Text('使用第三方键盘: $_isCustomKeyboard\n'),
              ),
              TextButton(
                onPressed: () {
                  check3rdKeyboard();
                },
                child: const Text('检查是否使用第三方键盘'),
              ),
              const SizedBox(height: 24),
              const Divider(),
              const SizedBox(height: 24),
              const Center(child: Text('禁用自动建议')),
              const TextField(
                autocorrect: false,
                enableSuggestions: false,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

更多关于Flutter第三方键盘检测插件thirdparty_keyboard_checker的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter第三方键盘检测插件thirdparty_keyboard_checker的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


thirdparty_keyboard_checker 是一个用于检测第三方键盘是否在 Flutter 应用中激活的插件。这个插件可以帮助开发者了解用户是否正在使用第三方键盘,并据此调整应用的行为或界面。

安装插件

首先,你需要在 pubspec.yaml 文件中添加 thirdparty_keyboard_checker 依赖:

dependencies:
  flutter:
    sdk: flutter
  thirdparty_keyboard_checker: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来安装插件。

基本用法

  1. 导入插件

    在你的 Dart 文件中导入 thirdparty_keyboard_checker

    import 'package:thirdparty_keyboard_checker/thirdparty_keyboard_checker.dart';
    
  2. 检测第三方键盘

    你可以使用 ThirdPartyKeyboardChecker 类来检测是否正在使用第三方键盘。以下是一个简单的示例:

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: Text('Third Party Keyboard Checker'),
            ),
            body: Center(
              child: FutureBuilder<bool>(
                future: ThirdPartyKeyboardChecker.isThirdPartyKeyboard,
                builder: (context, snapshot) {
                  if (snapshot.connectionState == ConnectionState.waiting) {
                    return CircularProgressIndicator();
                  } else if (snapshot.hasError) {
                    return Text('Error: ${snapshot.error}');
                  } else {
                    final isThirdPartyKeyboard = snapshot.data ?? false;
                    return Text(
                      isThirdPartyKeyboard
                          ? 'Third-party keyboard is active!'
                          : 'Default keyboard is active.',
                    );
                  }
                },
              ),
            ),
          ),
        );
      }
    }
    

    在这个示例中,ThirdPartyKeyboardChecker.isThirdPartyKeyboard 返回一个 Future<bool>,表示是否正在使用第三方键盘。

  3. 监听键盘变化

    如果你想实时监听键盘的变化,可以使用 ThirdPartyKeyboardChecker.onChanged 流:

    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      bool _isThirdPartyKeyboard = false;
    
      @override
      void initState() {
        super.initState();
        ThirdPartyKeyboardChecker.onChanged.listen((isThirdPartyKeyboard) {
          setState(() {
            _isThirdPartyKeyboard = isThirdPartyKeyboard;
          });
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: Text('Third Party Keyboard Checker'),
            ),
            body: Center(
              child: Text(
                _isThirdPartyKeyboard
                    ? 'Third-party keyboard is active!'
                    : 'Default keyboard is active.',
              ),
            ),
          ),
        );
      }
    }
回到顶部