keyboard_guide_page.dart 15 KB

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