keyboard_guide_page.dart 15 KB

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