Hibok
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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