keyboard_guide_page.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 '../../resource/assets.gen.dart';
  15. import '../../resource/colors.gen.dart';
  16. import '../../resource/string.gen.dart';
  17. import '../../utils/clipboard_util.dart';
  18. import '../../utils/url_launcher_util.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 && KeyboardGuideRecordUtil.isFirstShowKeyboardGuide()) {
  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. 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. // 输入框控制器
  244. controller: controller.editingController,
  245. decoration: InputDecoration(
  246. // 提示文字
  247. hintText: StringName.keyboardGuideInputHint,
  248. hintStyle: TextStyle(
  249. fontSize: 14.0,
  250. fontWeight: FontWeight.w400,
  251. color: ColorName.black40,
  252. ),
  253. // 去掉默认的边框
  254. border: InputBorder.none,
  255. // 设置输入框的内边距
  256. contentPadding: EdgeInsets.symmetric(
  257. horizontal: 9.0,
  258. vertical: 13.0,
  259. ),
  260. ),
  261. );
  262. }),
  263. ),
  264. ),
  265. ],
  266. ),
  267. ),
  268. ],
  269. ),
  270. );
  271. }
  272. /// 构建聊天气泡
  273. Widget _buildMsgBubble(KeyboardGuideMsg msg) {
  274. // 设置气泡的外边距,让气泡不易过长
  275. double marginValue = 35.0;
  276. EdgeInsets marginEdgeInsets;
  277. if (msg.isMe) {
  278. marginEdgeInsets = EdgeInsets.only(left: marginValue);
  279. } else {
  280. marginEdgeInsets = EdgeInsets.only(right: marginValue);
  281. }
  282. // 圆角大小
  283. double radiusSize = 14.0;
  284. // 背景圆角
  285. BorderRadius bgBorderRadius;
  286. if (msg.isMe) {
  287. bgBorderRadius = BorderRadius.only(
  288. topLeft: Radius.circular(radiusSize),
  289. topRight: Radius.circular(0),
  290. bottomLeft: Radius.circular(radiusSize),
  291. bottomRight: Radius.circular(radiusSize),
  292. );
  293. } else {
  294. bgBorderRadius = BorderRadius.only(
  295. topLeft: Radius.circular(0),
  296. topRight: Radius.circular(radiusSize),
  297. bottomLeft: Radius.circular(radiusSize),
  298. bottomRight: Radius.circular(0),
  299. );
  300. }
  301. // Flexible,文本超过一行时,自动换行,并且不超过最大宽度,不超过一行时,则自动包裹内容
  302. return Flexible(
  303. child: Container(
  304. padding: EdgeInsets.symmetric(vertical: 12.0, horizontal: 10.0),
  305. margin: marginEdgeInsets,
  306. decoration: BoxDecoration(
  307. color: msg.isMe ? ColorName.msgBubbleMe : ColorName.msgBubbleTa,
  308. borderRadius: bgBorderRadius,
  309. ),
  310. child: GestureDetector(
  311. onTap: () {
  312. // 复制内容到剪切板
  313. if (msg.type == KeyboardGuideMsgType.copy.type) {
  314. ClipboardUtil.copyToClipboard(msg.content);
  315. ToastUtil.show(StringName.copySuccess);
  316. } else if (msg.type == KeyboardGuideMsgType.intimacySetting.type) {
  317. // 跳转到亲密度设置页
  318. IntimacyScalePage.start();
  319. }
  320. },
  321. child: Row(
  322. // 宽高包裹内容
  323. mainAxisSize: MainAxisSize.min,
  324. // 图标和文本,垂直居中
  325. crossAxisAlignment: CrossAxisAlignment.center,
  326. children: [
  327. Flexible(
  328. // 消息文本
  329. child: Text(
  330. msg.content,
  331. style: TextStyle(
  332. fontSize: 14.0,
  333. color: ColorName.black80,
  334. fontWeight: FontWeight.w500,
  335. height: 1.5,
  336. ),
  337. softWrap: true,
  338. ),
  339. ),
  340. // 只有对方发送的,才有操作按钮
  341. if (!msg.isMe)
  342. Visibility(
  343. visible: msg.type != KeyboardGuideMsgType.normal.type,
  344. child: Padding(
  345. padding: EdgeInsets.only(left: 8.0),
  346. child: _buildMsgActionBtn(msg),
  347. ),
  348. ),
  349. ],
  350. ),
  351. ),
  352. ),
  353. );
  354. }
  355. /// 消息操作按钮
  356. Widget _buildMsgActionBtn(KeyboardGuideMsg msg) {
  357. if (msg.type == KeyboardGuideMsgType.copy.type) {
  358. return Assets.images.iconCopy.image(width: 18.w, height: 18.w);
  359. } else if (msg.type == KeyboardGuideMsgType.intimacySetting.type) {
  360. return Assets.images.iconSetting.image(width: 18.w, height: 18.w);
  361. } else {
  362. return SizedBox.shrink();
  363. }
  364. }
  365. /// 构建聊天消息列表项
  366. Widget _buildMsgItem(KeyboardGuideMsg msg, int index) {
  367. return Obx(() {
  368. Widget content;
  369. // 自己发的
  370. if (msg.isMe) {
  371. content = Row(
  372. // 如果是自己发的,则在右边
  373. mainAxisAlignment: MainAxisAlignment.end,
  374. // 顶部对齐
  375. crossAxisAlignment: CrossAxisAlignment.start,
  376. children: [
  377. // 聊天气泡
  378. _buildMsgBubble(msg),
  379. SizedBox(width: 9.w),
  380. // 头像
  381. _buildAvatar(msg),
  382. ],
  383. );
  384. } else {
  385. // 对方发的
  386. content = Row(
  387. // 如果是自己发的,则在右边
  388. mainAxisAlignment: MainAxisAlignment.start,
  389. // 顶部对齐
  390. crossAxisAlignment: CrossAxisAlignment.start,
  391. children: [
  392. // 头像
  393. _buildAvatar(msg),
  394. SizedBox(width: 9.w),
  395. // 聊天气泡
  396. _buildMsgBubble(msg),
  397. ],
  398. );
  399. }
  400. bool isTargetGuildMsg = controller.guideMsgIndex.value == index;
  401. return Container(
  402. margin: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.0.h),
  403. child:
  404. isTargetGuildMsg
  405. ? Container(key: controller.guideMsgGlobalKey, child: content)
  406. : content,
  407. );
  408. });
  409. }
  410. /// 构建头像
  411. Widget _buildAvatar(KeyboardGuideMsg msg) {
  412. double avatarSize = 36.0;
  413. return CircleAvatar(
  414. radius: 20,
  415. child:
  416. msg.isMe
  417. ? Assets.images.iconDefaultAvatar.image(
  418. height: avatarSize,
  419. width: avatarSize,
  420. )
  421. : Assets.images.iconTaAvatar.image(
  422. height: avatarSize,
  423. width: avatarSize,
  424. ),
  425. );
  426. }
  427. }