Hibok
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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