Hibok
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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