基础组件库
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

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