| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692 |
- 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<MsgPage> with AutomaticKeepAliveClientMixin {
- var types = ['商品', '对象'];
- List<DropdownMenuItem<String>> 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: SvgPicture.asset(
- 'images/svg/平台助手.svg',
- 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: <Widget>[
- 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: <Widget>[
- 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<MessageBeanContent> lastMsg = [];
- queryChatRoom() async {
- List<Map<String, dynamic>> 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<Map<String, dynamic>> 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;
- }
- }
- }
|