keyboard_guide_page.dart 12 KB

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