home_page.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 = true;
  21. @override
  22. void initState() {
  23. super.initState();
  24. _initPlatformState();
  25. // 初始化悬浮窗
  26. _keyboardAndroidPlugin.enableFloatingWindow(_enableFloatingWindow);
  27. }
  28. /// 获取平台版本
  29. Future<void> _initPlatformState() async {
  30. String platformVersion;
  31. try {
  32. platformVersion =
  33. await _keyboardAndroidPlugin.getPlatformVersion() ??
  34. 'Unknown platform version';
  35. } on PlatformException {
  36. platformVersion = 'Failed to get platform version.';
  37. }
  38. if (!mounted) {
  39. return;
  40. }
  41. setState(() {
  42. _platformVersion = platformVersion;
  43. });
  44. }
  45. /// 跳转到聊天页面
  46. void _goChatPage(BuildContext context) {
  47. Navigator.push(
  48. context,
  49. MaterialPageRoute(
  50. builder: (context) {
  51. return ChatPage();
  52. },
  53. ),
  54. );
  55. }
  56. @override
  57. Widget build(BuildContext context) {
  58. return Scaffold(
  59. appBar: AppBar(title: Text(widget.title)),
  60. body: Center(
  61. child: Column(
  62. mainAxisAlignment: MainAxisAlignment.center,
  63. children: <Widget>[
  64. Text('当前系统平台版本: $_platformVersion\n'),
  65. SwitchListTile(
  66. title: Text('开启、关闭悬浮窗功能'),
  67. value: _enableFloatingWindow,
  68. onChanged: (newValue) {
  69. _keyboardAndroidPlugin.enableFloatingWindow(newValue);
  70. setState(() {
  71. _enableFloatingWindow = newValue;
  72. });
  73. },
  74. ),
  75. OutlinedButton(
  76. onPressed: () {
  77. _keyboardAndroidPlugin.openInputMethodSettings();
  78. },
  79. child: Text("打开输入法设置"),
  80. ),
  81. OutlinedButton(
  82. onPressed: () async {
  83. bool isTargetKeyboardEnabled =
  84. await _keyboardAndroidPlugin.isTargetKeyboardEnabled();
  85. ToastUtil.showToast(isTargetKeyboardEnabled ? "可用" : "不可用");
  86. },
  87. child: Text("自定义键盘是否可用"),
  88. ),
  89. OutlinedButton(
  90. onPressed: () {
  91. _goChatPage(context);
  92. },
  93. child: Text("跳转到聊天页面"),
  94. ),
  95. ],
  96. ),
  97. ),
  98. );
  99. }
  100. }