Hibok
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

582 rindas
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 'friend_page.dart';
  35. import 'group_chat_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. //
  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. }