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