keyboard_guide_page.dart 13 KB

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