Hibok
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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