基础组件库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

home_page.dart 10 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import 'dart:convert' as convert;
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'package:cached_network_image/cached_network_image.dart';
  5. import 'package:flutter/cupertino.dart';
  6. import 'package:flutter/foundation.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:flutter/services.dart';
  9. import 'package:moblink/moblink.dart';
  10. import 'package:mobpush_plugin/mobpush_custom_message.dart';
  11. import 'package:mobpush_plugin/mobpush_notify_message.dart';
  12. import 'package:mobpush_plugin/mobpush_plugin.dart';
  13. import 'package:provider/provider.dart';
  14. import 'package:zhiying_base_widget/dialog/global_dialog/activity_dialog/activity_dialog.dart';
  15. import 'package:zhiying_base_widget/dialog/global_dialog/intellect_search_goods_dialog/intellect_create.dart';
  16. import 'package:zhiying_base_widget/dialog/global_dialog/notification_setting_dialog/notification_setting_dialog.dart';
  17. import 'package:zhiying_base_widget/dialog/global_dialog/policy_dialog/policy_dialog.dart';
  18. import 'package:zhiying_base_widget/utils/contants.dart';
  19. import 'package:zhiying_base_widget/utils/mob_push_util.dart';
  20. import 'package:zhiying_comm/models/base/base_tab_model.dart';
  21. import 'package:zhiying_comm/util/image_util.dart';
  22. import 'package:zhiying_comm/util/mob_util/mob_util.dart';
  23. import 'package:sharesdk_plugin/sharesdk_plugin.dart';
  24. import 'package:zhiying_comm/util/shared_prefe_util.dart';
  25. import 'package:zhiying_comm/util/update/app_update_util.dart';
  26. import 'package:zhiying_comm/zhiying_comm.dart';
  27. import 'package:zhiying_comm/util/event_util/login_success_event.dart';
  28. import 'package:zhiying_comm/util/event_util/event_util.dart';
  29. import 'package:zhiying_comm/util/event_util/log_out.dart';
  30. class HomePage extends StatefulWidget {
  31. HomePage({Key key}) : super(key: key);
  32. @override
  33. _HomePageState createState() => _HomePageState();
  34. }
  35. class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
  36. int _currentIndex = 0;
  37. List<Map<String, dynamic>> _data = List();
  38. static const EventChannel _eventChannel = const EventChannel('JAVA_TO_FLUTTER');
  39. @override
  40. void initState() {
  41. WidgetsBinding.instance.addObserver(this);
  42. String data = BaseSettingModel.setting.tab['data'];
  43. try {
  44. List list = convert.jsonDecode(data);
  45. _data = list.map((item) {
  46. return Map<String, dynamic>.from(item);
  47. }).toList();
  48. Logger.debug(_data);
  49. } catch (error) {
  50. Logger.error(error);
  51. }
  52. Constants.isShowIntellectDialog = false;
  53. TaobaoAuth.initAuth(context);
  54. // 弹窗
  55. _showPolicy();
  56. Moblink.uploadPrivacyPermissionStatus(1, (bool success) {});
  57. SharesdkPlugin.uploadPrivacyPermissionStatus(1, (bool success) {});
  58. // 是安卓系统,Android场景还原的实现
  59. /*if (defaultTargetPlatform == TargetPlatform.android) {
  60. }*/
  61. //app后台杀死时候的还原
  62. Moblink.restoreScene(_restore);
  63. // 监听开始(传递监听到原生端,用户监听场景还原的数据回传回来)
  64. _eventChannel.receiveBroadcastStream().listen(_onEvent, onError: _onError);
  65. MobPushUtil.addPushReceiver();
  66. EventUtil.instance.on<LoginSuccessEvent>().listen((event) async {
  67. UserInfoModel userInfo = UserInfoNotifier?.staitcUserInfo;
  68. var setting = await NativeUtil.getSetting();
  69. String masterId = setting['master_id'];
  70. Logger.log("我的Alias: " + masterId + "_" + userInfo?.userId);
  71. if (!EmptyUtil.isEmpty(userInfo.userId) && !EmptyUtil.isEmpty(masterId)) {
  72. MobPushUtil.setAlias(masterId + "_" + userInfo.userId);
  73. }
  74. });
  75. EventUtil.instance.on<LogOut>().listen((event) {
  76. MobPushUtil.deleteAlias();
  77. });
  78. super.initState();
  79. }
  80. @override
  81. void dispose() {
  82. WidgetsBinding.instance.removeObserver(this);
  83. super.dispose();
  84. }
  85. @override
  86. void didChangeAppLifecycleState(AppLifecycleState state) {
  87. ///智能粘贴板
  88. IntellectCreate.checkAndCreate(state, context);
  89. super.didChangeAppLifecycleState(state);
  90. }
  91. @override
  92. Widget build(BuildContext context) {
  93. ScreenUtil.init(context, width: 750, height: 1334);
  94. Logger.debug('home_page build');
  95. List<Map<String, dynamic>> tabs = _data;
  96. if (tabs == null || tabs.length == 0) {
  97. return Scaffold();
  98. }
  99. List<Widget> contentWidgets = tabs.map((item) {
  100. BaseTabModel model = BaseTabModel.fromJson(item);
  101. return PageFactory.create(model.skipIdentifier, item);
  102. }).toList();
  103. if (_currentIndex >= contentWidgets.length) {
  104. _currentIndex = 0;
  105. }
  106. return Scaffold(
  107. body: IndexedStack(
  108. index: _currentIndex,
  109. children: contentWidgets,
  110. ),
  111. //底部导航栏
  112. bottomNavigationBar: createBottomNavigationBar(tabs),
  113. );
  114. }
  115. Widget createBottomNavigationBar(List<Map<String, dynamic>> tabs) {
  116. List<BottomNavigationBarItem> items = List<BottomNavigationBarItem>();
  117. for (int i = 0; i < tabs.length; i++) {
  118. BaseTabModel model = BaseTabModel.fromJson(tabs[i]);
  119. String icon = ImageUtil.getUrl(model.icon);
  120. String selectedIcon = ImageUtil.getUrl(model.chooseIcon ?? model.icon);
  121. String textColor = model.fontColor;
  122. String chooseColor = model.chooseColor ?? textColor;
  123. if (model.isShow == "1") {
  124. items.add(BottomNavigationBarItem(
  125. icon: Container(
  126. width: 24,
  127. height: 24,
  128. margin: EdgeInsets.only(bottom: 4),
  129. child: CachedNetworkImage(
  130. imageUrl: _currentIndex == i ? selectedIcon : icon,
  131. fit: BoxFit.fitWidth,
  132. ),
  133. ),
  134. title: Text(
  135. model.name,
  136. style: TextStyle(fontSize: 11, color: HexColor.fromHex(_currentIndex == i ? chooseColor : textColor)),
  137. )));
  138. }
  139. }
  140. if (items.length < 2) {
  141. return Container();
  142. }
  143. String bgColor = '#ffffff';
  144. if (tabs.first != null) {
  145. BaseTabModel model = BaseTabModel.fromJson(tabs.first);
  146. bgColor = model.bgColor ?? bgColor;
  147. }
  148. return BottomNavigationBar(
  149. backgroundColor: HexColor.fromHex(bgColor),
  150. type: BottomNavigationBarType.fixed,
  151. selectedFontSize: 11,
  152. unselectedFontSize: 11,
  153. currentIndex: _currentIndex,
  154. elevation: 0,
  155. onTap: ((index) async {
  156. BaseTabModel model = BaseTabModel.fromJson(tabs[index]);
  157. if (await _checkLimit(model)) {
  158. ///避免同一个页面多次点击多次重绘
  159. if(_currentIndex==index){
  160. return;
  161. }else{
  162. setState(() {
  163. _currentIndex = index;
  164. });
  165. }
  166. }
  167. }),
  168. //底部导航栏
  169. items: items);
  170. }
  171. Future<bool> _checkLimit(BaseTabModel model) async {
  172. if (model.requiredLogin == '1') {
  173. UserInfoModel user = await Provider.of<UserInfoNotifier>(context, listen: false).getUserInfoModel();
  174. print(user.toString());
  175. if (user?.token == null || user.token == '') {
  176. print('need login...');
  177. RouterUtil.goLogin(context);
  178. return false;
  179. }
  180. }
  181. return true;
  182. }
  183. ///
  184. /// 各种弹窗
  185. /// 1、用户协议弹窗
  186. /// 2、通知栏开启弹窗
  187. /// 3、活动弹窗
  188. ///
  189. Future _showPolicy() async {
  190. // 协议弹窗
  191. String isShowPolicy = await SharedPreferencesUtil.getStringValue(Constants.isShowPolicy);
  192. if (isShowPolicy == null || isShowPolicy != '1') {
  193. bool isAccept = await PolicyDialog.show(context);
  194. if (!isAccept) {
  195. exit(0);
  196. } else {
  197. await SharedPreferencesUtil.setStringValue(Constants.isShowPolicy, "1");
  198. }
  199. }
  200. // 通知弹窗
  201. String isShowNotiPermission = await SharedPreferencesUtil.getStringValue(Constants.isShowNotiPermission);
  202. if (isShowNotiPermission == null || isShowNotiPermission != '1') {
  203. await NotificationSettingDialog.show(context);
  204. await SharedPreferencesUtil.setStringValue(Constants.isShowNotiPermission, "1");
  205. }
  206. // 活动弹窗
  207. await ActivityDialog.show(context);
  208. await AppUpdateUtil.updateApp(context);
  209. IntellectCreate.checkAndCreateFirst(context);
  210. }
  211. // 场景还原,记录邀请码
  212. void _restore(MLSDKScene scene) {
  213. // const bool inProduction = const bool.fromEnvironment("dart.vm.product");
  214. // if (!inProduction) {
  215. // // 非release环境,弹窗测试
  216. // showAlert('要还原的路径为:' + scene.className + '\n' + scene.params.toString(),
  217. // context);
  218. // }
  219. Logger.debug('要还原的路径为:' + scene.className);
  220. try {
  221. Map<String, dynamic> params = Map<String, dynamic>.from(scene.params);
  222. if (params.containsKey('tgid')) {
  223. String tgid = params['tgid'].toString();
  224. // 记录邀请码到本地
  225. MobUtil.storeInvitedCode(tgid);
  226. }
  227. } catch (e) {
  228. Logger.error(e);
  229. }
  230. }
  231. //app存在后台时候的还原
  232. void _onEvent(Object event) {
  233. Logger.debug('返回的内容: $event');
  234. try {
  235. if (null != event) {
  236. Map<String, dynamic> params = Map<String, dynamic>.from(event);
  237. // const bool inProduction = const bool.fromEnvironment("dart.vm.product");
  238. // if (!inProduction) {
  239. // // 非release环境,弹窗测试
  240. // showAlert('要还原的路径为[活着]:$event', context);
  241. // }
  242. if (params['params'].containsKey('tgid')) {
  243. String tgid = params['params']['tgid'].toString();
  244. // 记录邀请码到本地
  245. MobUtil.storeInvitedCode(tgid);
  246. }
  247. }
  248. } catch (e) {
  249. Logger.error(e);
  250. }
  251. }
  252. void _onError(Object error) {
  253. Logger.error('返回的错误: $error');
  254. }
  255. void showAlert(String text, BuildContext context) {
  256. showDialog(
  257. context: context,
  258. builder: (BuildContext context) =>
  259. CupertinoAlertDialog(title: new Text("提示"), content: new Text(text), actions: <Widget>[
  260. new FlatButton(
  261. child: new Text("OK"),
  262. onPressed: () {
  263. Navigator.of(context).pop();
  264. },
  265. )
  266. ]));
  267. }
  268. }