Hibok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

534 rivejä
18 KiB

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