Hibok
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

560 linhas
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/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/proto/transhousekeeper.pb.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().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. 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. CustomUI.buildTranslationHelperOrderDialog(context,false,orderId: data.orderId,scenes: data.scenes,desc: data.desc);
  262. }
  263. startTranslateHKChat(var friendID){
  264. Navigator.of(context).push(
  265. new MaterialPageRoute(
  266. builder: (context) {
  267. return ChatPage(
  268. key: Key('Chat'),
  269. friendId: friendID,
  270. isTranslateButler: true,
  271. );
  272. },
  273. ),
  274. );
  275. }
  276. msgTestPermission(data) async {
  277. PermissionStatus status =
  278. await LocationPermissions().checkPermissionStatus();
  279. if (status == PermissionStatus.granted) {
  280. if (UserData().hasLocationPermission == false) {
  281. UserData().getCurrentPosition();
  282. }
  283. UserData().hasLocationPermission = true;
  284. } else {
  285. UserData().hasLocationPermission = false;
  286. }
  287. }
  288. receiveAudioChatRequest(args) {
  289. print('处理连麦消息');
  290. int friendId = args;
  291. if (!MsgHandler.isAudioConnect) {
  292. if (LocalNotificationUtil.isBackground) {
  293. print('如果在后台,标记不弹');
  294. MsgHandler.audioChatRequestFriendId = friendId;
  295. } else {
  296. HttpUtil().getUserInfo(friendId, (data) {
  297. var info = UserInfo.fromJson(data);
  298. AppNavigator.pushAudioChatPage(context, info, true);
  299. });
  300. }
  301. }
  302. }
  303. loginOut(args) {
  304. HttpUtil().postLoginOut(context);
  305. SystemEditPageState.userLogout();
  306. showToast(I18n.of(context).otherLogin);
  307. }
  308. initValue() {
  309. _titles = [
  310. I18n.of(context).park,
  311. I18n.of(context).radio,
  312. I18n.of(context).news,
  313. I18n.of(context).me
  314. ];
  315. _navigationViews = [
  316. NavigationIconView(
  317. title: _titles[2],
  318. icon: IconData(
  319. 0xe67c,
  320. fontFamily: 'iconfont',
  321. ),
  322. isShowdot: true,
  323. iconSize: 29),
  324. NavigationIconView(
  325. title: _titles[0],
  326. isShowdot: true,
  327. iconMargin: EdgeInsets.only(top: 2),
  328. type: 5,
  329. icon: IconData(
  330. 0xe66e,
  331. fontFamily: 'iconfont',
  332. ),
  333. iconSize: 26.5),
  334. NavigationIconView(
  335. title: _titles[1],
  336. icon: IconData(
  337. 0xe656,
  338. fontFamily: 'iconfont',
  339. ),
  340. isCenter: true,
  341. iconSize: 31),
  342. NavigationIconView(
  343. title: _titles[1],
  344. isShowdot: true,
  345. icon: IconData(
  346. 0xe60b,
  347. fontFamily: 'iconfont',
  348. ),
  349. type: 6,
  350. iconSize: 26.5),
  351. NavigationIconView(
  352. title: _titles[3],
  353. icon: IconData(
  354. 0xe67b,
  355. fontFamily: 'iconfont',
  356. ),
  357. iconSize: 26.5),
  358. ];
  359. _pages = [
  360. RealTimeHelperPage(),
  361. FriendPage(),
  362. FindPage(),
  363. ConversActionPage(),
  364. // GroupChatListPage(),
  365. ProfilePage(
  366. userId: UserData().basicInfo.userId,
  367. ),
  368. ];
  369. // _navigationViews = [
  370. // NavigationIconView(
  371. // title: _titles[1],
  372. // isShowdot: true,
  373. // icon: IconData(
  374. // 0xe60b,
  375. // fontFamily: 'iconfont',
  376. // ),
  377. // type: 1,
  378. // iconSize: 26.5),
  379. // NavigationIconView(
  380. // title: _titles[0],
  381. // isShowdot: true,
  382. // iconMargin: EdgeInsets.only(top: 2),
  383. // type: 5,
  384. // icon: IconData(
  385. // 0xe663,
  386. // fontFamily: 'iconfont',
  387. // ),
  388. // iconSize: 26.5),
  389. // NavigationIconView(
  390. // title: _titles[1],
  391. // icon: IconData(
  392. // 0xe656,
  393. // fontFamily: 'iconfont',
  394. // ),
  395. // isCenter: true,
  396. // iconSize: 31),
  397. //
  398. // NavigationIconView(
  399. // title: _titles[3],
  400. // isShowdot: true,
  401. // icon: IconData(
  402. // 0xe66e,
  403. // fontFamily: 'iconfont',
  404. // ),
  405. // type: 4,
  406. // iconSize: 26.5),
  407. //
  408. // NavigationIconView(
  409. // title: _titles[3],
  410. // icon: IconData(
  411. // 0xe67b,
  412. // fontFamily: 'iconfont',
  413. // ),
  414. // iconSize: 26.5),
  415. // ];
  416. //
  417. // _pages = [
  418. // ConversActionPage(),
  419. //// RealTimeHelperPage(),
  420. // FriendPage(),
  421. // FindPage(),
  422. //// ConversActionPage(),
  423. // GroupChatListPage(),
  424. // ProfilePage(
  425. // userId: UserData().basicInfo.userId,
  426. // ),
  427. // ];
  428. }
  429. @override
  430. Widget build(BuildContext context) {
  431. initValue();
  432. return WillPopScope(
  433. onWillPop: () async {
  434. if (lastPopTime == null ||
  435. DateTime.now().difference(lastPopTime) > Duration(seconds: 2)) {
  436. lastPopTime = DateTime.now();
  437. showToast(I18n.of(context).confirm_exit);
  438. } else {
  439. lastPopTime = DateTime.now();
  440. await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  441. }
  442. return new Future.value(false);
  443. },
  444. child: Container(
  445. // color: Colors.white,
  446. color: AppColors.NewAppbarBgColor,
  447. child: Scaffold(
  448. appBar: PreferredSize(
  449. preferredSize: Size.fromHeight(1),
  450. child: AppBar(
  451. backgroundColor: AppColors.NewAppbarBgColor,
  452. ),
  453. ),
  454. resizeToAvoidBottomPadding: false,
  455. body: SafeArea(
  456. child: Stack(
  457. alignment: AlignmentDirectional.bottomStart,
  458. children: <Widget>[
  459. Container(
  460. padding: EdgeInsets.only(bottom: 48),
  461. color: Constants.LightGreyBackgroundColor,
  462. child: SafeArea(
  463. child: IndexedStack(
  464. index: _currentIndex,
  465. children: _pages,
  466. )),
  467. ),
  468. Positioned(
  469. bottom: 0,
  470. child: Container(
  471. margin: EdgeInsets.only(bottom: 0),
  472. child: Image.asset(
  473. 'assets/images/bg.png',
  474. width: Screen.width,
  475. ),
  476. ),
  477. ),
  478. Positioned(
  479. bottom: 0,
  480. child: Container(
  481. width: Screen.width,
  482. height: 0,
  483. color: Colors.white,
  484. ),
  485. ),
  486. Container(
  487. height: 57,
  488. child: myBottm.BottomNavigationBar(
  489. elevation: 0,
  490. showSelectedLabels: false,
  491. showUnselectedLabels: false,
  492. //iconSize: 23.5,
  493. backgroundColor: Colors
  494. .transparent, //Constants.LightGreyBackgroundColor,
  495. currentIndex: _currentIndex,
  496. type: myBottm.BottomNavigationBarType.fixed,
  497. fixedColor: const Color(AppColors.TabIconActive),
  498. items:
  499. _navigationViews.map((NavigationIconView view) {
  500. return view.item;
  501. }).toList(),
  502. onTap: (int index) {
  503. if (_currentIndex == index) {
  504. //在当前页面点击不刷新
  505. return;
  506. }
  507. //刷新我的界面数据
  508. if (index == 4) {
  509. MessageMgr().emit('update_data');
  510. }
  511. // if (index == 1) {
  512. // MessageMgr().emit('refresh_list_when_no_data');
  513. // }
  514. MessageMgr().emit('update_system');
  515. MessageMgr().emit('do_friend_apply');
  516. // LocalNotificationUtil.instance.cleanAllNotifications();
  517. setState(() {
  518. _currentIndex = index;
  519. UserData().homemainIndex = _currentIndex;
  520. });
  521. })),
  522. ],
  523. ))),
  524. ));
  525. }
  526. }