keyboard_guide_page.dart 11 KB

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