Hibok
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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