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

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