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

143 řádky
4.2 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. TaobaoAuth.initAuth(context);
  34. super.initState();
  35. }
  36. @override
  37. Widget build(BuildContext context) {
  38. ScreenUtil.init(context, width: 750, height: 1334);
  39. print('home_page build');
  40. List<Map<String, dynamic>> tabs = _data;
  41. if (tabs == null || tabs.length == 0) {
  42. return Scaffold();
  43. }
  44. List<Widget> contentWidgets = tabs.map((item) {
  45. BaseTabModel model = BaseTabModel.fromJson(item);
  46. return PageFactory.create(model.skipIdentifier, item);
  47. }).toList();
  48. if (_currentIndex >= contentWidgets.length) {
  49. _currentIndex = 0;
  50. }
  51. return Scaffold(
  52. body: IndexedStack(
  53. index: _currentIndex,
  54. children: contentWidgets,
  55. ),
  56. //底部导航栏
  57. bottomNavigationBar: createBottomNavigationBar(tabs),
  58. );
  59. }
  60. Widget createBottomNavigationBar(List<Map<String, dynamic>> tabs) {
  61. List<BottomNavigationBarItem> items = List<BottomNavigationBarItem>();
  62. for (int i = 0; i < tabs.length; i++) {
  63. BaseTabModel model = BaseTabModel.fromJson(tabs[i]);
  64. String icon = ImageUtil.getUrl(model.icon);
  65. String selectedIcon = ImageUtil.getUrl(model.chooseIcon ?? model.icon);
  66. String textColor = model.fontColor;
  67. String chooseColor = model.chooseColor ?? textColor;
  68. items.add(BottomNavigationBarItem(
  69. icon: Container(
  70. width: 24,
  71. height: 24,
  72. margin: EdgeInsets.only(bottom: 4),
  73. child: CachedNetworkImage(
  74. imageUrl: _currentIndex == i ? selectedIcon : icon,
  75. fit: BoxFit.fitWidth,
  76. ),
  77. ),
  78. title: Text(
  79. model.name,
  80. style: TextStyle(
  81. fontSize: 11,
  82. color: HexColor.fromHex(
  83. _currentIndex == i ? chooseColor : textColor)),
  84. )));
  85. }
  86. if (items.length < 2) {
  87. return Container();
  88. }
  89. String bgColor = '#ffffff';
  90. if (tabs.first != null) {
  91. BaseTabModel model = BaseTabModel.fromJson(tabs.first);
  92. bgColor = model.bgColor ?? bgColor;
  93. }
  94. return BottomNavigationBar(
  95. backgroundColor: HexColor.fromHex(bgColor),
  96. type: BottomNavigationBarType.fixed,
  97. selectedFontSize: 11,
  98. unselectedFontSize: 11,
  99. currentIndex: _currentIndex,
  100. elevation: 0,
  101. onTap: ((index) async {
  102. BaseTabModel model = BaseTabModel.fromJson(tabs[index]);
  103. if (await _checkLimit(model)) {
  104. setState(() {
  105. _currentIndex = index;
  106. });
  107. }
  108. }),
  109. //底部导航栏
  110. items: items);
  111. }
  112. Future<bool> _checkLimit(BaseTabModel model) async {
  113. if (model.requiredLogin == '1') {
  114. UserInfoModel user =
  115. await Provider.of<UserInfoNotifier>(context, listen: false)
  116. .getUserInfoModel();
  117. print(user.toString());
  118. if (user?.token == null || user.token == '') {
  119. print('need login...');
  120. RouterUtil.goLogin(context);
  121. return false;
  122. }
  123. }
  124. return true;
  125. }
  126. }