keyboard_guide_page.dart 12 KB

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