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

280 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 =
  31. const EventChannel('JAVA_TO_FLUTTER');
  32. @override
  33. void initState() {
  34. WidgetsBinding.instance.addObserver(this);
  35. String data = BaseSettingModel.setting.tab['data'];
  36. try {
  37. List list = convert.jsonDecode(data);
  38. _data = list.map((item) {
  39. return Map<String, dynamic>.from(item);
  40. }).toList();
  41. Logger.debug(_data);
  42. } catch (error) {
  43. Logger.error(error);
  44. }
  45. Constants.isShowIntellectDialog = false;
  46. AppUpdateUtil.updateApp(context);
  47. TaobaoAuth.initAuth(context);
  48. // 弹窗
  49. _showPolicy();
  50. Moblink.uploadPrivacyPermissionStatus(1, (bool success) {});
  51. SharesdkPlugin.uploadPrivacyPermissionStatus( 1 , (bool success) {});
  52. // 是安卓系统,Android场景还原的实现
  53. /*if (defaultTargetPlatform == TargetPlatform.android) {
  54. }*/
  55. //app后台杀死时候的还原
  56. Moblink.restoreScene(_restore);
  57. // 监听开始(传递监听到原生端,用户监听场景还原的数据回传回来)
  58. _eventChannel.receiveBroadcastStream().listen(_onEvent, onError: _onError);
  59. super.initState();
  60. }
  61. @override
  62. void dispose() {
  63. WidgetsBinding.instance.removeObserver(this);
  64. super.dispose();
  65. }
  66. @override
  67. void didChangeAppLifecycleState(AppLifecycleState state) async {
  68. ///智能粘贴板
  69. IntellectCreate.checkAndCreate(state, context);
  70. super.didChangeAppLifecycleState(state);
  71. }
  72. @override
  73. Widget build(BuildContext context) {
  74. ScreenUtil.init(context, width: 750, height: 1334);
  75. Logger.debug('home_page build');
  76. List<Map<String, dynamic>> tabs = _data;
  77. if (tabs == null || tabs.length == 0) {
  78. return Scaffold();
  79. }
  80. List<Widget> contentWidgets = tabs.map((item) {
  81. BaseTabModel model = BaseTabModel.fromJson(item);
  82. return PageFactory.create(model.skipIdentifier, item);
  83. }).toList();
  84. if (_currentIndex >= contentWidgets.length) {
  85. _currentIndex = 0;
  86. }
  87. return Scaffold(
  88. body: IndexedStack(
  89. index: _currentIndex,
  90. children: contentWidgets,
  91. ),
  92. //底部导航栏
  93. bottomNavigationBar: createBottomNavigationBar(tabs),
  94. );
  95. }
  96. Widget createBottomNavigationBar(List<Map<String, dynamic>> tabs) {
  97. List<BottomNavigationBarItem> items = List<BottomNavigationBarItem>();
  98. for (int i = 0; i < tabs.length; i++) {
  99. BaseTabModel model = BaseTabModel.fromJson(tabs[i]);
  100. String icon = ImageUtil.getUrl(model.icon);
  101. String selectedIcon = ImageUtil.getUrl(model.chooseIcon ?? model.icon);
  102. String textColor = model.fontColor;
  103. String chooseColor = model.chooseColor ?? textColor;
  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(
  117. fontSize: 11,
  118. color: HexColor.fromHex(
  119. _currentIndex == i ? chooseColor : textColor)),
  120. )));
  121. }
  122. if (items.length < 2) {
  123. return Container();
  124. }
  125. String bgColor = '#ffffff';
  126. if (tabs.first != null) {
  127. BaseTabModel model = BaseTabModel.fromJson(tabs.first);
  128. bgColor = model.bgColor ?? bgColor;
  129. }
  130. return BottomNavigationBar(
  131. backgroundColor: HexColor.fromHex(bgColor),
  132. type: BottomNavigationBarType.fixed,
  133. selectedFontSize: 11,
  134. unselectedFontSize: 11,
  135. currentIndex: _currentIndex,
  136. elevation: 0,
  137. onTap: ((index) async {
  138. BaseTabModel model = BaseTabModel.fromJson(tabs[index]);
  139. if (await _checkLimit(model)) {
  140. setState(() {
  141. _currentIndex = index;
  142. });
  143. }
  144. }),
  145. //底部导航栏
  146. items: items);
  147. }
  148. Future<bool> _checkLimit(BaseTabModel model) async {
  149. if (model.requiredLogin == '1') {
  150. UserInfoModel user =
  151. await Provider.of<UserInfoNotifier>(context, listen: false)
  152. .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(
  181. 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) => CupertinoAlertDialog(
  238. title: new Text("提示"),
  239. content: new Text(text),
  240. actions: <Widget>[
  241. new FlatButton(
  242. child: new Text("OK"),
  243. onPressed: () {
  244. Navigator.of(context).pop();
  245. },
  246. )
  247. ]));
  248. }
  249. }