home_page.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:keyboard_android/keyboard_android.dart';
  4. import '../util/ToastUtil.dart';
  5. import 'chat_page.dart';
  6. /// 首页
  7. class HomePage extends StatefulWidget {
  8. const HomePage({super.key, required this.title});
  9. /// 页面标题
  10. final String title;
  11. @override
  12. State<HomePage> createState() => _HomePageState();
  13. }
  14. class _HomePageState extends State<HomePage> {
  15. /// 平台版本,安卓上就是获取安卓的系统版本号
  16. String _platformVersion = 'Unknown';
  17. /// 插件对象
  18. final _keyboardAndroidPlugin = KeyboardAndroid();
  19. /// 是否启用悬浮窗
  20. bool _enableFloatingWindow = false;
  21. @override
  22. void initState() {
  23. super.initState();
  24. _initPlatformState();
  25. }
  26. /// 获取平台版本
  27. Future<void> _initPlatformState() async {
  28. String platformVersion;
  29. try {
  30. platformVersion =
  31. await _keyboardAndroidPlugin.getPlatformVersion() ??
  32. 'Unknown platform version';
  33. } on PlatformException {
  34. platformVersion = 'Failed to get platform version.';
  35. }
  36. if (!mounted) {
  37. return;
  38. }
  39. setState(() {
  40. _platformVersion = platformVersion;
  41. });
  42. }
  43. /// 跳转到聊天页面
  44. void _goChatPage(BuildContext context) {
  45. Navigator.push(
  46. context,
  47. MaterialPageRoute(
  48. builder: (context) {
  49. return ChatPage();
  50. },
  51. ),
  52. );
  53. }
  54. @override
  55. Widget build(BuildContext context) {
  56. return Scaffold(
  57. appBar: AppBar(title: Text(widget.title)),
  58. body: Center(
  59. child: Column(
  60. mainAxisAlignment: MainAxisAlignment.center,
  61. children: <Widget>[
  62. Text('当前系统平台版本: $_platformVersion\n'),
  63. SwitchListTile(
  64. title: Text('开启、关闭悬浮窗功能'),
  65. value: _enableFloatingWindow,
  66. onChanged: (newValue) {
  67. _keyboardAndroidPlugin.enableFloatingWindow(newValue);
  68. setState(() {
  69. _enableFloatingWindow = newValue;
  70. });
  71. },
  72. ),
  73. OutlinedButton(
  74. onPressed: () {
  75. _keyboardAndroidPlugin.openInputMethodSettings();
  76. },
  77. child: Text("打开输入法设置"),
  78. ),
  79. OutlinedButton(
  80. onPressed: () async {
  81. bool isTargetKeyboardEnabled =
  82. await _keyboardAndroidPlugin.isTargetKeyboardEnabled();
  83. ToastUtil.showToast(isTargetKeyboardEnabled ? "可用" : "不可用");
  84. },
  85. child: Text("自定义键盘是否可用"),
  86. ),
  87. OutlinedButton(
  88. onPressed: () {
  89. _goChatPage(context);
  90. },
  91. child: Text("跳转到聊天页面"),
  92. ),
  93. ],
  94. ),
  95. ),
  96. );
  97. }
  98. }