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

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