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.
 
 
 
 
 
 

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