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

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