import 'dart:convert'; import 'package:bbyyy/beans/message_bean_entity.dart'; import 'package:bbyyy/beans/my_shop_bean_entity.dart'; import 'package:bbyyy/beans/table_bean_entity.dart'; import 'package:bbyyy/https/url.dart'; import 'package:bbyyy/msgDB/my_msg_db.dart'; import 'package:bbyyy/msgDB/sql_manager.dart'; import 'package:bbyyy/my_tools/const.dart'; import 'package:bbyyy/my_tools/event_bus.dart'; import 'package:bbyyy/my_tools/my_colors.dart'; import 'package:bbyyy/my_tools/my_cookie.dart'; import 'package:bbyyy/my_tools/my_tools.dart'; import 'package:bbyyy/my_tools/my_views.dart'; import 'package:bbyyy/paegs/chat_page/chat_page.dart'; import 'package:bbyyy/paegs/system_information_page/system_information_page.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_slidable/flutter_slidable.dart'; import 'package:flutter_svg/svg.dart'; import 'package:pull_to_refresh/pull_to_refresh.dart'; class MsgPage extends StatefulWidget { @override _MsgPageState createState() => _MsgPageState(); } class _MsgPageState extends State with AutomaticKeepAliveClientMixin { var types = ['商品', '对象']; List> sortItems = []; int typeIndex = 0; bool showInput = false; TextEditingController _description = TextEditingController(); FocusNode _descriptionFocus = FocusNode(); RefreshController controller = RefreshController(initialRefresh: true); bool haveSysMsg = false; var sysMsgRead = false; String systemInformation; @override void initState() { // TODO: implement initState super.initState(); types.forEach((element) { sortItems.add(DropdownMenuItem( child: Text( element, style: TextStyle(color: MyColors.c666666, fontSize: 14), ), value: element, )); }); _descriptionFocus.addListener(() { if (_descriptionFocus.hasFocus) { } else { if (_description.text.isEmpty) { setState(() { showInput = false; }); } } }); EventBus().on('hasNewMsgInMsgPage', (arg) { queryChatRoom(); }); EventBus().on('haveSysMsg', (arg) { haveSysMsg = true; setState(() {}); }); } @override void dispose() { // TODO: implement dispose super.dispose(); EventBus().off('hasNewMsgInMsgPage'); EventBus().off('haveSysMsg'); } @override Widget build(BuildContext context) { return GestureDetector( onTap: () { MyTools().hideKeyboard(context); }, behavior: HitTestBehavior.translucent, child: Scaffold( body: Stack( children: [ Container( decoration: BoxDecoration( gradient: LinearGradient( colors: MyColors.lg, ), ), height: 152, // child: SafeArea( // child: Container( // decoration: BoxDecoration( // color: Colors.white, // borderRadius: BorderRadius.all( // Radius.circular(17), // ), // ), // height: 34, // child: Row( // children: [ // DropdownButtonHideUnderline( // child: DropdownButton( // items: sortItems, // onChanged: (v) { // setState(() { // typeIndex = types.indexOf(v); // }); // }, // value: types[typeIndex], // ), // ), // Container( // height: 13, // width: 1, // decoration: BoxDecoration( // borderRadius: BorderRadius.all( // Radius.circular(0.5), // ), // color: MyColors.c979797), // ), // Expanded( // child: showInput // ? TextField( // controller: _description, // focusNode: _descriptionFocus, // cursorColor: MyColors.cFF4233, // cursorWidth: 1.0, // decoration: InputDecoration( // border: InputBorder.none, // disabledBorder: InputBorder.none, // enabledBorder: InputBorder.none, // focusedBorder: InputBorder.none, // isDense: true, // hintText: '请输入描述', // hintStyle: TextStyle( // color: MyColors.c999999, fontSize: 14), // contentPadding: const EdgeInsets.fromLTRB( // 14, 4.5, 8, 4.5), // ), // maxLines: 1, // style: TextStyle( // color: MyColors.c333333, // fontSize: 14, // height: 1.3, // letterSpacing: 0.2), // keyboardType: TextInputType.text, // onChanged: (t) {}, // ) // : GestureDetector( // onTap: () { // setState(() { // showInput = true; // }); // Timer(Duration(milliseconds: 100), () { // FocusScope.of(context) // .requestFocus(_descriptionFocus); // }); // }, // behavior: HitTestBehavior.translucent, // child: Row( // children: [ // Container( // child: // SvgPicture.asset('images/svg/搜索.svg'), // margin: EdgeInsets.only(right: 5), // ), // Text( // '请输入', // style: TextStyle( // color: MyColors.cBFBFBF, // fontSize: 14), // ) // ], // mainAxisAlignment: MainAxisAlignment.center, // ), // ), // ), // Container( // decoration: BoxDecoration( // color: MyColors.cFF4233, // borderRadius: BorderRadius.all( // Radius.circular(17), // ), // ), // height: 34, // width: 76, // child: Text( // '搜索', // style: TextStyle(color: Colors.white, fontSize: 14), // ), // alignment: Alignment.center, // padding: EdgeInsets.only(bottom: 2), // ) // ], // ), // margin: EdgeInsets.only( // left: 15, // right: 15, // top: 72 - MediaQuery.of(context).padding.top), // padding: EdgeInsets.only(left: 16), // ), // ), child: SafeArea( child: Stack( children: [ Container( height: 50, child: Text( '消息', style: TextStyle(color: Colors.white, fontSize: 16), ), alignment: Alignment.center, ), ], alignment: Alignment.topCenter, ), ), alignment: Alignment.topCenter, ), Container( decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.only( topLeft: Radius.circular(16), topRight: Radius.circular(16), ), ), margin: EdgeInsets.only(top: MediaQuery.of(context).padding.top + 50), child: SmartRefresher( onRefresh: onRefresh, controller: controller, child: lastMsg.length == 0 && !haveSysMsg ? noData() : ListView.builder( itemBuilder: (c, index) { // if (index == 0) { // return msgItems(null); // } else { return msgItems(lastMsg[index]); // } }, itemCount: lastMsg.length, padding: EdgeInsets.only(bottom: 65), ), ), ) ], ), ), ); } @override // TODO: implement wantKeepAlive bool get wantKeepAlive => true; Widget msgItems(MessageBeanContent data) { // if (data == null) { // return GestureDetector( // onTap: () { // MyTools().toPage(context, PlatformAssistantPage(), (then) {}); // }, // behavior: HitTestBehavior.translucent, // child: Container( // height: 73, // child: Column( // children: [ // Expanded( // child: Row( // children: [ // Container( // margin: EdgeInsets.only(left: 14, right: 14), // child: SvgPicture.asset( // 'images/svg/平台助手.svg', // height: 45, // width: 45, // ), // ), // Expanded( // child: Container( // height: 45, // padding: EdgeInsets.only(right: 14), // child: Column( // children: [ // Text( // '平台助手', // style: TextStyle( // color: MyColors.c333333, fontSize: 15), // ), // Row( // children: [ // Expanded( // child: Text( // '你有一条新的申请消息', // style: TextStyle( // color: MyColors.c888888, fontSize: 12), // ), // ), // Container( // decoration: BoxDecoration( // color: MyColors.cFF4233, // borderRadius: // BorderRadius.all(Radius.circular(4))), // height: 8, // width: 8, // padding: EdgeInsets.only(left: 5, right: 5), // alignment: Alignment.center, // ) // ], // ) // ], // mainAxisAlignment: MainAxisAlignment.spaceBetween, // crossAxisAlignment: CrossAxisAlignment.start, // ), // ), // ) // ], // ), // ), // Container( // margin: EdgeInsets.only(left: 73, right: 14), // height: 0.5, // color: MyColors.cE7E7E7, // ) // ], // mainAxisAlignment: MainAxisAlignment.spaceBetween, // ), // ), // ); // } else { String pic; String name; int uid; MyShopBeanDataData chatWith = MyShopBeanDataData(); if (data.senderUid == MyCookie().getUID()) { pic = data.receiverPic; name = data.receiverName; uid = data.receiverUid; chatWith.userName = data.receiverName; chatWith.userUid = data.receiverUid; chatWith.userPic = data.receiverPic; } else { pic = data.senderPic; name = data.senderName; uid = data.senderUid; chatWith.userName = data.senderName; chatWith.userUid = data.senderUid; chatWith.userPic = data.senderPic; } if (data.senderUid == -1) { return Slidable( actionPane: SlidableDrawerActionPane(), actionExtentRatio: 0.25, child: GestureDetector( onTap: () { MyTools().toPage(context, SystemInformationPage(), (then) {}); }, behavior: HitTestBehavior.translucent, child: Container( height: 73, child: Column( children: [ Expanded( child: Row( children: [ Container( margin: EdgeInsets.only(left: 14, right: 14), child: ClipRRect( child: Image.asset( 'images/app_logo.png', height: 45, width: 45, ), borderRadius: BorderRadius.all(Radius.circular(4)), ), ), Expanded( child: Container( height: 45, padding: EdgeInsets.only(right: 14), child: Column( children: [ Row( children: [ Expanded( child: Text( '系统消息', style: TextStyle( color: MyColors.c333333, fontSize: 15), )), Text( '', style: TextStyle( color: MyColors.c888888, fontSize: 12), ), ], ), Row( children: [ Expanded( child: Text( data.content, style: TextStyle( color: MyColors.c888888, fontSize: 12), maxLines: 1, overflow: TextOverflow.ellipsis, softWrap: true, ), ), ], ) ], mainAxisAlignment: MainAxisAlignment.spaceBetween, ), ), ) ], ), ), Container( margin: EdgeInsets.only(left: 73, right: 14), height: 0.5, color: MyColors.cE7E7E7, ) ], mainAxisAlignment: MainAxisAlignment.spaceBetween, ), ), ), secondaryActions: [ SlideAction( child: Container( child: Container( decoration: BoxDecoration( borderRadius: BorderRadius.circular(30), color: MyColors.cFF4233, ), height: 40, width: 40, child: SvgPicture.asset( 'images/svg/del.svg', height: 25, width: 25, ), alignment: Alignment.center, ), ), onTap: () { showSimpleDialog('是否删除系统消息', context, () async { Navigator.pop(context); MyCookie().prefs.remove('${MyCookie().getUID()}系统消息'); chatRoom.remove('系统消息'); lastMsg.remove(data); setState(() {}); queryChatRoom(); }); }, ) ], ); } else { return Slidable( actionPane: SlidableDrawerActionPane(), actionExtentRatio: 0.25, child: GestureDetector( onTap: () { MyTools().toPage(context, ChatPage(chatWith, null), (then) { var now = new DateTime.now(); MyCookie().prefs.setString( '${MyCookie().getUID()}_${chatWith.userUid}', now.toString().substring(0, 19)); queryChatRoom(); }); }, behavior: HitTestBehavior.translucent, child: Container( height: 73, child: Column( children: [ Expanded( child: Row( children: [ Container( margin: EdgeInsets.only(left: 14, right: 14), child: ClipRRect( child: uid == 0 ? Image.asset( 'images/app_logo.png', height: 45, width: 45, ) : MyViews().netImg(imgURL(pic), 45, 45, placeholder: 'images/svg/占位图.svg'), borderRadius: BorderRadius.all(Radius.circular(4)), ), ), Expanded( child: Container( height: 45, padding: EdgeInsets.only(right: 14), child: Column( children: [ Row( children: [ Expanded( child: Text( uid == 0 ? '客服' : name, style: TextStyle( color: MyColors.c333333, fontSize: 15), )), Text( data.sentAt, style: TextStyle( color: MyColors.c888888, fontSize: 12), ), ], ), Row( children: [ Expanded( child: Text( data.type != chatMsgTypeText ? '[${data.type}]' : data.content, style: TextStyle( color: MyColors.c888888, fontSize: 12), maxLines: 1, overflow: TextOverflow.ellipsis, softWrap: true, ), ), Visibility( visible: showRedPoint( chatWith.userUid, data.sentAt), child: Container( decoration: BoxDecoration( color: MyColors.cFF4233, borderRadius: BorderRadius.all( Radius.circular(4))), height: 8, width: 8, padding: EdgeInsets.only(left: 5, right: 5), alignment: Alignment.center, ), ) ], ) ], mainAxisAlignment: MainAxisAlignment.spaceBetween, ), ), ) ], ), ), Container( margin: EdgeInsets.only(left: 73, right: 14), height: 0.5, color: MyColors.cE7E7E7, ) ], mainAxisAlignment: MainAxisAlignment.spaceBetween, ), ), ), secondaryActions: [ SlideAction( child: Container( child: Container( decoration: BoxDecoration( borderRadius: BorderRadius.circular(30), color: MyColors.cFF4233, ), height: 40, width: 40, child: SvgPicture.asset( 'images/svg/del.svg', height: 25, width: 25, ), alignment: Alignment.center, ), ), onTap: () { showSimpleDialog( '将删除与(${name.isEmpty ? '客服' : name})的聊天记录,确认删除?', context, () async { Navigator.pop(context); MsgDB msgDB = MsgDB('table${MyCookie().getUID()}_$uid'); await msgDB.deleteTableData(); chatRoom.remove(uid); lastMsg.remove(data); setState(() {}); queryChatRoom(); }); }, ) ], ); } // } } Map chatRoom = Map(); List lastMsg = []; queryChatRoom() async { List> map = await SqlManager.allTables(); map.forEach((element) async { TableBeanEntity table = TableBeanEntity().fromJson(json.decode(json.encode(element))); if (table.name.contains('table${MyCookie().getUID()}_')) { MsgDB msgDB = MsgDB(table.name); List> maps = await msgDB.queryTableData(1, 1); maps.forEach((element) { print(json.encode(element)); MessageBeanContent msg = MessageBeanContent().fromJson(json.decode(json.encode(element))); if (msg.senderUid == MyCookie().getUID()) { print(msg.receiverUid); chatRoom[msg.receiverUid] = msg; } else { print(msg.senderUid); chatRoom[msg.senderUid] = msg; } lastMsg.clear(); chatRoom.forEach((key, value) { lastMsg.add(value); }); lastMsg.sort((left, right) { if (left.senderUid == -1) { return -1; } else if (right.senderUid == -1) { return 1; } else { return right.sentAt.compareTo(left.sentAt); } }); systemInformation = MyCookie().prefs.getString('${MyCookie().getUID()}系统消息'); if (systemInformation != null) { print(systemInformation); MessageBeanContent sysMsg = MessageBeanContent(); sysMsg.senderName = '系统消息'; sysMsg.senderUid = -1; sysMsg.content = systemInformation.split('!@##@!')[0]; chatRoom['系统消息'] = sysMsg; if (!lastMsg.any((element) => element.senderUid == -1)) { lastMsg.insert(0, chatRoom['系统消息']); } } setState(() {}); }); } }); endRe(controller); } void onRefresh() { queryChatRoom(); } showRedPoint(int userUid, String sentAt) { try { String lastTime = MyCookie().prefs.getString('${MyCookie().getUID()}_$userUid'); print('--------------'); print(lastTime); print(sentAt); print('--------------'); return lastTime.compareTo(sentAt) == -1; } catch (e) { return true; } } }