keyboard_guide_page.dart 12 KB

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