Hibok
您不能選擇超過 %s 個話題 話題必須以字母或數字為開頭,可包含連接號 ('-') 且最長為 35 個字
 
 
 
 
 
 

273 行
9.4 KiB

  1. import 'package:chat/data/UserData.dart';
  2. import 'package:chat/data/chat_data_mgr.dart';
  3. import 'package:chat/data/constants.dart';
  4. import 'package:chat/generated/i18n.dart';
  5. import 'package:chat/home/last_chat_item.dart';
  6. import 'package:chat/models/last_message_model.dart';
  7. import 'package:chat/r.dart';
  8. import 'package:chat/utils/CustomUI.dart';
  9. import 'package:chat/utils/MessageMgr.dart';
  10. import 'package:chat/utils/app_navigator.dart';
  11. import 'package:chat/utils/net_state_widget.dart';
  12. import 'package:chat/utils/screen.dart';
  13. import 'package:chat/utils/sql_util.dart';
  14. import 'package:flutter/cupertino.dart';
  15. import 'package:flutter/material.dart';
  16. import 'package:permission_handler/permission_handler.dart';
  17. class LastChatPage extends StatefulWidget {
  18. final bool needRobot;
  19. final int enterType; // 0默认 1图片 2转发消息
  20. final dynamic enterContent;
  21. LastChatPage({this.needRobot = true, this.enterType = 0, this.enterContent});
  22. @override
  23. _LastChatPageState createState() => _LastChatPageState();
  24. }
  25. class _LastChatPageState extends State<LastChatPage> {
  26. @override
  27. void initState() {
  28. super.initState();
  29. MessageMgr().on(MessageMgr.REFRESH_PUSH_PERMISSION, requestPushPermission);
  30. MessageMgr().on('Update LastMsg', updateList);
  31. }
  32. @override
  33. void dispose() {
  34. MessageMgr().off(MessageMgr.REFRESH_PUSH_PERMISSION, requestPushPermission);
  35. MessageMgr().off('Update LastMsg', updateList);
  36. super.dispose();
  37. }
  38. requestPushPermission(args) async {
  39. print('请求推送权限');
  40. final PermissionStatus status = await PermissionHandler()
  41. .checkPermissionStatus(PermissionGroup.notification);
  42. print('请求推送权限--status:$status');
  43. if (status == PermissionStatus.denied) {
  44. setState(() {
  45. UserData().needRequestPushPermission = UserData().privatyMsgPushSwitch;
  46. });
  47. }
  48. if (status == PermissionStatus.granted) {
  49. UserData().needRequestPushPermission = false;
  50. if (mounted) {
  51. setState(() {});
  52. }
  53. }
  54. }
  55. updateList(args) {
  56. if (mounted) {
  57. setState(() {});
  58. }
  59. }
  60. @override
  61. Widget build(BuildContext context) {
  62. List<LastMessageModel> lastMsgList = ChatDataMgr().lastMsgProvider.lastMsgList;
  63. print('lastMsgList ${lastMsgList.length}');
  64. return Column(
  65. children: <Widget>[
  66. UserData().needRequestPushPermission
  67. ? InkWell(
  68. onTap: () {
  69. CustomUI.buildOneConfirm(
  70. context,
  71. I18n.of(context).notice_open,
  72. I18n.of(context).determine, () async {
  73. Navigator.of(context).pop();
  74. PermissionHandler().openAppSettings();
  75. }, title: I18n.of(context).open_notice);
  76. },
  77. child: Container(
  78. height: 25,
  79. alignment: Alignment.center,
  80. width: double.infinity,
  81. child: Row(
  82. crossAxisAlignment: CrossAxisAlignment.center,
  83. mainAxisAlignment: MainAxisAlignment.center,
  84. children: <Widget>[
  85. Text(
  86. I18n.of(context).not_open,
  87. textScaleFactor: 1,
  88. style: TextStyle(
  89. fontSize: 12.5, color: const Color(0xffab8f60)),
  90. ),
  91. Icon(
  92. Icons.chevron_right,
  93. size: 18,
  94. color: const Color(0xffab8f60),
  95. )
  96. ],
  97. ),
  98. color: const Color(0xfffaefcf),
  99. ))
  100. : Container(),
  101. NetStateWidget(),
  102. widget.needRobot ? _translateRobot() : Container(),
  103. _companyServer(),
  104. lastMsgList.length == 0
  105. ? _emptyContent()
  106. : _chatRecordsList(lastMsgList)
  107. ],
  108. );
  109. }
  110. Widget _translateRobot() {
  111. return InkWell(
  112. onTap: () {
  113. AppNavigator.pushTranslateRobotPage(context);
  114. },
  115. child: Container(
  116. padding: const EdgeInsets.all(10.0),
  117. height: 68,
  118. child: Row(
  119. children: <Widget>[
  120. ClipRRect(
  121. child: Image.asset(R.assetsImagesRobot),
  122. borderRadius: BorderRadius.circular(5),
  123. ),
  124. SizedBox(width: 10),
  125. Column(
  126. crossAxisAlignment: CrossAxisAlignment.start,
  127. children: <Widget>[
  128. fixedText(I18n.of(context).translate_robot,
  129. fontSize: 15, fontWeight: FontWeight.w500),
  130. SizedBox(height: 5),
  131. Text(
  132. I18n.of(context).robot_hello,
  133. textScaleFactor: 1.0,
  134. style: AppStyles.DescStyle,
  135. ),
  136. ],
  137. )
  138. ],
  139. ),
  140. alignment: Alignment.center,
  141. decoration: BoxDecoration(
  142. color: Color(AppColors.ConversationItemBgColor),
  143. border: Border(bottom: Constants.GreyBorderSide)),
  144. ));
  145. }
  146. Widget _companyServer() {
  147. return InkWell(
  148. onTap: () {
  149. AppNavigator.pushCompanyServerPage(context);
  150. },
  151. child: Container(
  152. padding: const EdgeInsets.all(10.0),
  153. height: 68,
  154. child: Row(
  155. children: <Widget>[
  156. ClipRRect(
  157. child: Image.asset(R.assetsImagesServerIcon),
  158. borderRadius: BorderRadius.circular(5),
  159. ),
  160. SizedBox(width: 10),
  161. Column(
  162. crossAxisAlignment: CrossAxisAlignment.start,
  163. children: <Widget>[
  164. fixedText(I18n.of(context).feedback_assistant,
  165. fontSize: 15, fontWeight: FontWeight.w500),
  166. SizedBox(height: 5),
  167. Text(
  168. I18n.of(context).feedback_tips,
  169. textScaleFactor: 1.0,
  170. style: AppStyles.DescStyle,
  171. ),
  172. ],
  173. )
  174. ],
  175. ),
  176. alignment: Alignment.center,
  177. decoration: BoxDecoration(
  178. color: Color(AppColors.ConversationItemBgColor),
  179. border: Border(bottom: Constants.GreyBorderSide)),
  180. ));
  181. }
  182. Widget _emptyContent() {
  183. return Expanded(
  184. child: Center(
  185. child: Column(
  186. mainAxisAlignment: MainAxisAlignment.center,
  187. crossAxisAlignment: CrossAxisAlignment.center,
  188. children: <Widget>[
  189. Padding(
  190. padding: EdgeInsets.only(bottom: 9.5, right: 11),
  191. child: Icon(
  192. IconData(
  193. 0xe640,
  194. fontFamily: 'iconfont',
  195. ),
  196. color: Constants.LightBlueIconColor,
  197. size: 60,
  198. )),
  199. fixedText(I18n.of(context).no_new,
  200. color: Constants.OpacityBlueTextColor, fontSize: 14),
  201. ],
  202. )),
  203. );
  204. }
  205. Widget _chatRecordsList(lastMsgList) {
  206. return Expanded(
  207. child: Container(
  208. alignment: Alignment.topCenter,
  209. child: ListView.builder(
  210. shrinkWrap: true,
  211. itemBuilder: (BuildContext context, int index) {
  212. LastMessageModel msg = lastMsgList[index];
  213. if (msg.sessionId == 10000) {
  214. return Container();
  215. }
  216. return Dismissible(
  217. key: Key(msg.sessionId.toString()),
  218. child: LastChatItem(
  219. Key(msg.sessionId.toString()),
  220. msg,
  221. enterType: widget.enterType,
  222. enterContent: widget.enterContent,
  223. ),
  224. direction: DismissDirection.endToStart,
  225. background: Container(
  226. color: Colors.red,
  227. child: Row(
  228. mainAxisAlignment: MainAxisAlignment.end,
  229. children: <Widget>[
  230. fixedText(I18n.of(context).delete,
  231. color: Colors.white),
  232. Icon(
  233. Icons.delete,
  234. color: Colors.white,
  235. )
  236. ],
  237. ),
  238. ),
  239. confirmDismiss: (direction) async {
  240. var _confirmContent = I18n.of(context).confirm_delete;
  241. bool isDismiss = await CustomUI.showIosDialog(
  242. context, _confirmContent, () {
  243. lastMsgList.removeAt(index);
  244. SqlUtil.userInfoTable.deleteUser(msg.friendId);
  245. ChatDataMgr().deleteMsg(msg.sessionId, false);
  246. setState(() {});
  247. Navigator.of(context).pop(true);
  248. }, () {
  249. Navigator.of(context).pop(false);
  250. });
  251. return isDismiss;
  252. });
  253. },
  254. itemCount: lastMsgList.length,
  255. )));
  256. }
  257. }