Hibok
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

534 righe
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. //
  337. // _pages = [
  338. // RealTimeHelperPage(),
  339. // FriendPage(),
  340. // FindPage(),
  341. // ConversActionPage(),
  342. //// GroupChatListPage(),
  343. // ProfilePage(
  344. // userId: UserData().basicInfo.userId,
  345. // ),
  346. // ];
  347. _navigationViews = [
  348. NavigationIconView(
  349. title: _titles[1],
  350. isShowdot: true,
  351. icon: IconData(
  352. 0xe60b,
  353. fontFamily: 'iconfont',
  354. ),
  355. type: 1,
  356. iconSize: 26.5),
  357. NavigationIconView(
  358. title: _titles[0],
  359. isShowdot: true,
  360. iconMargin: EdgeInsets.only(top: 2),
  361. type: 5,
  362. icon: IconData(
  363. 0xe663,
  364. fontFamily: 'iconfont',
  365. ),
  366. iconSize: 26.5),
  367. NavigationIconView(
  368. title: _titles[1],
  369. icon: IconData(
  370. 0xe656,
  371. fontFamily: 'iconfont',
  372. ),
  373. isCenter: true,
  374. iconSize: 31),
  375. NavigationIconView(
  376. title: _titles[3],
  377. icon: IconData(
  378. 0xe66e,
  379. fontFamily: 'iconfont',
  380. ),
  381. type: 4,
  382. isShowdot: true,
  383. iconSize: 26.5),
  384. NavigationIconView(
  385. title: _titles[3],
  386. icon: IconData(
  387. 0xe67b,
  388. fontFamily: 'iconfont',
  389. ),
  390. iconSize: 26.5),
  391. ];
  392. _pages = [
  393. ConversActionPage(),
  394. // RealTimeHelperPage(),
  395. FriendPage(),
  396. FindPage(),
  397. // ConversActionPage(),
  398. GroupChatListPage(),
  399. ProfilePage(
  400. userId: UserData().basicInfo.userId,
  401. ),
  402. ];
  403. }
  404. @override
  405. Widget build(BuildContext context) {
  406. initValue();
  407. return WillPopScope(
  408. onWillPop: () async {
  409. if (lastPopTime == null ||
  410. DateTime.now().difference(lastPopTime) > Duration(seconds: 2)) {
  411. lastPopTime = DateTime.now();
  412. showToast(I18n.of(context).confirm_exit);
  413. } else {
  414. lastPopTime = DateTime.now();
  415. await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  416. }
  417. return new Future.value(false);
  418. },
  419. child: Container(
  420. // color: Colors.white,
  421. color: AppColors.NewAppbarBgColor,
  422. child: Scaffold(
  423. appBar: PreferredSize(
  424. preferredSize: Size.fromHeight(1),
  425. child: AppBar(
  426. backgroundColor: AppColors.NewAppbarBgColor,
  427. ),
  428. ),
  429. resizeToAvoidBottomPadding: false,
  430. body: SafeArea(
  431. child: Stack(
  432. alignment: AlignmentDirectional.bottomStart,
  433. children: <Widget>[
  434. Container(
  435. padding: EdgeInsets.only(bottom: 48),
  436. color: Constants.LightGreyBackgroundColor,
  437. child: SafeArea(
  438. child: IndexedStack(
  439. index: _currentIndex,
  440. children: _pages,
  441. )),
  442. ),
  443. Positioned(
  444. bottom: 0,
  445. child: Container(
  446. margin: EdgeInsets.only(bottom: 0),
  447. child: Image.asset(
  448. 'assets/images/bg.png',
  449. width: Screen.width,
  450. ),
  451. ),
  452. ),
  453. Positioned(
  454. bottom: 0,
  455. child: Container(
  456. width: Screen.width,
  457. height: 0,
  458. color: Colors.white,
  459. ),
  460. ),
  461. Container(
  462. height: 57,
  463. child: myBottm.BottomNavigationBar(
  464. elevation: 0,
  465. showSelectedLabels: false,
  466. showUnselectedLabels: false,
  467. //iconSize: 23.5,
  468. backgroundColor: Colors
  469. .transparent, //Constants.LightGreyBackgroundColor,
  470. currentIndex: _currentIndex,
  471. type: myBottm.BottomNavigationBarType.fixed,
  472. fixedColor: const Color(AppColors.TabIconActive),
  473. items:
  474. _navigationViews.map((NavigationIconView view) {
  475. return view.item;
  476. }).toList(),
  477. onTap: (int index) {
  478. if (_currentIndex == index) {
  479. //在当前页面点击不刷新
  480. return;
  481. }
  482. //刷新我的界面数据
  483. if (index == 4) {
  484. MessageMgr().emit('update_data');
  485. }
  486. // if (index == 1) {
  487. // MessageMgr().emit('refresh_list_when_no_data');
  488. // }
  489. MessageMgr().emit('update_system');
  490. MessageMgr().emit('do_friend_apply');
  491. // LocalNotificationUtil.instance.cleanAllNotifications();
  492. setState(() {
  493. _currentIndex = index;
  494. UserData().homemainIndex = _currentIndex;
  495. });
  496. })),
  497. ],
  498. ))),
  499. ));
  500. }
  501. }