Hibok
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

583 строки
19 KiB

  1. import 'dart:async';
  2. import 'package:chat/chat/ChatPage.dart';
  3. import 'package:chat/data/UserData.dart';
  4. import 'package:chat/data/chat_data_mgr.dart';
  5. import 'package:chat/data/constants.dart';
  6. import 'package:chat/data/translate_hk_data_mgr.dart';
  7. import 'package:chat/generated/i18n.dart';
  8. import 'package:chat/home/SystemEditPage.dart';
  9. import 'package:chat/home/find_page.dart';
  10. import 'package:chat/home/realtimehelper/real_time_helper_page.dart';
  11. import 'package:chat/home/unread_dot_widget.dart';
  12. import 'package:chat/models/ChatMsg.dart';
  13. import 'package:chat/models/UserInfo.dart';
  14. import 'package:chat/models/ref_name_provider.dart';
  15. import 'package:chat/proto/all.pbserver.dart';
  16. import 'package:chat/utils/CustomUI.dart';
  17. import 'package:chat/utils/HttpUtil.dart';
  18. import 'package:chat/utils/MessageMgr.dart';
  19. import 'package:chat/utils/TokenMgr.dart';
  20. import 'package:chat/utils/app_navigator.dart';
  21. import 'package:chat/utils/file_preview_local.dart';
  22. import 'package:chat/utils/local_notification_util.dart';
  23. import 'package:chat/utils/msgHandler.dart';
  24. import 'package:chat/utils/receive_share_file.dart';
  25. import 'package:chat/utils/screen.dart';
  26. import 'package:chat/utils/sound_util.dart';
  27. import 'package:chat/utils/version_update_utils.dart';
  28. import 'package:dio/dio.dart';
  29. import 'package:flutter/material.dart';
  30. import 'package:chat/home/ConversActionPage.dart';
  31. import 'package:chat/home/ProfilePage.dart';
  32. import 'package:flutter/services.dart';
  33. import 'package:location_permissions/location_permissions.dart';
  34. import 'package:oktoast/oktoast.dart';
  35. import 'package:provider/provider.dart';
  36. import 'package:shared_preferences/shared_preferences.dart';
  37. import 'package:chat/utils/my_bottom_navigation_bar.dart' as myBottm;
  38. import 'friend_page.dart';
  39. class NavigationIconView {
  40. final BottomNavigationBarItem item;
  41. NavigationIconView({
  42. Key key,
  43. String title,
  44. IconData icon,
  45. int type,
  46. EdgeInsets iconMargin,
  47. double iconSize = 24,
  48. bool isCenter = false,
  49. bool isShowdot = false,
  50. }) : item = BottomNavigationBarItem(
  51. activeIcon: Stack(
  52. alignment: Alignment.center,
  53. children: <Widget>[
  54. isCenter
  55. ? Container(
  56. decoration: BoxDecoration(
  57. shape: BoxShape.circle,
  58. boxShadow: [
  59. BoxShadow(
  60. color: const Color(0x82008DED),
  61. blurRadius: 5.5,
  62. ),
  63. ],
  64. gradient: LinearGradient(
  65. begin: Alignment.topCenter,
  66. end: Alignment.bottomCenter,
  67. colors: <Color>[
  68. const Color(0xFF11E6FF),
  69. const Color(0xFF008AFF),
  70. ]),
  71. ),
  72. margin: EdgeInsets.only(bottom: 4),
  73. width: iconSize + 10,
  74. height: iconSize + 10,
  75. )
  76. : Container(),
  77. Container(
  78. margin: isCenter
  79. ? EdgeInsets.only(bottom: 8, right: 6)
  80. : iconMargin ?? EdgeInsets.zero,
  81. child: Icon(icon,
  82. size: iconSize,
  83. color:
  84. isCenter ? Colors.white : Constants.BlueTextColor)),
  85. ],
  86. ),
  87. title: Text(''),
  88. backgroundColor: Colors.white,
  89. icon: Stack(
  90. alignment: Alignment.center,
  91. children: <Widget>[
  92. isCenter
  93. ? Container(
  94. decoration: BoxDecoration(
  95. shape: BoxShape.circle,
  96. boxShadow: [
  97. BoxShadow(
  98. color: const Color(0x82008DED),
  99. blurRadius: 5.5,
  100. ),
  101. ],
  102. gradient: LinearGradient(
  103. begin: Alignment.topCenter,
  104. end: Alignment.bottomCenter,
  105. colors: <Color>[
  106. const Color(0xFF11E6FF),
  107. const Color(0xFF008AFF),
  108. ]),
  109. ),
  110. margin: EdgeInsets.only(bottom: 4),
  111. width: iconSize + 10,
  112. height: iconSize + 10,
  113. )
  114. : Container(),
  115. Container(
  116. margin: isCenter
  117. ? EdgeInsets.only(bottom: 8, right: 6)
  118. : iconMargin ?? EdgeInsets.zero,
  119. child: isShowdot
  120. ? UnreadDot(
  121. child: Icon(icon,
  122. size: iconSize,
  123. color: Constants.BottomIconGreyColor),
  124. type: type)
  125. : Icon(icon,
  126. size: iconSize,
  127. color: isCenter
  128. ? Colors.white
  129. : Constants.BottomIconGreyColor))
  130. // child: Icon(icon,
  131. // size: iconSize,
  132. // color: isCenter
  133. // ? Colors.white
  134. // : Constants.BottomIconGreyColor))
  135. ],
  136. ));
  137. }
  138. class HomeMain extends StatefulWidget {
  139. HomeMain({Key key}) : super(key: key);
  140. _HomeMainState createState() => _HomeMainState();
  141. }
  142. class _HomeMainState extends State<HomeMain> {
  143. List<Widget> _pages;
  144. int _currentIndex = 0;
  145. List<NavigationIconView> _navigationViews;
  146. List<String> _titles = [];
  147. DateTime lastPopTime;
  148. Timer time;
  149. @override
  150. void initState() {
  151. SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
  152. super.initState();
  153. LocalNotificationUtil.instance.initState(context);
  154. HttpUtil().checkReceiptCache();
  155. HttpUtil().getSettingInfo();
  156. MessageMgr().on('Receive AudioChat Request', receiveAudioChatRequest);
  157. MessageMgr().on('Login Out', loginOut);
  158. MessageMgr().on('PostContact', postContact);
  159. MessageMgr().on('Show My Profile', showMyProfile);
  160. MessageMgr().on('test_Permission', msgTestPermission);
  161. MessageMgr().on(MessageMgr.RECEIVE_THIRD_SHARE, goShareInfo);
  162. MessageMgr()
  163. .on(MessageMgr.TRANSLATE_HK_RECEIVE_ORDER, receiveTranslateOrder);
  164. MessageMgr().on(MessageMgr.TRANSLATE_HK_START_CHAT, startTranslateHKChat);
  165. ///初始化语音播放是外放还是听筒-默认外放
  166. SoundUtils.instance.initPlayMode();
  167. postContact(null);
  168. postLoaction();
  169. locationSchedule();
  170. HttpUtil().saveMobileInfo();
  171. WidgetsBinding.instance.addPostFrameCallback((_) {
  172. VersionUtils.versionUpdate(context);
  173. ReceiveShareFile.start();
  174. });
  175. }
  176. @override
  177. void didChangeDependencies() {
  178. super.didChangeDependencies();
  179. Provider.of<RefNameProvider>(context, listen: false).init();
  180. }
  181. showMyProfile(args) {
  182. setState(() {
  183. _currentIndex = _pages.length - 1;
  184. UserData().homemainIndex = _currentIndex;
  185. });
  186. }
  187. //三分钟获取一次位置信息
  188. void locationSchedule() {
  189. time = Timer.periodic(Duration(milliseconds: 180000), (timer) async {
  190. postLoaction();
  191. });
  192. }
  193. postLoaction() async {
  194. if (UserData().hasLocationPermission) {
  195. UserData().getCurrentPosition(callback: (loc) async {
  196. if (UserData().basicInfo.userId != null) {
  197. //传给服务器
  198. Map data = {
  199. "userId": UserData().basicInfo.userId,
  200. };
  201. data['sign'] = TokenMgr().getSign(data);
  202. data['lat'] = loc.latitude;
  203. data['lon'] = loc.longitude;
  204. Response res =
  205. await HttpUtil().post('location/user/insert', data: data);
  206. if (res == null) {
  207. return;
  208. }
  209. var resData = res.data;
  210. print('location/user/insert $resData');
  211. }
  212. });
  213. }
  214. }
  215. //上传通讯录
  216. postContact(args) async {
  217. SharedPreferences prefs = await SharedPreferences.getInstance();
  218. int localNum = prefs.getInt(Constants.ContactNum);
  219. if (UserData().contactList.length != 0 &&
  220. UserData().contactList.length != localNum &&
  221. UserData().basicInfo.userId != null) {
  222. Map data = {
  223. "userId": UserData().basicInfo.userId,
  224. };
  225. data['sign'] = TokenMgr().getSign(data);
  226. data['list'] = UserData().contactList;
  227. Response res = await HttpUtil().post('maillist/user/insert', data: data);
  228. if (res == null) {
  229. return;
  230. }
  231. var resData = res.data;
  232. if (resData['code'] == 0) {
  233. prefs.setInt(Constants.ContactNum, UserData().contactList.length);
  234. }
  235. }
  236. }
  237. @override
  238. void dispose() {
  239. MessageMgr().off('Receive AudioChat Request', receiveAudioChatRequest);
  240. MessageMgr().off('Login Out', loginOut);
  241. MessageMgr().off('PostContact', postContact);
  242. MessageMgr().off('Show My Profile', showMyProfile);
  243. MessageMgr().off('test_Permission', msgTestPermission);
  244. MessageMgr().off(MessageMgr.RECEIVE_THIRD_SHARE, goShareInfo);
  245. MessageMgr()
  246. .off(MessageMgr.TRANSLATE_HK_RECEIVE_ORDER, receiveTranslateOrder);
  247. MessageMgr().off(MessageMgr.TRANSLATE_HK_START_CHAT, startTranslateHKChat);
  248. LocalNotificationUtil.instance.dispose();
  249. if (time != null) {
  250. time.cancel();
  251. }
  252. super.dispose();
  253. }
  254. goShareInfo(data) async {
  255. Future.delayed(Duration(milliseconds: 1000), () {
  256. Navigator.of(Constants.getCurrentContext()).push(
  257. new MaterialPageRoute(
  258. builder: (context) {
  259. return FilePreview(data);
  260. },
  261. ),
  262. );
  263. });
  264. }
  265. receiveTranslateOrder(var data) async {
  266. TranslateOrder order = data;
  267. CustomUI.buildTranslationHelperOrderDialog(context,false,orderId: order.orderId,scenes: order.scenes,desc: order.desc,createTime: order.createTime,userName: order.userName);
  268. }
  269. startTranslateHKChat(var friendID){
  270. if(TranslateHKMgr().isUser){
  271. CustomUI.buildTranslationHelperOrderDialog(context,true,userStartChat: (){
  272. Navigator.of(context).push(
  273. new MaterialPageRoute(
  274. builder: (context) {
  275. return ChatPage(
  276. key: Key('Chat'),
  277. friendId: friendID,
  278. isTranslateButler: true,
  279. );
  280. },
  281. ),
  282. );
  283. });
  284. }else{
  285. Navigator.of(context).push(
  286. new MaterialPageRoute(
  287. builder: (context) {
  288. return ChatPage(
  289. key: Key('Chat'),
  290. friendId: friendID,
  291. isTranslateButler: true,
  292. );
  293. },
  294. ),
  295. );
  296. }
  297. }
  298. msgTestPermission(data) async {
  299. PermissionStatus status =
  300. await LocationPermissions().checkPermissionStatus();
  301. if (status == PermissionStatus.granted) {
  302. if (UserData().hasLocationPermission == false) {
  303. UserData().getCurrentPosition();
  304. }
  305. UserData().hasLocationPermission = true;
  306. } else {
  307. UserData().hasLocationPermission = false;
  308. }
  309. }
  310. receiveAudioChatRequest(args) {
  311. print('处理连麦消息');
  312. int friendId = args;
  313. if (!MsgHandler.isAudioConnect) {
  314. if (LocalNotificationUtil.isBackground) {
  315. print('如果在后台,标记不弹');
  316. MsgHandler.audioChatRequestFriendId = friendId;
  317. } else {
  318. HttpUtil().getUserInfo(friendId, (data) {
  319. var info = UserInfo.fromJson(data);
  320. AppNavigator.pushAudioChatPage(context, info, true);
  321. });
  322. }
  323. }
  324. }
  325. loginOut(args) {
  326. HttpUtil().postLoginOut(context);
  327. SystemEditPageState.userLogout();
  328. showToast(I18n.of(context).otherLogin);
  329. }
  330. initValue() {
  331. _titles = [
  332. I18n.of(context).park,
  333. I18n.of(context).radio,
  334. I18n.of(context).news,
  335. I18n.of(context).me
  336. ];
  337. _navigationViews = [
  338. NavigationIconView(
  339. title: _titles[2],
  340. icon: IconData(
  341. 0xe67c,
  342. fontFamily: 'iconfont',
  343. ),
  344. isShowdot: true,
  345. iconSize: 29),
  346. NavigationIconView(
  347. title: _titles[0],
  348. isShowdot: true,
  349. iconMargin: EdgeInsets.only(top: 2),
  350. type: 5,
  351. icon: IconData(
  352. 0xe66e,
  353. fontFamily: 'iconfont',
  354. ),
  355. iconSize: 26.5),
  356. NavigationIconView(
  357. title: _titles[1],
  358. icon: IconData(
  359. 0xe656,
  360. fontFamily: 'iconfont',
  361. ),
  362. isCenter: true,
  363. iconSize: 31),
  364. NavigationIconView(
  365. title: _titles[1],
  366. isShowdot: true,
  367. icon: IconData(
  368. 0xe60b,
  369. fontFamily: 'iconfont',
  370. ),
  371. type: 6,
  372. iconSize: 26.5),
  373. NavigationIconView(
  374. title: _titles[3],
  375. icon: IconData(
  376. 0xe67b,
  377. fontFamily: 'iconfont',
  378. ),
  379. iconSize: 26.5),
  380. ];
  381. _pages = [
  382. RealTimeHelperPage(),
  383. FriendPage(),
  384. FindPage(),
  385. ConversActionPage(),
  386. // GroupChatListPage(),
  387. ProfilePage(
  388. userId: UserData().basicInfo.userId,
  389. ),
  390. ];
  391. // _navigationViews = [
  392. // NavigationIconView(
  393. // title: _titles[1],
  394. // isShowdot: true,
  395. // icon: IconData(
  396. // 0xe60b,
  397. // fontFamily: 'iconfont',
  398. // ),
  399. // type: 1,
  400. // iconSize: 26.5),
  401. // NavigationIconView(
  402. // title: _titles[0],
  403. // isShowdot: true,
  404. // iconMargin: EdgeInsets.only(top: 2),
  405. // type: 5,
  406. // icon: IconData(
  407. // 0xe663,
  408. // fontFamily: 'iconfont',
  409. // ),
  410. // iconSize: 26.5),
  411. // NavigationIconView(
  412. // title: _titles[1],
  413. // icon: IconData(
  414. // 0xe656,
  415. // fontFamily: 'iconfont',
  416. // ),
  417. // isCenter: true,
  418. // iconSize: 31),
  419. //
  420. // NavigationIconView(
  421. // title: _titles[3],
  422. // isShowdot: true,
  423. // icon: IconData(
  424. // 0xe66e,
  425. // fontFamily: 'iconfont',
  426. // ),
  427. // type: 4,
  428. // iconSize: 26.5),
  429. //
  430. // NavigationIconView(
  431. // title: _titles[3],
  432. // icon: IconData(
  433. // 0xe67b,
  434. // fontFamily: 'iconfont',
  435. // ),
  436. // iconSize: 26.5),
  437. // ];
  438. //
  439. // _pages = [
  440. // ConversActionPage(),
  441. //// RealTimeHelperPage(),
  442. // FriendPage(),
  443. // FindPage(),
  444. //// ConversActionPage(),
  445. // GroupChatListPage(),
  446. // ProfilePage(
  447. // userId: UserData().basicInfo.userId,
  448. // ),
  449. // ];
  450. }
  451. @override
  452. Widget build(BuildContext context) {
  453. initValue();
  454. return WillPopScope(
  455. onWillPop: () async {
  456. if (lastPopTime == null ||
  457. DateTime.now().difference(lastPopTime) > Duration(seconds: 2)) {
  458. lastPopTime = DateTime.now();
  459. showToast(I18n.of(context).confirm_exit);
  460. } else {
  461. lastPopTime = DateTime.now();
  462. await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  463. }
  464. return new Future.value(false);
  465. },
  466. child: Container(
  467. // color: Colors.white,
  468. color: AppColors.NewAppbarBgColor,
  469. child: SafeArea(top: false,child: Scaffold(
  470. // appBar: PreferredSize(
  471. // preferredSize: Size.fromHeight(1),
  472. //// child: AppBar(
  473. //// backgroundColor: AppColors.NewAppbarBgColor,
  474. //// ),
  475. // ),
  476. resizeToAvoidBottomPadding: false,
  477. body: Stack(
  478. alignment: AlignmentDirectional.bottomStart,
  479. children: <Widget>[
  480. Container(
  481. padding: EdgeInsets.only(bottom: 48),
  482. color: Constants.LightGreyBackgroundColor,
  483. child: SafeArea(
  484. top: false,
  485. child: IndexedStack(
  486. index: _currentIndex,
  487. children: _pages,
  488. )),
  489. ),
  490. Positioned(
  491. bottom: 0,
  492. child: Container(
  493. margin: EdgeInsets.only(bottom: 0),
  494. child: Image.asset(
  495. 'assets/images/bg.png',
  496. width: Screen.width,
  497. ),
  498. ),
  499. ),
  500. Positioned(
  501. bottom: 0,
  502. child: Container(
  503. width: Screen.width,
  504. height: 0,
  505. color: Colors.white,
  506. ),
  507. ),
  508. Container(
  509. height: 57,
  510. child: myBottm.BottomNavigationBar(
  511. elevation: 0,
  512. showSelectedLabels: false,
  513. showUnselectedLabels: false,
  514. //iconSize: 23.5,
  515. backgroundColor: Colors
  516. .transparent, //Constants.LightGreyBackgroundColor,
  517. currentIndex: _currentIndex,
  518. type: myBottm.BottomNavigationBarType.fixed,
  519. fixedColor: const Color(AppColors.TabIconActive),
  520. items:
  521. _navigationViews.map((NavigationIconView view) {
  522. return view.item;
  523. }).toList(),
  524. onTap: (int index) {
  525. if (_currentIndex == index) {
  526. //在当前页面点击不刷新
  527. return;
  528. }
  529. //刷新我的界面数据
  530. if (index == 4) {
  531. MessageMgr().emit('update_data');
  532. }
  533. // if (index == 1) {
  534. // MessageMgr().emit('refresh_list_when_no_data');
  535. // }
  536. MessageMgr().emit('update_system');
  537. MessageMgr().emit('do_friend_apply');
  538. // LocalNotificationUtil.instance.cleanAllNotifications();
  539. setState(() {
  540. _currentIndex = index;
  541. UserData().homemainIndex = _currentIndex;
  542. });
  543. })),
  544. ],
  545. ))),
  546. ));
  547. }
  548. }