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

186 lines
5.8 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/material.dart';
  6. import 'package:permission_handler/permission_handler.dart';
  7. import 'package:provider/provider.dart';
  8. import 'package:zhiying_base_widget/dialog/global_dialog/intellect_search_goods_dialog/intellect_create.dart';
  9. import 'package:zhiying_base_widget/dialog/global_dialog/notification_setting_dialog/notification_setting_dialog.dart';
  10. import 'package:zhiying_base_widget/dialog/global_dialog/policy_dialog/policy_dialog.dart';
  11. import 'package:zhiying_base_widget/utils/contants.dart';
  12. import 'package:zhiying_comm/models/base/base_tab_model.dart';
  13. import 'package:zhiying_comm/util/image_util.dart';
  14. import 'package:zhiying_comm/util/shared_prefe_util.dart';
  15. import 'package:zhiying_comm/util/update/app_update_util.dart';
  16. import 'package:zhiying_comm/zhiying_comm.dart';
  17. class HomePage extends StatefulWidget {
  18. HomePage({Key key}) : super(key: key);
  19. @override
  20. _HomePageState createState() => _HomePageState();
  21. }
  22. class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
  23. int _currentIndex = 0;
  24. List<Map<String, dynamic>> _data = List();
  25. @override
  26. void initState() {
  27. WidgetsBinding.instance.addObserver(this);
  28. String data = BaseSettingModel.setting.tab['data'];
  29. try {
  30. List list = convert.jsonDecode(data);
  31. _data = list.map((item) {
  32. return Map<String, dynamic>.from(item);
  33. }).toList();
  34. Logger.debug(_data);
  35. } catch (error) {
  36. Logger.error(error);
  37. }
  38. Constants.isShowIntellectDialog = false;
  39. AppUpdateUtil.updateApp(context);
  40. TaobaoAuth.initAuth(context);
  41. _showPolicy();
  42. super.initState();
  43. }
  44. @override
  45. void dispose() {
  46. WidgetsBinding.instance.removeObserver(this);
  47. super.dispose();
  48. }
  49. @override
  50. void didChangeAppLifecycleState(AppLifecycleState state) async {
  51. ///智能粘贴板
  52. IntellectCreate.checkAndCreate(state, context);
  53. super.didChangeAppLifecycleState(state);
  54. }
  55. @override
  56. Widget build(BuildContext context) {
  57. ScreenUtil.init(context, width: 750, height: 1334);
  58. print('home_page build');
  59. List<Map<String, dynamic>> tabs = _data;
  60. if (tabs == null || tabs.length == 0) {
  61. return Scaffold();
  62. }
  63. List<Widget> contentWidgets = tabs.map((item) {
  64. BaseTabModel model = BaseTabModel.fromJson(item);
  65. return PageFactory.create(model.skipIdentifier, item);
  66. }).toList();
  67. if (_currentIndex >= contentWidgets.length) {
  68. _currentIndex = 0;
  69. }
  70. return Scaffold(
  71. body: IndexedStack(
  72. index: _currentIndex,
  73. children: contentWidgets,
  74. ),
  75. //底部导航栏
  76. bottomNavigationBar: createBottomNavigationBar(tabs),
  77. );
  78. }
  79. Widget createBottomNavigationBar(List<Map<String, dynamic>> tabs) {
  80. List<BottomNavigationBarItem> items = List<BottomNavigationBarItem>();
  81. for (int i = 0; i < tabs.length; i++) {
  82. BaseTabModel model = BaseTabModel.fromJson(tabs[i]);
  83. String icon = ImageUtil.getUrl(model.icon);
  84. String selectedIcon = ImageUtil.getUrl(model.chooseIcon ?? model.icon);
  85. String textColor = model.fontColor;
  86. String chooseColor = model.chooseColor ?? textColor;
  87. items.add(BottomNavigationBarItem(
  88. icon: Container(
  89. width: 24,
  90. height: 24,
  91. margin: EdgeInsets.only(bottom: 4),
  92. child: CachedNetworkImage(
  93. imageUrl: _currentIndex == i ? selectedIcon : icon,
  94. fit: BoxFit.fitWidth,
  95. ),
  96. ),
  97. title: Text(
  98. model.name,
  99. style: TextStyle(
  100. fontSize: 11,
  101. color: HexColor.fromHex(
  102. _currentIndex == i ? chooseColor : textColor)),
  103. )));
  104. }
  105. if (items.length < 2) {
  106. return Container();
  107. }
  108. String bgColor = '#ffffff';
  109. if (tabs.first != null) {
  110. BaseTabModel model = BaseTabModel.fromJson(tabs.first);
  111. bgColor = model.bgColor ?? bgColor;
  112. }
  113. return BottomNavigationBar(
  114. backgroundColor: HexColor.fromHex(bgColor),
  115. type: BottomNavigationBarType.fixed,
  116. selectedFontSize: 11,
  117. unselectedFontSize: 11,
  118. currentIndex: _currentIndex,
  119. elevation: 0,
  120. onTap: ((index) async {
  121. BaseTabModel model = BaseTabModel.fromJson(tabs[index]);
  122. if (await _checkLimit(model)) {
  123. setState(() {
  124. _currentIndex = index;
  125. });
  126. }
  127. }),
  128. //底部导航栏
  129. items: items);
  130. }
  131. Future<bool> _checkLimit(BaseTabModel model) async {
  132. if (model.requiredLogin == '1') {
  133. UserInfoModel user =
  134. await Provider.of<UserInfoNotifier>(context, listen: false)
  135. .getUserInfoModel();
  136. print(user.toString());
  137. if (user?.token == null || user.token == '') {
  138. print('need login...');
  139. RouterUtil.goLogin(context);
  140. return false;
  141. }
  142. }
  143. return true;
  144. }
  145. Future _showPolicy() async {
  146. String isShowPolicy =
  147. await SharedPreferencesUtil.getStringValue(Constants.isShowPolicy);
  148. if (isShowPolicy == null || isShowPolicy != '1') {
  149. bool isAccept = await PolicyDialog.show(context);
  150. if (!isAccept) {
  151. exit(0);
  152. } else {
  153. await SharedPreferencesUtil.setStringValue(Constants.isShowPolicy, "1");
  154. }
  155. }
  156. String isShowNotiPermission =
  157. await SharedPreferencesUtil.getStringValue(Constants.isShowNotiPermission);
  158. if (isShowNotiPermission == null || isShowNotiPermission != '1') {
  159. await NotificationSettingDialog.show(context);
  160. await SharedPreferencesUtil.setStringValue(Constants.isShowNotiPermission, "1");
  161. }
  162. IntellectCreate.checkAndCreateFirst(context);
  163. }
  164. }