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

253 lines
7.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/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. // 场景还原,记录邀请码
  57. void _restore(MLSDKScene scene) {
  58. const bool inProduction = const bool.fromEnvironment("dart.vm.product");
  59. if (!inProduction) {
  60. // 非release环境,弹窗测试
  61. showAlert('要还原的路径为:' + scene.className + '\n' + scene.params.toString(),
  62. context);
  63. }
  64. Logger.debug('要还原的路径为:' + scene.className);
  65. try {
  66. Map<String, dynamic> params = Map<String, dynamic>.from(scene.params);
  67. if (params.containsKey('tgid')) {
  68. String tgid = params['tgid'].toString();
  69. // 记录邀请码到本地
  70. SharedPreferencesUtil.setNetCacheResult(Constants.mobInvitedCode, tgid);
  71. }
  72. } catch (e) {
  73. Logger.debug(e);
  74. }
  75. }
  76. @override
  77. void dispose() {
  78. WidgetsBinding.instance.removeObserver(this);
  79. super.dispose();
  80. }
  81. @override
  82. void didChangeAppLifecycleState(AppLifecycleState state) async {
  83. ///智能粘贴板
  84. IntellectCreate.checkAndCreate(state, context);
  85. super.didChangeAppLifecycleState(state);
  86. }
  87. @override
  88. Widget build(BuildContext context) {
  89. ScreenUtil.init(context, width: 750, height: 1334);
  90. Logger.debug('home_page build');
  91. List<Map<String, dynamic>> tabs = _data;
  92. if (tabs == null || tabs.length == 0) {
  93. return Scaffold();
  94. }
  95. List<Widget> contentWidgets = tabs.map((item) {
  96. BaseTabModel model = BaseTabModel.fromJson(item);
  97. return PageFactory.create(model.skipIdentifier, item);
  98. }).toList();
  99. if (_currentIndex >= contentWidgets.length) {
  100. _currentIndex = 0;
  101. }
  102. return Scaffold(
  103. body: IndexedStack(
  104. index: _currentIndex,
  105. children: contentWidgets,
  106. ),
  107. //底部导航栏
  108. bottomNavigationBar: createBottomNavigationBar(tabs),
  109. );
  110. }
  111. Widget createBottomNavigationBar(List<Map<String, dynamic>> tabs) {
  112. List<BottomNavigationBarItem> items = List<BottomNavigationBarItem>();
  113. for (int i = 0; i < tabs.length; i++) {
  114. BaseTabModel model = BaseTabModel.fromJson(tabs[i]);
  115. String icon = ImageUtil.getUrl(model.icon);
  116. String selectedIcon = ImageUtil.getUrl(model.chooseIcon ?? model.icon);
  117. String textColor = model.fontColor;
  118. String chooseColor = model.chooseColor ?? textColor;
  119. items.add(BottomNavigationBarItem(
  120. icon: Container(
  121. width: 24,
  122. height: 24,
  123. margin: EdgeInsets.only(bottom: 4),
  124. child: CachedNetworkImage(
  125. imageUrl: _currentIndex == i ? selectedIcon : icon,
  126. fit: BoxFit.fitWidth,
  127. ),
  128. ),
  129. title: Text(
  130. model.name,
  131. style: TextStyle(
  132. fontSize: 11,
  133. color: HexColor.fromHex(
  134. _currentIndex == i ? chooseColor : textColor)),
  135. )));
  136. }
  137. if (items.length < 2) {
  138. return Container();
  139. }
  140. String bgColor = '#ffffff';
  141. if (tabs.first != null) {
  142. BaseTabModel model = BaseTabModel.fromJson(tabs.first);
  143. bgColor = model.bgColor ?? bgColor;
  144. }
  145. return BottomNavigationBar(
  146. backgroundColor: HexColor.fromHex(bgColor),
  147. type: BottomNavigationBarType.fixed,
  148. selectedFontSize: 11,
  149. unselectedFontSize: 11,
  150. currentIndex: _currentIndex,
  151. elevation: 0,
  152. onTap: ((index) async {
  153. BaseTabModel model = BaseTabModel.fromJson(tabs[index]);
  154. if (await _checkLimit(model)) {
  155. setState(() {
  156. _currentIndex = index;
  157. });
  158. }
  159. }),
  160. //底部导航栏
  161. items: items);
  162. }
  163. Future<bool> _checkLimit(BaseTabModel model) async {
  164. if (model.requiredLogin == '1') {
  165. UserInfoModel user =
  166. await Provider.of<UserInfoNotifier>(context, listen: false)
  167. .getUserInfoModel();
  168. print(user.toString());
  169. if (user?.token == null || user.token == '') {
  170. print('need login...');
  171. RouterUtil.goLogin(context);
  172. return false;
  173. }
  174. }
  175. return true;
  176. }
  177. Future _showPolicy() async {
  178. String isShowPolicy =
  179. await SharedPreferencesUtil.getStringValue(Constants.isShowPolicy);
  180. if (isShowPolicy == null || isShowPolicy != '1') {
  181. bool isAccept = await PolicyDialog.show(context);
  182. if (!isAccept) {
  183. exit(0);
  184. } else {
  185. await SharedPreferencesUtil.setStringValue(Constants.isShowPolicy, "1");
  186. }
  187. }
  188. String isShowNotiPermission = await SharedPreferencesUtil.getStringValue(
  189. Constants.isShowNotiPermission);
  190. if (isShowNotiPermission == null || isShowNotiPermission != '1') {
  191. await NotificationSettingDialog.show(context);
  192. await SharedPreferencesUtil.setStringValue(
  193. Constants.isShowNotiPermission, "1");
  194. }
  195. IntellectCreate.checkAndCreateFirst(context);
  196. }
  197. //app存在后台时候的还原
  198. void _onEvent(Object event) {
  199. Logger.debug('返回的内容: $event');
  200. if (null != event) {
  201. showAlert('要还原的路径为[活着]:$event', context);
  202. }
  203. }
  204. void _onError(Object error) {
  205. Logger.debug('返回的错误');
  206. }
  207. void showAlert(String text, BuildContext context) {
  208. showDialog(
  209. context: context,
  210. builder: (BuildContext context) => CupertinoAlertDialog(
  211. title: new Text("提示"),
  212. content: new Text(text),
  213. actions: <Widget>[
  214. new FlatButton(
  215. child: new Text("OK"),
  216. onPressed: () {
  217. Navigator.of(context).pop();
  218. },
  219. )
  220. ]));
  221. }
  222. }