Hibok
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

583 řádky
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/unread_dot_widget.dart';
  10. import 'package:chat/models/UserInfo.dart';
  11. import 'package:chat/models/ref_name_provider.dart';
  12. import 'package:chat/utils/CustomUI.dart';
  13. import 'package:chat/utils/HttpUtil.dart';
  14. import 'package:chat/utils/MessageMgr.dart';
  15. import 'package:chat/utils/TokenMgr.dart';
  16. import 'package:chat/utils/app_navigator.dart';
  17. import 'package:chat/utils/file_preview_local.dart';
  18. import 'package:chat/utils/local_notification_util.dart';
  19. import 'package:chat/utils/msgHandler.dart';
  20. import 'package:chat/utils/receive_share_file.dart';
  21. import 'package:chat/utils/screen.dart';
  22. import 'package:chat/utils/sound_util.dart';
  23. import 'package:chat/utils/version_update_utils.dart';
  24. import 'package:dio/dio.dart';
  25. import 'package:flutter/material.dart';
  26. import 'package:chat/home/ConversActionPage.dart';
  27. import 'package:chat/home/ProfilePage.dart';
  28. import 'package:flutter/services.dart';
  29. import 'package:location_permissions/location_permissions.dart';
  30. import 'package:oktoast/oktoast.dart';
  31. import 'package:provider/provider.dart';
  32. import 'package:shared_preferences/shared_preferences.dart';
  33. import 'package:chat/utils/my_bottom_navigation_bar.dart' as myBottm;
  34. import 'realtimehelper/real_time_helper_page.dart';
  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. _pages = [
  380. RealTimeHelperPage(),
  381. FriendPage(),
  382. FindPage(),
  383. ConversActionPage(),
  384. // GroupChatListPage(),
  385. ProfilePage(
  386. userId: UserData().basicInfo.userId,
  387. ),
  388. ];
  389. // _navigationViews = [
  390. // NavigationIconView(
  391. // title: _titles[1],
  392. // isShowdot: true,
  393. // icon: IconData(
  394. // 0xe60b,
  395. // fontFamily: 'iconfont',
  396. // ),
  397. // type: 1,
  398. // iconSize: 26.5),
  399. // NavigationIconView(
  400. // title: _titles[0],
  401. // isShowdot: true,
  402. // iconMargin: EdgeInsets.only(top: 2),
  403. // type: 5,
  404. // icon: IconData(
  405. // 0xe663,
  406. // fontFamily: 'iconfont',
  407. // ),
  408. // iconSize: 26.5),
  409. // NavigationIconView(
  410. // title: _titles[1],
  411. // icon: IconData(
  412. // 0xe656,
  413. // fontFamily: 'iconfont',
  414. // ),
  415. // isCenter: true,
  416. // iconSize: 31),
  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. // NavigationIconView(
  427. // title: _titles[3],
  428. // icon: IconData(
  429. // 0xe67b,
  430. // fontFamily: 'iconfont',
  431. // ),
  432. // iconSize: 26.5),
  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) async{
  520. if (_currentIndex == index) {
  521. //在当前页面点击不刷新
  522. return;
  523. }
  524. setStatusBar();
  525. //刷新我的界面数据
  526. if (index == 4) {
  527. MessageMgr().emit('update_data');
  528. }
  529. // if (index == 1) {
  530. // MessageMgr().emit('refresh_list_when_no_data');
  531. // }
  532. MessageMgr().emit('update_system');
  533. //MessageMgr().emit('do_friend_apply');
  534. // LocalNotificationUtil.instance.cleanAllNotifications();
  535. setState(() {
  536. _currentIndex = index;
  537. UserData().homemainIndex = _currentIndex;
  538. });
  539. })),
  540. ],
  541. ))),
  542. ));
  543. }
  544. }