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