keyboard_guide_page.dart 12 KB

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