msg_page.dart 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. import 'dart:convert';
  2. import 'package:bbyyy/beans/message_bean_entity.dart';
  3. import 'package:bbyyy/beans/my_shop_bean_entity.dart';
  4. import 'package:bbyyy/beans/table_bean_entity.dart';
  5. import 'package:bbyyy/https/url.dart';
  6. import 'package:bbyyy/msgDB/my_msg_db.dart';
  7. import 'package:bbyyy/msgDB/sql_manager.dart';
  8. import 'package:bbyyy/my_tools/const.dart';
  9. import 'package:bbyyy/my_tools/event_bus.dart';
  10. import 'package:bbyyy/my_tools/my_colors.dart';
  11. import 'package:bbyyy/my_tools/my_cookie.dart';
  12. import 'package:bbyyy/my_tools/my_tools.dart';
  13. import 'package:bbyyy/my_tools/my_views.dart';
  14. import 'package:bbyyy/paegs/chat_page/chat_page.dart';
  15. import 'package:flutter/cupertino.dart';
  16. import 'package:flutter/material.dart';
  17. import 'package:flutter_slidable/flutter_slidable.dart';
  18. import 'package:flutter_svg/svg.dart';
  19. import 'package:pull_to_refresh/pull_to_refresh.dart';
  20. class MsgPage extends StatefulWidget {
  21. @override
  22. _MsgPageState createState() => _MsgPageState();
  23. }
  24. class _MsgPageState extends State<MsgPage> with AutomaticKeepAliveClientMixin {
  25. var types = ['商品', '对象'];
  26. List<DropdownMenuItem<String>> sortItems = [];
  27. int typeIndex = 0;
  28. bool showInput = false;
  29. TextEditingController _description = TextEditingController();
  30. FocusNode _descriptionFocus = FocusNode();
  31. RefreshController controller = RefreshController(initialRefresh: true);
  32. @override
  33. void initState() {
  34. // TODO: implement initState
  35. super.initState();
  36. types.forEach((element) {
  37. sortItems.add(DropdownMenuItem(
  38. child: Text(
  39. element,
  40. style: TextStyle(color: MyColors.c666666, fontSize: 14),
  41. ),
  42. value: element,
  43. ));
  44. });
  45. _descriptionFocus.addListener(() {
  46. if (_descriptionFocus.hasFocus) {
  47. } else {
  48. if (_description.text.isEmpty) {
  49. setState(() {
  50. showInput = false;
  51. });
  52. }
  53. }
  54. });
  55. EventBus().on('hasNewMsgInMsgPage', (arg) {
  56. queryChatRoom();
  57. });
  58. }
  59. @override
  60. void dispose() {
  61. // TODO: implement dispose
  62. super.dispose();
  63. EventBus().off('hasNewMsgInMsgPage');
  64. }
  65. @override
  66. Widget build(BuildContext context) {
  67. return GestureDetector(
  68. onTap: () {
  69. MyTools().hideKeyboard(context);
  70. },
  71. behavior: HitTestBehavior.translucent,
  72. child: Scaffold(
  73. body: Stack(
  74. children: [
  75. Container(
  76. decoration: BoxDecoration(
  77. gradient: LinearGradient(
  78. colors: MyColors.lg,
  79. ),
  80. ),
  81. height: 152,
  82. // child: SafeArea(
  83. // child: Container(
  84. // decoration: BoxDecoration(
  85. // color: Colors.white,
  86. // borderRadius: BorderRadius.all(
  87. // Radius.circular(17),
  88. // ),
  89. // ),
  90. // height: 34,
  91. // child: Row(
  92. // children: [
  93. // DropdownButtonHideUnderline(
  94. // child: DropdownButton(
  95. // items: sortItems,
  96. // onChanged: (v) {
  97. // setState(() {
  98. // typeIndex = types.indexOf(v);
  99. // });
  100. // },
  101. // value: types[typeIndex],
  102. // ),
  103. // ),
  104. // Container(
  105. // height: 13,
  106. // width: 1,
  107. // decoration: BoxDecoration(
  108. // borderRadius: BorderRadius.all(
  109. // Radius.circular(0.5),
  110. // ),
  111. // color: MyColors.c979797),
  112. // ),
  113. // Expanded(
  114. // child: showInput
  115. // ? TextField(
  116. // controller: _description,
  117. // focusNode: _descriptionFocus,
  118. // cursorColor: MyColors.cFF4233,
  119. // cursorWidth: 1.0,
  120. // decoration: InputDecoration(
  121. // border: InputBorder.none,
  122. // disabledBorder: InputBorder.none,
  123. // enabledBorder: InputBorder.none,
  124. // focusedBorder: InputBorder.none,
  125. // isDense: true,
  126. // hintText: '请输入描述',
  127. // hintStyle: TextStyle(
  128. // color: MyColors.c999999, fontSize: 14),
  129. // contentPadding: const EdgeInsets.fromLTRB(
  130. // 14, 4.5, 8, 4.5),
  131. // ),
  132. // maxLines: 1,
  133. // style: TextStyle(
  134. // color: MyColors.c333333,
  135. // fontSize: 14,
  136. // height: 1.3,
  137. // letterSpacing: 0.2),
  138. // keyboardType: TextInputType.text,
  139. // onChanged: (t) {},
  140. // )
  141. // : GestureDetector(
  142. // onTap: () {
  143. // setState(() {
  144. // showInput = true;
  145. // });
  146. // Timer(Duration(milliseconds: 100), () {
  147. // FocusScope.of(context)
  148. // .requestFocus(_descriptionFocus);
  149. // });
  150. // },
  151. // behavior: HitTestBehavior.translucent,
  152. // child: Row(
  153. // children: [
  154. // Container(
  155. // child:
  156. // SvgPicture.asset('images/svg/搜索.svg'),
  157. // margin: EdgeInsets.only(right: 5),
  158. // ),
  159. // Text(
  160. // '请输入',
  161. // style: TextStyle(
  162. // color: MyColors.cBFBFBF,
  163. // fontSize: 14),
  164. // )
  165. // ],
  166. // mainAxisAlignment: MainAxisAlignment.center,
  167. // ),
  168. // ),
  169. // ),
  170. // Container(
  171. // decoration: BoxDecoration(
  172. // color: MyColors.cFF4233,
  173. // borderRadius: BorderRadius.all(
  174. // Radius.circular(17),
  175. // ),
  176. // ),
  177. // height: 34,
  178. // width: 76,
  179. // child: Text(
  180. // '搜索',
  181. // style: TextStyle(color: Colors.white, fontSize: 14),
  182. // ),
  183. // alignment: Alignment.center,
  184. // padding: EdgeInsets.only(bottom: 2),
  185. // )
  186. // ],
  187. // ),
  188. // margin: EdgeInsets.only(
  189. // left: 15,
  190. // right: 15,
  191. // top: 72 - MediaQuery.of(context).padding.top),
  192. // padding: EdgeInsets.only(left: 16),
  193. // ),
  194. // ),
  195. child: SafeArea(
  196. child: Stack(
  197. children: [
  198. Container(
  199. height: 50,
  200. child: Text(
  201. '消息',
  202. style: TextStyle(color: Colors.white, fontSize: 16),
  203. ),
  204. alignment: Alignment.center,
  205. ),
  206. ],
  207. alignment: Alignment.topCenter,
  208. ),
  209. ),
  210. alignment: Alignment.topCenter,
  211. ),
  212. Container(
  213. decoration: BoxDecoration(
  214. color: Colors.white,
  215. borderRadius: BorderRadius.only(
  216. topLeft: Radius.circular(16),
  217. topRight: Radius.circular(16),
  218. ),
  219. ),
  220. margin:
  221. EdgeInsets.only(top: MediaQuery.of(context).padding.top + 50),
  222. child: SmartRefresher(
  223. onRefresh: onRefresh,
  224. controller: controller,
  225. child: lastMsg.length == 0
  226. ? noData()
  227. : ListView.builder(
  228. itemBuilder: (c, index) {
  229. // if (index == 0) {
  230. // return msgItems(null);
  231. // } else {
  232. return msgItems(lastMsg[index]);
  233. // }
  234. },
  235. itemCount: lastMsg.length,
  236. padding: EdgeInsets.only(bottom: 65),
  237. ),
  238. ),
  239. )
  240. ],
  241. ),
  242. ),
  243. );
  244. }
  245. @override
  246. // TODO: implement wantKeepAlive
  247. bool get wantKeepAlive => true;
  248. Widget msgItems(MessageBeanContent data) {
  249. // if (data == null) {
  250. // return GestureDetector(
  251. // onTap: () {
  252. // MyTools().toPage(context, PlatformAssistantPage(), (then) {});
  253. // },
  254. // behavior: HitTestBehavior.translucent,
  255. // child: Container(
  256. // height: 73,
  257. // child: Column(
  258. // children: [
  259. // Expanded(
  260. // child: Row(
  261. // children: [
  262. // Container(
  263. // margin: EdgeInsets.only(left: 14, right: 14),
  264. // child: SvgPicture.asset(
  265. // 'images/svg/平台助手.svg',
  266. // height: 45,
  267. // width: 45,
  268. // ),
  269. // ),
  270. // Expanded(
  271. // child: Container(
  272. // height: 45,
  273. // padding: EdgeInsets.only(right: 14),
  274. // child: Column(
  275. // children: [
  276. // Text(
  277. // '平台助手',
  278. // style: TextStyle(
  279. // color: MyColors.c333333, fontSize: 15),
  280. // ),
  281. // Row(
  282. // children: [
  283. // Expanded(
  284. // child: Text(
  285. // '你有一条新的申请消息',
  286. // style: TextStyle(
  287. // color: MyColors.c888888, fontSize: 12),
  288. // ),
  289. // ),
  290. // Container(
  291. // decoration: BoxDecoration(
  292. // color: MyColors.cFF4233,
  293. // borderRadius:
  294. // BorderRadius.all(Radius.circular(4))),
  295. // height: 8,
  296. // width: 8,
  297. // padding: EdgeInsets.only(left: 5, right: 5),
  298. // alignment: Alignment.center,
  299. // )
  300. // ],
  301. // )
  302. // ],
  303. // mainAxisAlignment: MainAxisAlignment.spaceBetween,
  304. // crossAxisAlignment: CrossAxisAlignment.start,
  305. // ),
  306. // ),
  307. // )
  308. // ],
  309. // ),
  310. // ),
  311. // Container(
  312. // margin: EdgeInsets.only(left: 73, right: 14),
  313. // height: 0.5,
  314. // color: MyColors.cE7E7E7,
  315. // )
  316. // ],
  317. // mainAxisAlignment: MainAxisAlignment.spaceBetween,
  318. // ),
  319. // ),
  320. // );
  321. // } else {
  322. String pic;
  323. String name;
  324. int uid;
  325. MyShopBeanDataData chatWith = MyShopBeanDataData();
  326. if (data.senderUid == MyCookie().getUID()) {
  327. pic = data.receiverPic;
  328. name = data.receiverName;
  329. uid = data.receiverUid;
  330. chatWith.userName = data.receiverName;
  331. chatWith.userUid = data.receiverUid;
  332. chatWith.userPic = data.receiverPic;
  333. } else {
  334. pic = data.senderPic;
  335. name = data.senderName;
  336. uid = data.senderUid;
  337. chatWith.userName = data.senderName;
  338. chatWith.userUid = data.senderUid;
  339. chatWith.userPic = data.senderPic;
  340. }
  341. return Slidable(
  342. actionPane: SlidableDrawerActionPane(),
  343. actionExtentRatio: 0.25,
  344. child: GestureDetector(
  345. onTap: () {
  346. MyTools().toPage(context, ChatPage(chatWith, null), (then) {
  347. var now = new DateTime.now();
  348. MyCookie().prefs.setString(
  349. '${MyCookie().getUID()}_${chatWith.userUid}',
  350. now.toString().substring(0, 19));
  351. queryChatRoom();
  352. });
  353. },
  354. behavior: HitTestBehavior.translucent,
  355. child: Container(
  356. height: 73,
  357. child: Column(
  358. children: [
  359. Expanded(
  360. child: Row(
  361. children: [
  362. Container(
  363. margin: EdgeInsets.only(left: 14, right: 14),
  364. child: ClipRRect(
  365. child: uid == 0
  366. ? Image.asset(
  367. 'images/app_logo.png',
  368. height: 45,
  369. width: 45,
  370. )
  371. : MyViews().netImg(imgURL(pic), 45, 45,
  372. placeholder: 'images/svg/占位图.svg'),
  373. borderRadius: BorderRadius.all(Radius.circular(4)),
  374. ),
  375. ),
  376. Expanded(
  377. child: Container(
  378. height: 45,
  379. padding: EdgeInsets.only(right: 14),
  380. child: Column(
  381. children: [
  382. Row(
  383. children: [
  384. Expanded(
  385. child: Text(
  386. uid == 0 ? '客服' : name,
  387. style: TextStyle(
  388. color: MyColors.c333333, fontSize: 15),
  389. )),
  390. Text(
  391. data.sentAt,
  392. style: TextStyle(
  393. color: MyColors.c888888, fontSize: 12),
  394. ),
  395. ],
  396. ),
  397. Row(
  398. children: [
  399. Expanded(
  400. child: Text(
  401. data.type != chatMsgTypeText
  402. ? '[${data.type}]'
  403. : data.content,
  404. style: TextStyle(
  405. color: MyColors.c888888, fontSize: 12),
  406. maxLines: 1,
  407. overflow: TextOverflow.ellipsis,
  408. softWrap: true,
  409. ),
  410. ),
  411. Visibility(
  412. visible: showRedPoint(
  413. chatWith.userUid, data.sentAt),
  414. child: Container(
  415. decoration: BoxDecoration(
  416. color: MyColors.cFF4233,
  417. borderRadius: BorderRadius.all(
  418. Radius.circular(4))),
  419. height: 8,
  420. width: 8,
  421. padding: EdgeInsets.only(left: 5, right: 5),
  422. alignment: Alignment.center,
  423. ),
  424. )
  425. ],
  426. )
  427. ],
  428. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  429. ),
  430. ),
  431. )
  432. ],
  433. ),
  434. ),
  435. Container(
  436. margin: EdgeInsets.only(left: 73, right: 14),
  437. height: 0.5,
  438. color: MyColors.cE7E7E7,
  439. )
  440. ],
  441. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  442. ),
  443. ),
  444. ),
  445. secondaryActions: <Widget>[
  446. SlideAction(
  447. child: Container(
  448. child: Container(
  449. decoration: BoxDecoration(
  450. borderRadius: BorderRadius.circular(30),
  451. color: MyColors.cFF4233,
  452. ),
  453. height: 40,width: 40,
  454. child: SvgPicture.asset('images/svg/del.svg',height: 25,width: 25,),
  455. alignment: Alignment.center,
  456. ),
  457. ),
  458. onTap: (){
  459. showSimpleDialog(
  460. '将删除与(${name.isEmpty ? '客服' : name})的聊天记录,确认删除?', context,
  461. () async {
  462. Navigator.pop(context);
  463. MsgDB msgDB = MsgDB('table${MyCookie().getUID()}_$uid');
  464. await msgDB.deleteTableData();
  465. chatRoom.remove(uid);
  466. lastMsg.remove(data);
  467. setState(() {});
  468. queryChatRoom();
  469. });
  470. },
  471. )
  472. ],
  473. );
  474. // }
  475. }
  476. Map chatRoom = Map();
  477. List<MessageBeanContent> lastMsg = [];
  478. queryChatRoom() async {
  479. List<Map<String, dynamic>> map = await SqlManager.allTables();
  480. map.forEach((element) async {
  481. TableBeanEntity table =
  482. TableBeanEntity().fromJson(json.decode(json.encode(element)));
  483. if (table.name.contains('table${MyCookie().getUID()}_')) {
  484. MsgDB msgDB = MsgDB(table.name);
  485. List<Map<String, dynamic>> maps = await msgDB.queryTableData(1, 1);
  486. maps.forEach((element) {
  487. print(json.encode(element));
  488. MessageBeanContent msg =
  489. MessageBeanContent().fromJson(json.decode(json.encode(element)));
  490. if (msg.senderUid == MyCookie().getUID()) {
  491. print(msg.receiverUid);
  492. chatRoom[msg.receiverUid] = msg;
  493. } else {
  494. print(msg.senderUid);
  495. chatRoom[msg.senderUid] = msg;
  496. }
  497. lastMsg.clear();
  498. chatRoom.forEach((key, value) {
  499. lastMsg.add(value);
  500. });
  501. lastMsg.sort((left, right) => right.sentAt.compareTo(left.sentAt));
  502. setState(() {});
  503. });
  504. }
  505. });
  506. endRe(controller);
  507. }
  508. void onRefresh() {
  509. queryChatRoom();
  510. }
  511. showRedPoint(int userUid, String sentAt) {
  512. try {
  513. String lastTime =
  514. MyCookie().prefs.getString('${MyCookie().getUID()}_$userUid');
  515. print('--------------');
  516. print(lastTime);
  517. print(sentAt);
  518. print('--------------');
  519. return lastTime.compareTo(sentAt) == -1;
  520. } catch (e) {
  521. return true;
  522. }
  523. }
  524. }