|
|
@@ -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();
|
|
|
+ },
|
|
|
+ ),
|
|
|
+ ),
|
|
|
+ ],
|
|
|
),
|
|
|
),
|
|
|
);
|