基础组件库
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

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