keyboard_guide_page.dart 11 KB

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