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

4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. setState(() {
  139. _currentIndex = index;
  140. });
  141. }
  142. }),
  143. //底部导航栏
  144. items: items);
  145. }
  146. Future<bool> _checkLimit(BaseTabModel model) async {
  147. if (model.requiredLogin == '1') {
  148. UserInfoModel user = await Provider.of<UserInfoNotifier>(context, listen: false).getUserInfoModel();
  149. print(user.toString());
  150. if (user?.token == null || user.token == '') {
  151. print('need login...');
  152. RouterUtil.goLogin(context);
  153. return false;
  154. }
  155. }
  156. return true;
  157. }
  158. ///
  159. /// 各种弹窗
  160. /// 1、用户协议弹窗
  161. /// 2、通知栏开启弹窗
  162. /// 3、活动弹窗
  163. ///
  164. Future _showPolicy() async {
  165. // 协议弹窗
  166. String isShowPolicy = await SharedPreferencesUtil.getStringValue(Constants.isShowPolicy);
  167. if (isShowPolicy == null || isShowPolicy != '1') {
  168. bool isAccept = await PolicyDialog.show(context);
  169. if (!isAccept) {
  170. exit(0);
  171. } else {
  172. await SharedPreferencesUtil.setStringValue(Constants.isShowPolicy, "1");
  173. }
  174. }
  175. // 通知弹窗
  176. String isShowNotiPermission = await SharedPreferencesUtil.getStringValue(Constants.isShowNotiPermission);
  177. if (isShowNotiPermission == null || isShowNotiPermission != '1') {
  178. await NotificationSettingDialog.show(context);
  179. await SharedPreferencesUtil.setStringValue(Constants.isShowNotiPermission, "1");
  180. }
  181. // 活动弹窗
  182. await ActivityDialog.show(context);
  183. IntellectCreate.checkAndCreateFirst(context);
  184. }
  185. // 场景还原,记录邀请码
  186. void _restore(MLSDKScene scene) {
  187. // const bool inProduction = const bool.fromEnvironment("dart.vm.product");
  188. // if (!inProduction) {
  189. // // 非release环境,弹窗测试
  190. // showAlert('要还原的路径为:' + scene.className + '\n' + scene.params.toString(),
  191. // context);
  192. // }
  193. Logger.debug('要还原的路径为:' + scene.className);
  194. try {
  195. Map<String, dynamic> params = Map<String, dynamic>.from(scene.params);
  196. if (params.containsKey('tgid')) {
  197. String tgid = params['tgid'].toString();
  198. // 记录邀请码到本地
  199. MobUtil.storeInvitedCode(tgid);
  200. }
  201. } catch (e) {
  202. Logger.error(e);
  203. }
  204. }
  205. //app存在后台时候的还原
  206. void _onEvent(Object event) {
  207. Logger.debug('返回的内容: $event');
  208. try {
  209. if (null != event) {
  210. Map<String, dynamic> params = Map<String, dynamic>.from(event);
  211. // const bool inProduction = const bool.fromEnvironment("dart.vm.product");
  212. // if (!inProduction) {
  213. // // 非release环境,弹窗测试
  214. // showAlert('要还原的路径为[活着]:$event', context);
  215. // }
  216. if (params['params'].containsKey('tgid')) {
  217. String tgid = params['params']['tgid'].toString();
  218. // 记录邀请码到本地
  219. MobUtil.storeInvitedCode(tgid);
  220. }
  221. }
  222. } catch (e) {
  223. Logger.error(e);
  224. }
  225. }
  226. void _onError(Object error) {
  227. Logger.error('返回的错误: $error');
  228. }
  229. void showAlert(String text, BuildContext context) {
  230. showDialog(
  231. context: context,
  232. builder: (BuildContext context) =>
  233. CupertinoAlertDialog(title: new Text("提示"), content: new Text(text), actions: <Widget>[
  234. new FlatButton(
  235. child: new Text("OK"),
  236. onPressed: () {
  237. Navigator.of(context).pop();
  238. },
  239. )
  240. ]));
  241. }
  242. }