Hibok
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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