基础组件库
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.
 
 
 
 
 

282 lines
8.9 KiB

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