keyboard_guide_page.dart 10 KB

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