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

149 řádky
4.4 KiB

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