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

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