keyboard_guide_page.dart 12 KB

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