| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548 |
- import 'dart:convert';
- import 'dart:io';
- import 'package:bbyyy/beans/goods_bean_entity.dart';
- import 'package:bbyyy/beans/message_bean_entity.dart';
- import 'package:bbyyy/beans/send_message_bean_entity.dart';
- import 'package:bbyyy/beans/upload_image_bean_entity.dart';
- import 'package:bbyyy/https/my_request.dart';
- import 'package:bbyyy/https/url.dart';
- import 'package:bbyyy/msgDB/my_msg_db.dart';
- import 'package:bbyyy/my_tools/AsperctRaioImage.dart';
- import 'package:bbyyy/my_tools/const.dart';
- import 'package:bbyyy/my_tools/easy_loading/easy_loading.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_data.dart';
- import 'package:bbyyy/paegs/picture_display_page/picture_display_page.dart';
- import 'package:bbyyy/paegs/red_packet_page/red_packet_page.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:flutter_svg/svg.dart';
- class ChatPageView {
- TextEditingController textEditingController = TextEditingController();
- var showM = false;
- var showS = false;
- inputBox(BuildContext context, FocusNode focusNode) {
- return StatefulBuilder(builder: (c, s) {
- return SafeArea(
- top: false,
- child: Container(
- child: Material(
- color: MyColors.cf8f8f8,
- child: Column(
- children: <Widget>[
- Container(
- constraints: BoxConstraints(
- maxHeight: 144.0,
- minHeight: 50,
- ),
- padding: EdgeInsets.only(left: 0, right: 13),
- child: Row(
- children: <Widget>[
- Expanded(
- flex: 280,
- child: Container(
- constraints: BoxConstraints(
- minHeight: 30,
- ),
- margin: EdgeInsets.only(
- top: 10, bottom: 10, left: 14, right: 8.0),
- decoration: BoxDecoration(
- color: Colors.white,
- borderRadius: BorderRadius.circular(2.0)),
- child: TextField(
- controller: textEditingController,
- focusNode: focusNode,
- cursorColor: MyColors.cFF4233,
- cursorWidth: 1.0,
- onTap: () {
- s(() {
- showM = false;
- EventBus().emit('s2b');
- });
- },
- decoration: InputDecoration(
- border: InputBorder.none,
- disabledBorder: InputBorder.none,
- enabledBorder: InputBorder.none,
- focusedBorder: InputBorder.none,
- isDense: true,
- contentPadding:
- const EdgeInsets.fromLTRB(8, 4.5, 8, 4.5)),
- maxLines: null,
- style: TextStyle(
- color: Colors.black,
- fontSize: 16,
- height: 1.3,
- letterSpacing: 0.2),
- keyboardType: TextInputType.multiline,
- onChanged: (t) {
- s(() {
- if (t.length > 0) {
- showS = true;
- } else {
- showS = false;
- }
- });
- },
- ),
- ),
- ),
- Expanded(
- child: showS
- ? GestureDetector(
- child: Container(
- height: 30,
- width: 52,
- margin: EdgeInsets.only(bottom: 10),
- alignment: Alignment.center,
- decoration: BoxDecoration(
- borderRadius:
- BorderRadius.all(Radius.circular(15)),
- color: MyColors.cFF4233),
- child: Text(
- '发送',
- style: TextStyle(
- color: Colors.white, fontSize: 15),
- ),
- ),
- onTap: () {
- // s(() {
- // sendMessages(
- // textEditingController.text.toString(),
- // false);
- // textEditingController.clear();
- // showS = false;
- // });
- sendMsg(
- chatMsgTypeText,
- ChatData().chatWith.userUid,
- textEditingController.text.toString(),
- (re, hE) async {
- if (!hE) {
- SendMessageBeanEntity entity =
- SendMessageBeanEntity().fromJson(
- json.decode(re.data.toString()));
- MsgDB msgDB = MsgDB(
- 'table${MyCookie().getUID()}_${entity.data.receiverUid}');
- if (!msgDB.isTableExits) {
- await msgDB.open();
- }
- await msgDB.addTableData([entity.data]);
- EventBus().emit('hasNewMsg', entity.data);
- EventBus().emit(
- 'hasNewMsgInMsgPage', entity.data);
- }
- }, (e) {});
- s(() {
- textEditingController.clear();
- showS = false;
- });
- },
- behavior: HitTestBehavior.translucent,
- )
- : GestureDetector(
- child: Container(
- child: SvgPicture.asset(
- 'images/svg/功能(发图片什么的).svg',
- height: 28,
- width: 28,
- ),
- margin: EdgeInsets.only(bottom: 10),
- ),
- onTap: () {
- s(() {
- showM = !showM;
- if (showM) {
- MyTools().hideKeyboard(context);
- }
- });
- },
- behavior: HitTestBehavior.translucent,
- ),
- flex: 58,
- ),
- ],
- crossAxisAlignment: CrossAxisAlignment.end,
- ),
- ),
- Visibility(
- child: Column(
- children: <Widget>[
- Divider(
- height: 0.5,
- color: MyColors.cE1E1E1,
- ),
- Container(
- height: 100,
- child: SingleChildScrollView(
- child: moreItem(context, focusNode, s),
- ),
- ),
- ],
- ),
- visible: showM,
- )
- ],
- mainAxisSize: MainAxisSize.min,
- ),
- elevation: 0,
- ),
- color: MyColors.cf3f3f3,
- ),
- );
- });
- }
- moreItem(BuildContext context, FocusNode focusNode, StateSetter s) {
- return Container(
- height: 100,
- child: GridView(
- gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
- crossAxisCount: 5, //横轴三个子widget
- childAspectRatio: 1,
- crossAxisSpacing: 20,
- mainAxisSpacing: 10 //宽高比为1时,子widget
- ),
- children: getItems(context, focusNode, s),
- padding: EdgeInsets.only(left: 14, right: 20, top: 8, bottom: 8),
- ),
- );
- }
- getItems(BuildContext context, FocusNode focusNode, StateSetter s) {
- List<Widget> items = [];
- items.add(GestureDetector(
- child: Container(
- child: SvgPicture.asset(
- 'images/svg/图片.svg',
- ),
- ),
- behavior: HitTestBehavior.translucent,
- onTap: () async {
- File img = await selectPicturesIndividually();
- if (img != null) {
- sendImg(img);
- }
- },
- ));
- if (!MyCookie().underReview)
- items.add(GestureDetector(
- child: Container(
- child: SvgPicture.asset(
- 'images/svg/发红包.svg',
- ),
- ),
- behavior: HitTestBehavior.translucent,
- onTap: () async {
- MyTools()
- .toPage(context, RedPacketPage(ChatData().chatWith), (then) {});
- },
- ));
- return items;
- }
- goodsInfo(GoodsBeanDataData goods) {
- if (goods == null) {
- return Container();
- } else {
- return Container(
- padding: EdgeInsets.symmetric(horizontal: 12, vertical: 10),
- color: Colors.white,
- child: Row(
- children: [
- Container(
- margin: EdgeInsets.only(right: 7),
- child: ClipRRect(
- child: MyViews().netImg(imgURL(goods.coverPath), 54, 54,
- placeholder: 'images/svg/goodsDefImg.svg'),
- borderRadius: BorderRadius.circular(4),
- ),
- ),
- Expanded(
- child: Container(
- constraints: BoxConstraints(minHeight: 54),
- child: Column(
- children: [
- Text(
- '${goods.title}',
- style: TextStyle(color: MyColors.c333333, fontSize: 13),
- maxLines: 2,
- overflow: TextOverflow.ellipsis,
- softWrap: true,
- ),
- Row(
- children: [
- MyViews()
- .myText('¥${goods.price}', MyColors.cFF4233, 12),
- Container(
- decoration: BoxDecoration(
- color: MyColors.cFF4233,
- borderRadius: BorderRadius.circular(7),
- ),
- height: 14,
- width: 40,
- child: MyViews()
- .myText('找货', Colors.white, 8, height: 1.0),
- alignment: Alignment.center,
- padding: EdgeInsets.only(top: 1),
- )
- ],
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- ),
- ],
- crossAxisAlignment: CrossAxisAlignment.start,
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- ),
- ),
- ),
- ],
- ),
- );
- }
- }
- chatItems(MessageBeanContent msg, BuildContext context) {
- return Container(
- padding: EdgeInsets.only(left: 14, right: 14, bottom: 11, top: 11),
- child: Column(
- children: [
- Visibility(
- child: MyViews().myText(msg.sentAt, MyColors.c999999, 10),
- visible: showTime(msg),
- ),
- Container(
- height: 13,
- ),
- Row(
- children: [
- msg.senderUid != MyCookie().getUID()
- ? headWidget(msg)
- : Expanded(
- child: Container(
- child: chatBubble(msg, context),
- alignment: Alignment.centerRight,
- )),
- Container(
- width: 8,
- ),
- msg.senderUid != MyCookie().getUID()
- ? Expanded(
- child: Container(
- child: chatBubble(msg, context),
- alignment: Alignment.centerLeft,
- ))
- : headWidget(msg),
- ],
- crossAxisAlignment: msg.senderUid != MyCookie().getUID()
- ? CrossAxisAlignment.start
- : CrossAxisAlignment.start,
- mainAxisAlignment: MainAxisAlignment.start,
- ),
- Visibility(
- child: Container(
- child: MyViews().myText('红包已自动存入', MyColors.c999999, 10),
- margin: EdgeInsets.only(top: 10),
- ),
- visible: msg.type == chatMsgTypeRedPackage,
- )
- ],
- ),
- );
- }
- headWidget(MessageBeanContent msg) {
- return ClipRRect(
- child: msg.senderUid == 0
- ? Image.asset(
- 'images/app_logo.png',
- width: 40,
- height: 40,
- )
- : MyViews().netImg(imgURL(msg.senderPic), 40, 40),
- borderRadius: BorderRadius.circular(20),
- );
- }
- chatBubble(MessageBeanContent msg, BuildContext context) {
- if (msg.type == chatMsgTypePic) {}
- return ClipRRect(
- borderRadius: BorderRadius.circular(4),
- child: Container(
- constraints: BoxConstraints(
- minHeight: 40, maxWidth: MediaQuery.of(context).size.width - 140),
- color: Colors.white,
- child: bubbleItem(msg, context),
- padding: EdgeInsets.symmetric(
- horizontal: msg.type == chatMsgTypeText ? 14 : 1,
- vertical: msg.type == chatMsgTypeText ? 10 : 1),
- ),
- );
- }
- // sendMessages(String content, bool isImg) async {
- // var now = new DateTime.now();
- // //http://172.16.104.14:4151/pub?topic=97231698
- // Response response = await Dio().post(
- // 'http://${MyCookie().server}:4151/pub?topic=${ChatData().chatWith.userUid}',
- // data: {
- // "type": "新消息",
- // "content": {
- // "sender": {
- // "pic": MyCookie().loginInformation.data.extra.picture,
- // "name": MyCookie().loginInformation.data.extra.name,
- // "id": MyCookie().loginInformation.data.extra.id,
- // "uid": MyCookie().getUID(),
- // },
- // "receiver": {
- // "pic": ChatData().chatWith.userPic,
- // "name": ChatData().chatWith.userName,
- // "id": ChatData().chatWith.userUid,
- // "uid": ChatData().chatWith.userUid,
- // },
- // "content": content,
- // "time": now.toString().substring(0, 19),
- // "type": isImg ? "图片" : "文字",
- // "uuid": getUUID()
- // }
- // });
- // if (response.data.toString() == 'OK') {
- // print(response.data.toString());
- // MsgDB msgDB =
- // MsgDB('table${MyCookie().getUID()}_${ChatData().chatWith.userUid}');
- // if (!msgDB.isTableExits) {
- // await msgDB.open();
- // }
- // NewMsgBeanEntity newMsg =
- // NewMsgBeanEntity().fromJson(json.decode(json.encode({
- // "type": "新消息",
- // "content": {
- // "sender": {
- // "pic": MyCookie().loginInformation.data.extra.picture,
- // "name": MyCookie().loginInformation.data.extra.name,
- // "id": MyCookie().loginInformation.data.extra.id,
- // "uid": MyCookie().getUID(),
- // },
- // "receiver": {
- // "pic": ChatData().chatWith.userPic,
- // "name": ChatData().chatWith.userName,
- // "id": ChatData().chatWith.userUid,
- // "uid": ChatData().chatWith.userUid,
- // },
- // "content": content,
- // "time": now.toString().substring(0, 19),
- // "type": isImg ? "图片" : "文字",
- // "uuid": getUUID(),
- // "shop": {},
- // "order": {},
- // "goods": {},
- // }
- // })));
- // await msgDB.addTableData([newMsg]);
- // EventBus().emit('hasNewMsg', newMsg);
- // EventBus().emit('hasNewMsgInMsgPage', newMsg);
- // }
- // }
- void sendImg(File img) {
- EasyLoading.show();
- upload(img, (re, hE) {
- if (!hE) {
- UploadImageBeanEntity data =
- UploadImageBeanEntity().fromJson(json.decode(re.data.toString()));
- sendMsg(chatMsgTypePic, ChatData().chatWith.userUid, data.data[0].path,
- (re, hE) async {
- if (!hE) {
- SendMessageBeanEntity entity = SendMessageBeanEntity()
- .fromJson(json.decode(re.data.toString()));
- MsgDB msgDB =
- MsgDB('table${MyCookie().getUID()}_${entity.data.receiverUid}');
- if (!msgDB.isTableExits) {
- await msgDB.open();
- }
- await msgDB.addTableData([entity.data]);
- EventBus().emit('hasNewMsg', entity.data);
- EventBus().emit('hasNewMsgInMsgPage', entity.data);
- }
- }, (e) {});
- }
- EasyLoading.dismiss();
- }, (e) {});
- }
- showTime(MessageBeanContent msg) {
- int index =
- ChatData().msges.indexWhere((element) => element.uuid == msg.uuid);
- if (index == ChatData().msges.length - 1) {
- return true;
- } else {
- return DateTime.parse(msg.sentAt).isAfter(
- DateTime.parse(ChatData().msges[index + 1].sentAt)
- .add(Duration(minutes: 2)));
- }
- }
- bubbleItem(MessageBeanContent msg, BuildContext context) {
- if (msg.type == chatMsgTypeText) {
- return GestureDetector(
- onLongPress: (){
- showToast('已复制到剪切板');
- Clipboard.setData(ClipboardData(text: '${msg.content}'));
- },
- child: MyViews().myText(msg.content, MyColors.c333333, 15));
- } else if (msg.type == chatMsgTypePic) {
- return GestureDetector(
- child: AsperctRaioImage.network(imgURL(msg.content),
- builder: (context, snapshot, url) {
- return MyViews().netImg(
- imgURL(msg.content),
- (snapshot.data.height.toDouble() / 5) /
- (snapshot.data.width.toDouble() / 5) *
- 150,
- 150,
- placeholder: 'images/svg/goodsDefImg.svg');
- }),
- onTap: () {
- MyTools().toPage(
- context, PictureDisplayPage(imgURL(msg.content)), (then) {});
- },
- behavior: HitTestBehavior.translucent,
- );
- } else if (msg.type == chatMsgTypeRedPackage) {
- return Container(
- decoration: BoxDecoration(
- color: Colors.white,
- borderRadius: BorderRadius.circular(4),
- ),
- padding: EdgeInsets.all(14),
- child: Row(
- children: [
- SvgPicture.asset(
- 'images/svg/红包.svg',
- width: 29,
- height: 34,
- ),
- Container(
- margin: EdgeInsets.only(left: 7),
- child: Column(
- children: [
- MyViews().myText(
- msg.content.split('!@#')[0], MyColors.c333333, 14),
- MyViews().myText('收款人:${msg.content.split('!@#')[1]}',
- MyColors.c333333, 11),
- ],
- crossAxisAlignment: CrossAxisAlignment.start,
- ),
- )
- ],
- ),
- );
- }
- }
- }
|