| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- 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 {
- const MyApp({super.key});
- @override
- State<MyApp> createState() => _MyAppState();
- }
- class _MyAppState extends State<MyApp> {
- /// 平台版本,安卓上就是获取安卓的系统版本号
- String _platformVersion = 'Unknown';
- /// 插件对象
- final _keyboardAndroidPlugin = KeyboardAndroid();
- /// 输入框焦点
- final _inputFocusNode = FocusNode();
- /// 是否启用悬浮窗
- bool _enableFloatingWindow = false;
- @override
- void initState() {
- super.initState();
- initPlatformState();
- }
- /// 获取平台版本
- Future<void> initPlatformState() async {
- String platformVersion;
- try {
- platformVersion =
- await _keyboardAndroidPlugin.getPlatformVersion() ??
- 'Unknown platform version';
- } on PlatformException {
- platformVersion = 'Failed to get platform version.';
- }
- if (!mounted) {
- return;
- }
- setState(() {
- _platformVersion = platformVersion;
- });
- }
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- // 初始化EasyLoading
- builder: EasyLoading.init(),
- home: Scaffold(
- 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();
- },
- ),
- ),
- ],
- ),
- ),
- );
- }
- }
|