Hibok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

419 line
13 KiB

  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'dart:typed_data';
  4. import 'package:chat/chat/gift_select_widget.dart';
  5. import 'package:chat/data/UserData.dart';
  6. import 'package:chat/generated/i18n.dart';
  7. import 'package:chat/home/VipPage.dart';
  8. import 'package:chat/map/google_map_location_picker.dart';
  9. import 'package:chat/models/UserInfo.dart';
  10. import 'package:chat/models/money_change.dart';
  11. import 'package:chat/proto/all.pbserver.dart';
  12. import 'package:chat/utils/ChargeMoney.dart';
  13. import 'package:chat/utils/CustomUI.dart';
  14. import 'package:chat/utils/HttpUtil.dart';
  15. import 'package:chat/data/constants.dart';
  16. import 'package:chat/utils/app_navigator.dart';
  17. import 'package:chat/utils/blacklist_mgr.dart';
  18. import 'package:chat/utils/image_util.dart';
  19. import 'package:chat/utils/msgHandler.dart';
  20. import 'package:chat/utils/screen.dart';
  21. import 'package:dio/dio.dart';
  22. import 'package:flutter/material.dart';
  23. import 'package:flutter_image_compress/flutter_image_compress.dart';
  24. import 'package:google_maps_flutter/google_maps_flutter.dart';
  25. import 'package:image_picker/image_picker.dart';
  26. import 'package:oktoast/oktoast.dart';
  27. import 'package:permission_handler/permission_handler.dart';
  28. import 'package:provider/provider.dart';
  29. import 'package:video_thumbnail/video_thumbnail.dart';
  30. import 'package:file_picker/file_picker.dart';
  31. import '../r.dart';
  32. class UtilKeyboard extends StatefulWidget {
  33. final double keyboardHeight;
  34. final Function sendMsg;
  35. final bool isGroup;
  36. UtilKeyboard({this.keyboardHeight, this.sendMsg, this.isGroup});
  37. @override
  38. _UtilKeyboardState createState() => _UtilKeyboardState();
  39. }
  40. class _UtilKeyboardState extends State<UtilKeyboard> {
  41. bool isAuthority = false;
  42. @override
  43. Widget build(BuildContext context) {
  44. bool isShowRedPacket = UserData().redPacketSW > 0;
  45. List<Widget> iconList = [];
  46. iconList.add(
  47. _buildOtherSelect(R.assetsImagesChatItem1, I18n.of(context).camera, () {
  48. print('开始选择拍照');
  49. _sendPicture(context);
  50. }));
  51. iconList.add(
  52. _buildOtherSelect(R.assetsImagesChatItem2, I18n.of(context).video, () {
  53. print('开始选择视频');
  54. _sendVideo(context);
  55. }));
  56. if (!widget.isGroup) {
  57. iconList.add(
  58. _buildOtherSelect(R.assetsImagesChatItem3, I18n.of(context).chat, () {
  59. _audioChat(context);
  60. }));
  61. }
  62. iconList.add(
  63. _buildOtherSelect(R.assetsImagesChatItem4, I18n.of(context).locate, () {
  64. _openMap(context);
  65. }));
  66. if (!widget.isGroup) {
  67. iconList.add(Offstage(
  68. offstage: !isShowRedPacket,
  69. child: _buildOtherSelect(
  70. R.assetsImagesChatItem5, I18n.of(context).red_money, () {
  71. AppNavigator.pushCoinBagPage(context);
  72. })));
  73. iconList.add(_buildOtherSelect(
  74. R.assetsImagesChatItem6, I18n.of(context).giving_gift, () {
  75. _showGiftSheet(context);
  76. }));
  77. }
  78. iconList.add(_buildOtherSelect(R.assetsImagesChatItem7, '文件', () async {
  79. _sendFile(context);
  80. }));
  81. return Container(
  82. width: Screen.width,
  83. color: Colors.white,
  84. height: widget.keyboardHeight,
  85. padding: EdgeInsets.only(top: 20, bottom: 10, left: 20),
  86. alignment: Alignment.topLeft,
  87. child: Wrap(spacing: 10.0, runSpacing: 20.0, children: iconList));
  88. }
  89. _showGiftSheet(BuildContext context) {
  90. int friendId = 0;
  91. if (!widget.isGroup) {
  92. friendId = Provider.of<int>(context);
  93. }
  94. showModalBottomSheet(
  95. context: context,
  96. elevation: 2.0,
  97. shape: RoundedRectangleBorder(
  98. borderRadius: BorderRadius.only(
  99. topLeft: Radius.circular(20), topRight: Radius.circular(20))),
  100. backgroundColor: Colors.transparent,
  101. builder: (BuildContext context) {
  102. return StatefulBuilder(
  103. builder: (BuildContext context, setBottomSheetState) {
  104. return GiftSelectWidget(friendId, widget.sendMsg);
  105. },
  106. );
  107. });
  108. }
  109. _openMap(BuildContext context) async {
  110. if (await CustomUI.showPermissionSetting(
  111. context, PermissionGroup.location, I18n.of(context).open_location)) {
  112. var result = await LocationPicker.pickLocation(
  113. context, 'AIzaSyAb9JNtW0BEZ_qLeDg87ZhvxSmZply-7hU',
  114. initialCenter: LatLng(UserData().latitude, UserData().longitude));
  115. if (result == null ||
  116. result.address == null ||
  117. result.address.length == 0) {
  118. return;
  119. }
  120. var reslutStr = jsonEncode(result);
  121. int friendId = 0;
  122. if (!widget.isGroup) {
  123. friendId = Provider.of<int>(context);
  124. }
  125. var msg = MsgHandler.createSendMsg(
  126. ChatType.PlaceChatType, utf8.encode(reslutStr),
  127. friendId: friendId,
  128. channelType:
  129. widget.isGroup ? ChatChannelType.Group : ChatChannelType.Session);
  130. widget.sendMsg(msg);
  131. }
  132. }
  133. _audioChat(BuildContext context) async {
  134. if (await CustomUI.showPermissionSetting(context,
  135. PermissionGroup.microphone, I18n.of(context).video_permission)) {
  136. int friendId = Provider.of<int>(context);
  137. UserInfo info = await HttpUtil().getFriendInfo(friendId, true);
  138. if (info == null) {
  139. print('获取用户信息失败');
  140. return;
  141. }
  142. if (info.chatStatus == 0) {
  143. showToast(I18n.of(context).cantt_voice);
  144. return;
  145. }
  146. if (BlacklistMgr.isBlack(info.userId)) {
  147. return;
  148. }
  149. //对方关闭陌生人消息,则提示
  150. if (!info.isCanStrangerNews) {
  151. showToast(I18n.of(context).stranger_close_tips);
  152. return;
  153. }
  154. if (info.isAuthority ||
  155. (!UserData().isMan() && UserData().basicInfo.isAttestation) ||
  156. info.distince < 200) {
  157. isAuthority = true;
  158. }
  159. becomeVip() {
  160. Navigator.of(context).push(
  161. new MaterialPageRoute(
  162. builder: (context) {
  163. return VipPage();
  164. },
  165. ),
  166. );
  167. }
  168. payCallback() {
  169. if (Provider.of<MoneyChangeProvider>(context).money <
  170. UserData().accountPrice) {
  171. Navigator.of(context).pop();
  172. CustomUI.buildOneConfirm(
  173. context,
  174. I18n.of(context).balance_insufficien,
  175. I18n.of(context).recharge, () {
  176. Navigator.of(context).pop();
  177. ChargeMoney.showChargeSheet(context, () {});
  178. });
  179. return;
  180. }
  181. Navigator.of(context).pop();
  182. HttpUtil().buyChatAccount(UserData().accountPrice, info.userId, context,
  183. () {
  184. isAuthority = true;
  185. });
  186. }
  187. freeTime() {
  188. HttpUtil().userFreeTime(context, info.userId, 2, () {
  189. UserData().basicInfo.usedNum++;
  190. Navigator.of(context).pop();
  191. isAuthority = true;
  192. });
  193. }
  194. if (!isAuthority) {
  195. if (UserData().isVip) {
  196. UserData().basicInfo.freeNum < UserData().basicInfo.usedNum
  197. ? CustomUI.buildOneConfirm(
  198. context,
  199. I18n.of(context).unlock_information,
  200. I18n.of(context)
  201. .pay_unlock
  202. .replaceFirst('/s1', UserData().accountPrice.toString()),
  203. payCallback)
  204. : CustomUI.buildOneConfirm(
  205. context,
  206. I18n.of(context).unlock_information,
  207. I18n.of(context).unlock_choose,
  208. freeTime,
  209. );
  210. } else {
  211. CustomUI.buildTowConfirm(
  212. context,
  213. I18n.of(context).unlock_information,
  214. I18n.of(context).become_member,
  215. becomeVip,
  216. I18n.of(context)
  217. .pay_unlock
  218. .replaceFirst('/s1', UserData().accountPrice.toString()),
  219. payCallback);
  220. }
  221. return;
  222. }
  223. AppNavigator.pushAudioChatPage(context, info);
  224. }
  225. }
  226. void _sendPicture(BuildContext context) async {
  227. if (await CustomUI.showPermissionSetting(
  228. context, PermissionGroup.camera, I18n.of(context).camera_permission)) {
  229. File imgFile = await ImagePicker.pickImage(source: ImageSource.camera);
  230. if (imgFile == null) {
  231. return;
  232. }
  233. var imgSize = await imgFile.length();
  234. print('图片大小:${imgSize / 1024}KB');
  235. var sendImg;
  236. bool isNeedUpload = false;
  237. if (imgSize > ImgSizeLimit) {
  238. print('图片大于 $ImgSizeLimit,压缩');
  239. //发送压缩图
  240. sendImg = await WidgetUtil.getCompressImg(imgFile.absolute.path);
  241. isNeedUpload = true;
  242. } else {
  243. sendImg = imgFile.readAsBytesSync().toList();
  244. }
  245. var rect = await WidgetUtil.getImageWH(
  246. image: Image.memory(Uint8List.fromList(sendImg)));
  247. int aspectRatio = rect.width * 100 ~/ rect.height;
  248. int friendId = 0;
  249. if (!widget.isGroup) {
  250. friendId = Provider.of<int>(context);
  251. }
  252. var msg = MsgHandler.createSendMsg(ChatType.ImageChatType, sendImg,
  253. extra: aspectRatio,
  254. friendId: friendId,
  255. localFile: isNeedUpload ? imgFile.absolute.path : null,
  256. channelType:
  257. widget.isGroup ? ChatChannelType.Group : ChatChannelType.Session);
  258. widget.sendMsg(msg);
  259. }
  260. }
  261. void _sendFile(BuildContext context) async {
  262. File file = await FilePicker.getFile();
  263. int fileSize = file.lengthSync();
  264. print('选择的文件 ${file.path} 大小 $fileSize');
  265. if (fileSize > 33 * 1024 * 1024) {
  266. showToast('文件大于33M');
  267. return;
  268. }
  269. int friendId = 0;
  270. if (!widget.isGroup) {
  271. friendId = Provider.of<int>(context);
  272. }
  273. var fileName = file.path.split('/').last;
  274. print('fileName $fileName');
  275. var ext = '';
  276. var extList = fileName.split('.');
  277. if (extList.length > 1) {
  278. ext = extList.last;
  279. }
  280. print('ext $ext');
  281. var fileMsg = FileChat.create();
  282. fileMsg.type = ext;
  283. fileMsg.size = fileSize;
  284. fileMsg.name = fileName;
  285. var msg = MsgHandler.createSendMsg(
  286. ChatType.FileChatType, fileMsg.writeToBuffer(),
  287. friendId: friendId,
  288. localFile: file.path,
  289. channelType:
  290. widget.isGroup ? ChatChannelType.Group : ChatChannelType.Session);
  291. widget.sendMsg(msg);
  292. }
  293. void _sendVideo(BuildContext context) async {
  294. if (await CustomUI.showPhotoPermissionSetting(context)) {
  295. File video = await ImagePicker.pickVideo(source: ImageSource.gallery);
  296. if (video == null) {
  297. return;
  298. }
  299. var videoSize = await video.length();
  300. print('视频大小:$videoSize');
  301. if (videoSize > 33 * 1024 * 1024) {
  302. showToast(I18n.of(Constants.getCurrentContext()).video_more_big);
  303. return;
  304. }
  305. final thumbnail = await getVideoThumbnail(video);
  306. var rect = await WidgetUtil.getImageWH(
  307. image: Image.memory(Uint8List.fromList(thumbnail)));
  308. int aspectRatio = rect.width * 100 ~/ rect.height;
  309. int friendId = 0;
  310. if (!widget.isGroup) {
  311. friendId = Provider.of<int>(context);
  312. }
  313. var msg = MsgHandler.createSendMsg(ChatType.ShortVideoChatType, thumbnail,
  314. extra: aspectRatio,
  315. friendId: friendId,
  316. localFile: video.path,
  317. channelType:
  318. widget.isGroup ? ChatChannelType.Group : ChatChannelType.Session);
  319. widget.sendMsg(msg);
  320. }
  321. }
  322. getVideoThumbnail(File video) async {
  323. List<int> thumbnail = await VideoThumbnail.thumbnailData(
  324. imageFormat: ImageFormat.JPEG,
  325. video: video.path,
  326. quality: 20,
  327. );
  328. if (thumbnail.length > ImgSizeLimit) {
  329. print('图片较大 ${thumbnail.length}');
  330. thumbnail =
  331. await FlutterImageCompress.compressWithList(thumbnail, quality: 10);
  332. print('压缩后 ${thumbnail.length}');
  333. }
  334. return thumbnail;
  335. }
  336. }
  337. Widget _buildOtherSelect(String imgPath, String title, VoidCallback onTap) {
  338. var imgWidth = Screen.width / 4 - 20;
  339. return InkWell(
  340. child: Container(
  341. width: imgWidth,
  342. child: Column(
  343. children: <Widget>[
  344. SizedBox(
  345. child: Image.asset(imgPath, fit: BoxFit.contain),
  346. width: 40,
  347. height: 40,
  348. ),
  349. SizedBox(height: 5),
  350. Text(title,
  351. maxLines: 1,
  352. overflow: TextOverflow.ellipsis,
  353. textScaleFactor: 1.0,
  354. style: TextStyle(color: Color(0xFF090909), fontSize: 12))
  355. ],
  356. ),
  357. ),
  358. onTap: onTap,
  359. );
  360. }