Ver Fonte

[feat]完善键盘插件例子

hezihao há 8 meses atrás
pai
commit
2166be7d99

+ 6 - 3
lib/plugins/keyboard_android_platform.dart

@@ -16,6 +16,9 @@ class KeyboardAndroidPlatform {
   var tag = "KeyboardAndroidPlatform";
   final ChatRepository chatRepository;
 
+  /// 插件对象
+  final _keyboardAndroidPlugin = KeyboardAndroid();
+
   KeyboardAndroidPlatform(this.chatRepository) {
     AtmobLog.d(tag, '$tag....init');
     init();
@@ -61,16 +64,16 @@ class KeyboardAndroidPlatform {
 
   void enableFloatingWindow(bool enable) {
     debugPrint('enableFloatingWindow $enable');
-    KeyboardAndroid.enableFloatingWindow(enable);
+    _keyboardAndroidPlugin.enableFloatingWindow(enable);
   }
 
   void openInputMethodSettings() {
     debugPrint('openInputMethodSettings');
-    KeyboardAndroid.openInputMethodSettings();
+    _keyboardAndroidPlugin.openInputMethodSettings();
   }
 
   Future<bool> isTargetKeyboardEnabled() {
-    var enable = KeyboardAndroid.isTargetKeyboardEnabled();
+    var enable = _keyboardAndroidPlugin.isTargetKeyboardEnabled();
     debugPrint('isTargetKeyboardEnabled value : $enable');
     return enable;
   }

+ 63 - 13
plugins/keyboard_android/example/lib/main.dart

@@ -2,10 +2,13 @@ import 'package:flutter/material.dart';
 import 'dart:async';
 
 import 'package:flutter/services.dart';
+import 'package:flutter_easyloading/flutter_easyloading.dart';
 import 'package:keyboard_android/keyboard_android.dart';
+import 'package:keyboard_android_example/util/ToastUtil.dart';
 
 void main() {
   runApp(const MyApp());
+  ToastUtil.configLoading();
 }
 
 class MyApp extends StatefulWidget {
@@ -16,31 +19,38 @@ class MyApp extends StatefulWidget {
 }
 
 class _MyAppState extends State<MyApp> {
+  /// 平台版本,安卓上就是获取安卓的系统版本号
   String _platformVersion = 'Unknown';
+
+  /// 插件对象
   final _keyboardAndroidPlugin = KeyboardAndroid();
 
+  /// 输入框焦点
+  final _inputFocusNode = FocusNode();
+
+  /// 是否启用悬浮窗
+  bool _enableFloatingWindow = false;
+
   @override
   void initState() {
     super.initState();
     initPlatformState();
   }
 
-  // Platform messages are asynchronous, so we initialize in an async method.
+  /// 获取平台版本
   Future<void> initPlatformState() async {
     String platformVersion;
-    // Platform messages may fail, so we use a try/catch PlatformException.
-    // We also handle the message potentially returning null.
     try {
       platformVersion =
-          await _keyboardAndroidPlugin.getPlatformVersion() ?? 'Unknown platform version';
+          await _keyboardAndroidPlugin.getPlatformVersion() ??
+          'Unknown platform version';
     } on PlatformException {
       platformVersion = 'Failed to get platform version.';
     }
 
-    // If the widget was removed from the tree while the asynchronous platform
-    // message was in flight, we want to discard the reply rather than calling
-    // setState to update our non-existent appearance.
-    if (!mounted) return;
+    if (!mounted) {
+      return;
+    }
 
     setState(() {
       _platformVersion = platformVersion;
@@ -50,12 +60,52 @@ class _MyAppState extends State<MyApp> {
   @override
   Widget build(BuildContext context) {
     return MaterialApp(
+      // 初始化EasyLoading
+      builder: EasyLoading.init(),
       home: Scaffold(
-        appBar: AppBar(
-          title: const Text('Plugin example app'),
-        ),
-        body: Center(
-          child: Text('Running on: $_platformVersion\n'),
+        appBar: AppBar(title: const Text('Plugin example app')),
+        body: Column(
+          mainAxisAlignment: MainAxisAlignment.center,
+          children: [
+            Text('当前系统平台版本: $_platformVersion\n'),
+            SwitchListTile(
+              title: Text('开启、关闭悬浮窗功能'),
+              value: _enableFloatingWindow,
+              onChanged: (newValue) {
+                _keyboardAndroidPlugin.enableFloatingWindow(newValue);
+                setState(() {
+                  _enableFloatingWindow = newValue;
+                });
+              },
+            ),
+            OutlinedButton(
+              onPressed: () {
+                _keyboardAndroidPlugin.openInputMethodSettings();
+              },
+              child: Text("打开输入法设置"),
+            ),
+            OutlinedButton(
+              onPressed: () async {
+                bool isTargetKeyboardEnabled =
+                    await _keyboardAndroidPlugin.isTargetKeyboardEnabled();
+                ToastUtil.showToast(isTargetKeyboardEnabled ? "可用" : "不可用");
+              },
+              child: Text("自定义键盘是否可用"),
+            ),
+            Container(
+              margin: EdgeInsets.symmetric(vertical: 5, horizontal: 15),
+              child: TextField(
+                focusNode: _inputFocusNode,
+                decoration: InputDecoration(
+                  hintText: '请输入内容',
+                  border: OutlineInputBorder(),
+                ),
+                onTapUpOutside: (event) {
+                  _inputFocusNode.unfocus();
+                },
+              ),
+            ),
+          ],
         ),
       ),
     );

+ 27 - 0
plugins/keyboard_android/example/lib/util/ToastUtil.dart

@@ -0,0 +1,27 @@
+import 'package:flutter/material.dart';
+import 'package:flutter_easyloading/flutter_easyloading.dart';
+
+/// Toast工具类
+class ToastUtil {
+  /// 配置EasyLoading
+  static void configLoading() {
+    EasyLoading.instance
+      ..displayDuration = const Duration(milliseconds: 2000)
+      ..indicatorType = EasyLoadingIndicatorType.fadingCircle
+      ..loadingStyle = EasyLoadingStyle.dark
+      ..indicatorSize = 45.0
+      ..radius = 10.0
+      ..progressColor = Colors.yellow
+      ..backgroundColor = Colors.green
+      ..indicatorColor = Colors.yellow
+      ..textColor = Colors.yellow
+      ..maskColor = Colors.blue.withOpacity(0.5)
+      ..userInteractions = true
+      ..dismissOnTap = false;
+  }
+
+  /// 显示Toast
+  static void showToast(String msg) {
+    EasyLoading.showToast(msg);
+  }
+}

+ 16 - 0
plugins/keyboard_android/example/pubspec.lock

@@ -75,6 +75,14 @@ packages:
     description: flutter
     source: sdk
     version: "0.0.0"
+  flutter_easyloading:
+    dependency: "direct main"
+    description:
+      name: flutter_easyloading
+      sha256: ba21a3c883544e582f9cc455a4a0907556714e1e9cf0eababfcb600da191d17c
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "3.0.5"
   flutter_lints:
     dependency: "direct dev"
     description:
@@ -83,6 +91,14 @@ packages:
       url: "https://pub.flutter-io.cn"
     source: hosted
     version: "5.0.0"
+  flutter_spinkit:
+    dependency: transitive
+    description:
+      name: flutter_spinkit
+      sha256: d2696eed13732831414595b98863260e33e8882fc069ee80ec35d4ac9ddb0472
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "5.2.1"
   flutter_test:
     dependency: "direct dev"
     description: flutter

+ 3 - 0
plugins/keyboard_android/example/pubspec.yaml

@@ -17,6 +17,7 @@ dependencies:
   flutter:
     sdk: flutter
 
+  # 依赖本插件
   keyboard_android:
     # When depending on this package from a real application you should use:
     #   keyboard_android: ^x.y.z
@@ -28,6 +29,8 @@ dependencies:
   # The following adds the Cupertino Icons font to your application.
   # Use with the CupertinoIcons class for iOS style icons.
   cupertino_icons: ^1.0.8
+  # Loading弹窗和Toast
+  flutter_easyloading: ^3.0.5
 
 dev_dependencies:
   integration_test:

+ 7 - 5
plugins/keyboard_android/lib/keyboard_android.dart

@@ -1,24 +1,26 @@
 import 'keyboard_android_platform_interface.dart';
 
 class KeyboardAndroid {
+  /// 获取平台版本
   Future<String?> getPlatformVersion() {
     return KeyboardAndroidPlatform.instance.getPlatformVersion();
   }
 
-  static Future<void> enableFloatingWindow(bool enable) async {
+  /// 打开或关闭悬浮窗
+  Future<void> enableFloatingWindow(bool enable) async {
     return KeyboardAndroidPlatform.instance.enableFloatingWindow(enable);
   }
 
-  static Future<void> openInputMethodSettings() async {
+  /// 打开输入法设置
+  Future<void> openInputMethodSettings() async {
     return KeyboardAndroidPlatform.instance.openInputMethodSettings();
   }
 
-  static Future<bool> isTargetKeyboardEnabled() async {
+  /// 检查目标键盘是否启用
+  Future<bool> isTargetKeyboardEnabled() async {
     return KeyboardAndroidPlatform.instance.isTargetKeyboardEnabled();
   }
 
-
-
   // /// 获取键映射
   // static Future<List<Map<String, String>>> getKeyMappings() {
   //   return KeyboardAndroidPlatform.instance.getKeyMappings();

+ 0 - 3
plugins/keyboard_android/lib/keyboard_android_method_channel.dart

@@ -17,7 +17,6 @@ class MethodChannelKeyboardAndroid extends KeyboardAndroidPlatform {
     return version;
   }
 
-  /// 打开悬浮窗
   @override
   Future<void> enableFloatingWindow(bool enable) async {
     await methodChannel.invokeMethod('enableFloatingWindow', {
@@ -25,13 +24,11 @@ class MethodChannelKeyboardAndroid extends KeyboardAndroidPlatform {
     });
   }
 
-  /// 打开输入法设置
   @override
   Future<void> openInputMethodSettings() async {
     await methodChannel.invokeMethod('openInputMethodSettings');
   }
 
-  /// 检查目标键盘是否启用
   @override
   Future<bool> isTargetKeyboardEnabled() async {
     return await methodChannel.invokeMethod<bool>('isTargetKeyboardEnabled') ??

+ 0 - 1
plugins/keyboard_android/lib/keyboard_android_platform_interface.dart

@@ -39,7 +39,6 @@ abstract class KeyboardAndroidPlatform extends PlatformInterface {
     );
   }
 
-
   Future<bool> isTargetKeyboardEnabled() {
     throw UnimplementedError(
       'isTargetKeyboardEnabled() has not been implemented.',

+ 26 - 26
pubspec.lock

@@ -37,18 +37,18 @@ packages:
     dependency: transitive
     description:
       name: archive
-      sha256: "0c64e928dcbefddecd234205422bcfc2b5e6d31be0b86fef0d0dd48d7b4c9742"
+      sha256: "7dcbd0f87fe5f61cb28da39a1a8b70dbc106e2fe0516f7836eb7bb2948481a12"
       url: "https://pub.dev"
     source: hosted
-    version: "4.0.4"
+    version: "4.0.5"
   args:
     dependency: transitive
     description:
       name: args
-      sha256: bf9f5caeea8d8fe6721a9c358dd8a5c1947b27f1cfaa18b39c301273594919e6
+      sha256: d0481093c50b1da8910eb0bb301626d4d8eb7284aa739614d2b394ee09e3ea04
       url: "https://pub.dev"
     source: hosted
-    version: "2.6.0"
+    version: "2.7.0"
   async:
     dependency: transitive
     description:
@@ -149,10 +149,10 @@ packages:
     dependency: transitive
     description:
       name: built_value
-      sha256: "8b158ab94ec6913e480dc3f752418348b5ae099eb75868b5f4775f0572999c61"
+      sha256: ea90e81dc4a25a043d9bee692d20ed6d1c4a1662a28c03a96417446c093ed6b4
       url: "https://pub.dev"
     source: hosted
-    version: "8.9.4"
+    version: "8.9.5"
   cached_network_image:
     dependency: "direct main"
     description:
@@ -197,10 +197,10 @@ packages:
     dependency: transitive
     description:
       name: chewie
-      sha256: "645fbca3f22309381edb5af59a4c8aa544a3d3872d7b7b7c986c2b18b3bdd265"
+      sha256: "0bf6f7692cb65f7b8f59a2a17025b9cbe8f75ab4251e66161a4fc86162475fb6"
       url: "https://pub.dev"
     source: hosted
-    version: "1.10.0"
+    version: "1.11.0"
   clock:
     dependency: transitive
     description:
@@ -824,10 +824,10 @@ packages:
     dependency: transitive
     description:
       name: package_config
-      sha256: "92d4488434b520a62570293fbd33bb556c7d49230791c1b4bbd973baf6d2dc67"
+      sha256: f096c55ebb7deb7e384101542bfba8c52696c1b56fca2eb62827989ef2353bbc
       url: "https://pub.dev"
     source: hosted
-    version: "2.1.1"
+    version: "2.2.0"
   package_info_plus:
     dependency: "direct main"
     description:
@@ -880,10 +880,10 @@ packages:
     dependency: transitive
     description:
       name: path_provider_android
-      sha256: "4adf4fd5423ec60a29506c76581bc05854c55e3a0b72d35bb28d661c9686edf2"
+      sha256: "0ca7359dad67fd7063cb2892ab0c0737b2daafd807cf1acecd62374c8fae6c12"
       url: "https://pub.dev"
     source: hosted
-    version: "2.2.15"
+    version: "2.2.16"
   path_provider_foundation:
     dependency: transitive
     description:
@@ -976,18 +976,18 @@ packages:
     dependency: transitive
     description:
       name: provider
-      sha256: c8a055ee5ce3fd98d6fc872478b03823ffdb448699c6ebdbbc71d59b596fd48c
+      sha256: "489024f942069c2920c844ee18bb3d467c69e48955a4f32d1677f71be103e310"
       url: "https://pub.dev"
     source: hosted
-    version: "6.1.2"
+    version: "6.1.4"
   pub_semver:
     dependency: transitive
     description:
       name: pub_semver
-      sha256: "7b3cfbf654f3edd0c6298ecd5be782ce997ddf0e00531b9464b55245185bbbbd"
+      sha256: "5bfcf68ca79ef689f8990d1160781b4bad40a3bd5e5218ad4076ddb7f4081585"
       url: "https://pub.dev"
     source: hosted
-    version: "2.1.5"
+    version: "2.2.0"
   pubspec_parse:
     dependency: transitive
     description:
@@ -1229,10 +1229,10 @@ packages:
     dependency: transitive
     description:
       name: url_launcher_android
-      sha256: "6fc2f56536ee873eeb867ad176ae15f304ccccc357848b351f6f0d8d4a40d193"
+      sha256: "1d0eae19bd7606ef60fe69ef3b312a437a16549476c42321d5dc1506c9ca3bf4"
       url: "https://pub.dev"
     source: hosted
-    version: "6.3.14"
+    version: "6.3.15"
   url_launcher_ios:
     dependency: transitive
     description:
@@ -1333,10 +1333,10 @@ packages:
     dependency: transitive
     description:
       name: video_player_android
-      sha256: "412e0b474c79bb7df14bf25d2b278a1e025760d3a1aaaa9fd712d9d17f589151"
+      sha256: ae7d4f1b41e3ac6d24dd9b9d5d6831b52d74a61bdd90a7a6262a33d8bb97c29a
       url: "https://pub.dev"
     source: hosted
-    version: "2.8.0"
+    version: "2.8.2"
   video_player_avfoundation:
     dependency: transitive
     description:
@@ -1373,10 +1373,10 @@ packages:
     dependency: transitive
     description:
       name: wakelock_plus
-      sha256: "36c88af0b930121941345306d259ec4cc4ecca3b151c02e3a9e71aede83c615e"
+      sha256: b90fbcc8d7bdf3b883ea9706d9d76b9978cb1dfa4351fcc8014d6ec31a493354
       url: "https://pub.dev"
     source: hosted
-    version: "1.2.10"
+    version: "1.2.11"
   wakelock_plus_platform_interface:
     dependency: transitive
     description:
@@ -1429,10 +1429,10 @@ packages:
     dependency: transitive
     description:
       name: webview_flutter_android
-      sha256: "512c26ccc5b8a571fd5d13ec994b7509f142ff6faf85835e243dde3538fdc713"
+      sha256: e09150b28a07933839adef0e4a088bb43e8c8d9e6b93025b01882d4067a58ab0
       url: "https://pub.dev"
     source: hosted
-    version: "4.3.2"
+    version: "4.3.4"
   webview_flutter_platform_interface:
     dependency: transitive
     description:
@@ -1453,10 +1453,10 @@ packages:
     dependency: transitive
     description:
       name: win32
-      sha256: b89e6e24d1454e149ab20fbb225af58660f0c0bf4475544650700d8e2da54aef
+      sha256: dc6ecaa00a7c708e5b4d10ee7bec8c270e9276dfcab1783f57e9962d7884305f
       url: "https://pub.dev"
     source: hosted
-    version: "5.11.0"
+    version: "5.12.0"
   win32_registry:
     dependency: transitive
     description: