keyboard_guide_page.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_screenutil/flutter_screenutil.dart';
  3. import 'package:get/get.dart';
  4. import 'package:keyboard/module/keyboard_guide/keyboard_guide_controller.dart';
  5. import 'package:keyboard/router/app_pages.dart';
  6. import 'package:keyboard/utils/toast_util.dart';
  7. import 'package:keyboard/widget/platform_util.dart';
  8. import 'package:lottie/lottie.dart';
  9. import '../../base/base_page.dart';
  10. import '../../data/bean/keyboard_guide_msg.dart';
  11. import '../../data/consts/event_report.dart';
  12. import '../../handler/event_handler.dart';
  13. import '../../resource/assets.gen.dart';
  14. import '../../resource/colors.gen.dart';
  15. import '../../resource/string.gen.dart';
  16. import '../../utils/clipboard_util.dart';
  17. import '../../utils/url_launcher_util.dart';
  18. import '../../widget/app_lifecycle_widget.dart';
  19. import '../intimacy_scale/intimacy_scale_page.dart';
  20. import 'enums/keyboard_guide_msg_type.dart';
  21. /// 键盘引导页面
  22. class KeyboardGuidePage extends BasePage<KeyboardGuidePageController> {
  23. const KeyboardGuidePage({super.key});
  24. /// 跳转到键盘引导页
  25. static void start() {
  26. Get.toNamed(RoutePath.keyboardGuide);
  27. }
  28. /// 跳转并关闭当前页
  29. static void startAndOffMe() {
  30. Get.offNamed(RoutePath.keyboardGuide);
  31. }
  32. @override
  33. immersive() {
  34. return false;
  35. }
  36. @override
  37. Widget buildBody(BuildContext context) {
  38. return Scaffold(
  39. backgroundColor: backgroundColor(),
  40. body: AppLifecycleWidget(
  41. onAppLifecycleCallback: (isForeground) {
  42. // 完成教程
  43. controller.setNotFirstShowKeyboardTutorial();
  44. if (isForeground) {
  45. // 切换到前台时,重新检查设置,更新按钮状态
  46. controller.checkSetting();
  47. // 如果选择为默认键盘了,则尝试显示引导弹窗
  48. if (controller.isDefaultKeyboard.value) {
  49. controller.showGuideOverlayDialog();
  50. }
  51. }
  52. },
  53. child: Column(
  54. children: [
  55. // 使用 Obx 监听 isDefaultKeyboard 的变化
  56. Obx(() {
  57. // 当 isDefaultKeyboard 变为 true 时,显示引导对话框
  58. if (controller.isDefaultKeyboard.value) {
  59. // 使用 Future.microtask 确保在构建完成后调用
  60. Future.microtask(() => controller.showGuideOverlayDialog());
  61. }
  62. // 返回一个空的 SizedBox,不影响 UI
  63. return SizedBox.shrink();
  64. }),
  65. // 标题栏
  66. _buildTitleBar(),
  67. // 消息列表
  68. Expanded(flex: 1, child: _buildContent()),
  69. // 底部输入栏
  70. _buildBottomInput(),
  71. ],
  72. ),
  73. ),
  74. );
  75. }
  76. // 标题栏
  77. Widget _buildTitleBar() {
  78. return Container(
  79. color: backgroundColor(),
  80. height: kToolbarHeight,
  81. padding: EdgeInsets.symmetric(horizontal: 16.0),
  82. child: Row(
  83. children: [
  84. // 返回按钮
  85. GestureDetector(
  86. onTap: controller.clickBack,
  87. child: Assets.images.iconMineBackArrow.image(
  88. width: 24.w,
  89. height: 24.h,
  90. ),
  91. ),
  92. // 标题
  93. Expanded(
  94. child: Container(
  95. alignment: Alignment.center,
  96. child: Text("", style: const TextStyle(fontSize: 18)),
  97. ),
  98. ),
  99. // 右侧按钮
  100. GestureDetector(
  101. onTap: () async {
  102. bool result = await UrlLauncherUtil.openWeChat();
  103. EventHandler.report(EventId.event_03007);
  104. if (!result) {
  105. ToastUtil.show(StringName.keyboardGuideWechatNotInstall);
  106. }
  107. },
  108. child: Container(
  109. padding: EdgeInsets.only(
  110. left: 12.w,
  111. right: 14.w,
  112. top: 10.w,
  113. bottom: 10.w,
  114. ),
  115. decoration: BoxDecoration(
  116. image: DecorationImage(
  117. image: Assets.images.bgGoApp.provider(),
  118. fit: BoxFit.fill,
  119. ),
  120. ),
  121. child: Row(
  122. children: [
  123. Assets.images.iconWechat.image(height: 22.w, width: 22.w),
  124. SizedBox(width: 1.0),
  125. Text(
  126. StringName.keyboardGuideGoWechat,
  127. style: TextStyle(
  128. color: ColorName.black80,
  129. fontSize: 12,
  130. fontWeight: FontWeight.w400,
  131. ),
  132. ),
  133. ],
  134. ),
  135. ),
  136. ),
  137. ],
  138. ),
  139. );
  140. }
  141. /// 内容
  142. Widget _buildContent() {
  143. return Obx(() {
  144. // 选择了默认键盘,显示聊天列表
  145. if (controller.isDefaultKeyboard.value) {
  146. return _buildChatList();
  147. } else {
  148. // 未选择,显示引导动画
  149. return _buildGuideAnimation();
  150. }
  151. });
  152. }
  153. /// 引导动画
  154. Widget _buildGuideAnimation() {
  155. Widget animationWidget;
  156. if (PlatformUtil.isIOS) {
  157. animationWidget = Lottie.asset(
  158. Assets.anim.animKeyboardFloatingWindowChooseKeyboardIos,
  159. repeat: true,
  160. );
  161. } else if (PlatformUtil.isAndroid) {
  162. animationWidget = Lottie.asset(
  163. Assets.anim.animKeyboardFloatingWindowChooseKeyboardAndroid,
  164. repeat: true,
  165. );
  166. } else {
  167. animationWidget = SizedBox.shrink();
  168. }
  169. return Container(child: animationWidget);
  170. }
  171. /// 聊天列表
  172. Widget _buildChatList() {
  173. return Obx(() {
  174. return ListView.builder(
  175. controller: controller.scrollController,
  176. itemCount: controller.msgList.length,
  177. itemBuilder: (BuildContext context, int index) {
  178. KeyboardGuideMsg msg = controller.msgList[index];
  179. return _buildMsgItem(msg, index);
  180. },
  181. );
  182. });
  183. }
  184. /// 构建底部输入框
  185. Widget _buildBottomInput() {
  186. return Center(
  187. child: Column(
  188. children: [
  189. Container(
  190. color: ColorName.msgInputBar,
  191. padding: const EdgeInsets.symmetric(
  192. vertical: 11.0,
  193. horizontal: 12.0,
  194. ),
  195. child: Row(
  196. mainAxisAlignment: MainAxisAlignment.start,
  197. children: [
  198. Expanded(
  199. flex: 1,
  200. // 输入框的圆角边框
  201. child: Container(
  202. decoration: BoxDecoration(
  203. color: ColorName.white,
  204. borderRadius: BorderRadius.circular(10.0),
  205. ),
  206. child: Obx(() {
  207. return TextField(
  208. // 是否可用,选择了默认键盘时,才可用
  209. enabled:
  210. controller.isIOS.value
  211. ? true
  212. : controller.isDefaultKeyboard.value,
  213. // 是否自动获取焦点
  214. autofocus: false,
  215. style: TextStyle(
  216. color: ColorName.black80,
  217. fontSize: 14.0,
  218. fontWeight: FontWeight.w500,
  219. ),
  220. // 设置光标颜色
  221. cursorColor: ColorName.inputCursor,
  222. // 光标宽度
  223. cursorWidth: 2.0,
  224. // 光标圆角
  225. cursorRadius: Radius.circular(2),
  226. // 设置按钮显示为发送
  227. textInputAction: TextInputAction.send,
  228. // 用户点击软键盘的发送按钮时,触发回调
  229. onSubmitted: (value) {
  230. var msg = controller.editingController.text;
  231. controller.sendMsg(msg);
  232. // 保持输入框焦点获取,不降下键盘
  233. controller.requestInputFocus();
  234. },
  235. // 输入框焦点
  236. focusNode: controller.inputFocusNode,
  237. // 点击外部区域,关闭软键盘
  238. onTapUpOutside: (event) {
  239. // if (PlatformUtil.isIOS) {
  240. // controller.clearInputFocus();
  241. // }
  242. },
  243. onTap: () {
  244. if (PlatformUtil.isIOS) {
  245. controller.clearInputFocus();
  246. }
  247. },
  248. // 输入框控制器
  249. controller: controller.editingController,
  250. decoration: InputDecoration(
  251. // 提示文字
  252. hintText: StringName.keyboardGuideInputHint,
  253. hintStyle: TextStyle(
  254. fontSize: 14.0,
  255. fontWeight: FontWeight.w400,
  256. color: ColorName.black40,
  257. ),
  258. // 去掉默认的边框
  259. border: InputBorder.none,
  260. // 设置输入框的内边距
  261. contentPadding: EdgeInsets.symmetric(
  262. horizontal: 9.0,
  263. vertical: 13.0,
  264. ),
  265. ),
  266. );
  267. }),
  268. ),
  269. ),
  270. ],
  271. ),
  272. ),
  273. ],
  274. ),
  275. );
  276. }
  277. /// 构建聊天气泡
  278. Widget _buildMsgBubble(KeyboardGuideMsg msg) {
  279. // 设置气泡的外边距,让气泡不易过长
  280. double marginValue = 35.0;
  281. EdgeInsets marginEdgeInsets;
  282. if (msg.isMe) {
  283. marginEdgeInsets = EdgeInsets.only(left: marginValue);
  284. } else {
  285. marginEdgeInsets = EdgeInsets.only(right: marginValue);
  286. }
  287. // 圆角大小
  288. double radiusSize = 14.0;
  289. // 背景圆角
  290. BorderRadius bgBorderRadius;
  291. if (msg.isMe) {
  292. bgBorderRadius = BorderRadius.only(
  293. topLeft: Radius.circular(radiusSize),
  294. topRight: Radius.circular(0),
  295. bottomLeft: Radius.circular(radiusSize),
  296. bottomRight: Radius.circular(radiusSize),
  297. );
  298. } else {
  299. bgBorderRadius = BorderRadius.only(
  300. topLeft: Radius.circular(0),
  301. topRight: Radius.circular(radiusSize),
  302. bottomLeft: Radius.circular(radiusSize),
  303. bottomRight: Radius.circular(0),
  304. );
  305. }
  306. // Flexible,文本超过一行时,自动换行,并且不超过最大宽度,不超过一行时,则自动包裹内容
  307. return Flexible(
  308. child: Container(
  309. padding: EdgeInsets.symmetric(vertical: 12.0, horizontal: 10.0),
  310. margin: marginEdgeInsets,
  311. decoration: BoxDecoration(
  312. color: msg.isMe ? ColorName.msgBubbleMe : ColorName.msgBubbleTa,
  313. borderRadius: bgBorderRadius,
  314. ),
  315. child: GestureDetector(
  316. onTap: () {
  317. // 复制内容到剪切板
  318. if (msg.type == KeyboardGuideMsgType.copy.type) {
  319. ClipboardUtil.copyToClipboard(msg.content);
  320. ToastUtil.show(StringName.copySuccess);
  321. } else if (msg.type == KeyboardGuideMsgType.intimacySetting.type) {
  322. // 跳转到亲密度设置页
  323. IntimacyScalePage.start();
  324. }
  325. },
  326. child: Row(
  327. // 宽高包裹内容
  328. mainAxisSize: MainAxisSize.min,
  329. // 图标和文本,垂直居中
  330. crossAxisAlignment: CrossAxisAlignment.center,
  331. children: [
  332. Flexible(
  333. // 消息文本
  334. child: Text(
  335. msg.content,
  336. style: TextStyle(
  337. fontSize: 14.0,
  338. color: ColorName.black80,
  339. fontWeight: FontWeight.w500,
  340. height: 1.5,
  341. ),
  342. softWrap: true,
  343. ),
  344. ),
  345. // 只有对方发送的,才有操作按钮
  346. if (!msg.isMe)
  347. Visibility(
  348. visible: msg.type != KeyboardGuideMsgType.normal.type,
  349. child: Padding(
  350. padding: EdgeInsets.only(left: 8.0),
  351. child: _buildMsgActionBtn(msg),
  352. ),
  353. ),
  354. ],
  355. ),
  356. ),
  357. ),
  358. );
  359. }
  360. /// 消息操作按钮
  361. Widget _buildMsgActionBtn(KeyboardGuideMsg msg) {
  362. if (msg.type == KeyboardGuideMsgType.copy.type) {
  363. return Assets.images.iconCopy.image(width: 18.w, height: 18.w);
  364. } else if (msg.type == KeyboardGuideMsgType.intimacySetting.type) {
  365. return Assets.images.iconSetting.image(width: 18.w, height: 18.w);
  366. } else {
  367. return SizedBox.shrink();
  368. }
  369. }
  370. /// 构建聊天消息列表项
  371. Widget _buildMsgItem(KeyboardGuideMsg msg, int index) {
  372. return Obx(() {
  373. Widget content;
  374. // 自己发的
  375. if (msg.isMe) {
  376. content = Row(
  377. // 如果是自己发的,则在右边
  378. mainAxisAlignment: MainAxisAlignment.end,
  379. // 顶部对齐
  380. crossAxisAlignment: CrossAxisAlignment.start,
  381. children: [
  382. // 聊天气泡
  383. _buildMsgBubble(msg),
  384. SizedBox(width: 9.w),
  385. // 头像
  386. _buildAvatar(msg),
  387. ],
  388. );
  389. } else {
  390. // 对方发的
  391. content = Row(
  392. // 如果是自己发的,则在右边
  393. mainAxisAlignment: MainAxisAlignment.start,
  394. // 顶部对齐
  395. crossAxisAlignment: CrossAxisAlignment.start,
  396. children: [
  397. // 头像
  398. _buildAvatar(msg),
  399. SizedBox(width: 9.w),
  400. // 聊天气泡
  401. _buildMsgBubble(msg),
  402. ],
  403. );
  404. }
  405. bool isTargetGuildMsg = controller.guideMsgIndex.value == index;
  406. return Container(
  407. margin: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.0.h),
  408. child:
  409. isTargetGuildMsg
  410. ? Container(key: controller.guideMsgGlobalKey, child: content)
  411. : content,
  412. );
  413. });
  414. }
  415. /// 构建头像
  416. Widget _buildAvatar(KeyboardGuideMsg msg) {
  417. double avatarSize = 36.0;
  418. return CircleAvatar(
  419. radius: 20,
  420. child:
  421. msg.isMe
  422. ? Assets.images.iconDefaultAvatar.image(
  423. height: avatarSize,
  424. width: avatarSize,
  425. )
  426. : Assets.images.iconTaAvatar.image(
  427. height: avatarSize,
  428. width: avatarSize,
  429. ),
  430. );
  431. }
  432. }