Hibok
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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