Hibok
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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