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

home_page.dart 3.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. ScreenUtil.init(context, width: 750, height: 1334);
  33. print('home_page build');
  34. List<Map<String, dynamic>> tabs = _data;
  35. if (tabs == null || tabs.length == 0) {
  36. return Scaffold();
  37. }
  38. List<Widget> contentWidgets = tabs.map((item) {
  39. BaseTabModel model = BaseTabModel.fromJson(item);
  40. return PageFactory.create(model.skipIdentifier, item);
  41. }).toList();
  42. if (_currentIndex >= contentWidgets.length) {
  43. _currentIndex = 0;
  44. }
  45. return Scaffold(
  46. body: IndexedStack(
  47. index: _currentIndex,
  48. children: contentWidgets,
  49. ),
  50. //底部导航栏
  51. bottomNavigationBar: createBottomNavigationBar(tabs),
  52. );
  53. }
  54. Widget createBottomNavigationBar(List<Map<String, dynamic>> tabs) {
  55. List<BottomNavigationBarItem> items = List<BottomNavigationBarItem>();
  56. for (int i = 0; i < tabs.length; i++) {
  57. BaseTabModel model = BaseTabModel.fromJson(tabs[i]);
  58. String icon = ImageUtil.getUrl(model.icon);
  59. String selectedIcon = ImageUtil.getUrl(model.chooseIcon ?? model.icon);
  60. String textColor = model.fontColor;
  61. String chooseColor = model.chooseColor ?? textColor;
  62. items.add(BottomNavigationBarItem(
  63. icon: Container(
  64. width: 24,
  65. height: 24,
  66. margin: EdgeInsets.only(bottom: 4),
  67. child: CachedNetworkImage(
  68. imageUrl: _currentIndex == i ? selectedIcon : icon,
  69. fit: BoxFit.fitWidth,
  70. ),
  71. ),
  72. title: Text(
  73. model.name,
  74. style: TextStyle(
  75. fontSize: 11,
  76. color: HexColor.fromHex(
  77. _currentIndex == i ? chooseColor : textColor)),
  78. )));
  79. }
  80. if (items.length < 2) {
  81. return Container();
  82. }
  83. String bgColor = '#ffffff';
  84. if (tabs.first != null) {
  85. BaseTabModel model = BaseTabModel.fromJson(tabs.first);
  86. bgColor = model.bgColor ?? bgColor;
  87. }
  88. return BottomNavigationBar(
  89. backgroundColor: HexColor.fromHex(bgColor),
  90. type: BottomNavigationBarType.fixed,
  91. selectedFontSize: 11,
  92. unselectedFontSize: 11,
  93. currentIndex: _currentIndex,
  94. elevation: 0,
  95. onTap: ((index) {
  96. setState(() {
  97. _currentIndex = index;
  98. });
  99. }),
  100. //底部导航栏
  101. items: items);
  102. }
  103. }