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

279 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. AppUpdateUtil.updateApp(context);
  46. TaobaoAuth.initAuth(context);
  47. // 弹窗
  48. _showPolicy();
  49. Moblink.uploadPrivacyPermissionStatus(1, (bool success) {});
  50. SharesdkPlugin.uploadPrivacyPermissionStatus(1, (bool success) {});
  51. // 是安卓系统,Android场景还原的实现
  52. /*if (defaultTargetPlatform == TargetPlatform.android) {
  53. }*/
  54. //app后台杀死时候的还原
  55. Moblink.restoreScene(_restore);
  56. // 监听开始(传递监听到原生端,用户监听场景还原的数据回传回来)
  57. _eventChannel.receiveBroadcastStream().listen(_onEvent, onError: _onError);
  58. super.initState();
  59. }
  60. @override
  61. void dispose() {
  62. WidgetsBinding.instance.removeObserver(this);
  63. super.dispose();
  64. }
  65. @override
  66. void didChangeAppLifecycleState(AppLifecycleState state) async {
  67. ///智能粘贴板
  68. IntellectCreate.checkAndCreate(state, context);
  69. super.didChangeAppLifecycleState(state);
  70. }
  71. @override
  72. Widget build(BuildContext context) {
  73. ScreenUtil.init(context, width: 750, height: 1334);
  74. Logger.debug('home_page build');
  75. List<Map<String, dynamic>> tabs = _data;
  76. if (tabs == null || tabs.length == 0) {
  77. return Scaffold();
  78. }
  79. List<Widget> contentWidgets = tabs.map((item) {
  80. BaseTabModel model = BaseTabModel.fromJson(item);
  81. return PageFactory.create(model.skipIdentifier, item);
  82. }).toList();
  83. if (_currentIndex >= contentWidgets.length) {
  84. _currentIndex = 0;
  85. }
  86. return Scaffold(
  87. body: IndexedStack(
  88. index: _currentIndex,
  89. children: contentWidgets,
  90. ),
  91. //底部导航栏
  92. bottomNavigationBar: createBottomNavigationBar(tabs),
  93. );
  94. }
  95. Widget createBottomNavigationBar(List<Map<String, dynamic>> tabs) {
  96. List<BottomNavigationBarItem> items = List<BottomNavigationBarItem>();
  97. for (int i = 0; i < tabs.length; i++) {
  98. BaseTabModel model = BaseTabModel.fromJson(tabs[i]);
  99. String icon = ImageUtil.getUrl(model.icon);
  100. String selectedIcon = ImageUtil.getUrl(model.chooseIcon ?? model.icon);
  101. String textColor = model.fontColor;
  102. String chooseColor = model.chooseColor ?? textColor;
  103. if (model.isShow == "1") {
  104. items.add(BottomNavigationBarItem(
  105. icon: Container(
  106. width: 24,
  107. height: 24,
  108. margin: EdgeInsets.only(bottom: 4),
  109. child: CachedNetworkImage(
  110. imageUrl: _currentIndex == i ? selectedIcon : icon,
  111. fit: BoxFit.fitWidth,
  112. ),
  113. ),
  114. title: Text(
  115. model.name,
  116. style: TextStyle(fontSize: 11, color: HexColor.fromHex(_currentIndex == i ? chooseColor : textColor)),
  117. )));
  118. }
  119. }
  120. if (items.length < 2) {
  121. return Container();
  122. }
  123. String bgColor = '#ffffff';
  124. if (tabs.first != null) {
  125. BaseTabModel model = BaseTabModel.fromJson(tabs.first);
  126. bgColor = model.bgColor ?? bgColor;
  127. }
  128. return BottomNavigationBar(
  129. backgroundColor: HexColor.fromHex(bgColor),
  130. type: BottomNavigationBarType.fixed,
  131. selectedFontSize: 11,
  132. unselectedFontSize: 11,
  133. currentIndex: _currentIndex,
  134. elevation: 0,
  135. onTap: ((index) async {
  136. BaseTabModel model = BaseTabModel.fromJson(tabs[index]);
  137. if (await _checkLimit(model)) {
  138. ///避免同一个页面多次点击多次重绘
  139. if(_currentIndex==index){
  140. return;
  141. }else{
  142. setState(() {
  143. _currentIndex = index;
  144. });
  145. }
  146. }
  147. }),
  148. //底部导航栏
  149. items: items);
  150. }
  151. Future<bool> _checkLimit(BaseTabModel model) async {
  152. if (model.requiredLogin == '1') {
  153. UserInfoModel user = await Provider.of<UserInfoNotifier>(context, listen: false).getUserInfoModel();
  154. print(user.toString());
  155. if (user?.token == null || user.token == '') {
  156. print('need login...');
  157. RouterUtil.goLogin(context);
  158. return false;
  159. }
  160. }
  161. return true;
  162. }
  163. ///
  164. /// 各种弹窗
  165. /// 1、用户协议弹窗
  166. /// 2、通知栏开启弹窗
  167. /// 3、活动弹窗
  168. ///
  169. Future _showPolicy() async {
  170. // 协议弹窗
  171. String isShowPolicy = await SharedPreferencesUtil.getStringValue(Constants.isShowPolicy);
  172. if (isShowPolicy == null || isShowPolicy != '1') {
  173. bool isAccept = await PolicyDialog.show(context);
  174. if (!isAccept) {
  175. exit(0);
  176. } else {
  177. await SharedPreferencesUtil.setStringValue(Constants.isShowPolicy, "1");
  178. }
  179. }
  180. // 通知弹窗
  181. String isShowNotiPermission = await SharedPreferencesUtil.getStringValue(Constants.isShowNotiPermission);
  182. if (isShowNotiPermission == null || isShowNotiPermission != '1') {
  183. await NotificationSettingDialog.show(context);
  184. await SharedPreferencesUtil.setStringValue(Constants.isShowNotiPermission, "1");
  185. }
  186. // 活动弹窗
  187. await ActivityDialog.show(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. }