Hibok
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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