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