Hibok
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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