基础组件库
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.2 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:cached_network_image/cached_network_image.dart';
  4. import 'package:zhiying_base_widget/pages/home_page/home_page_bloc.dart';
  5. import 'package:zhiying_comm/zhiying_comm.dart';
  6. import 'package:zhiying_comm/util/base_bloc.dart';
  7. class HomePage extends StatefulWidget {
  8. HomePage({Key key}) : super(key: key);
  9. @override
  10. _HomePageState createState() => _HomePageState();
  11. }
  12. class _HomePageState extends State<HomePage> {
  13. @override
  14. Widget build(BuildContext context) {
  15. print('home_page build');
  16. return BlocProvider<HomePageBloc>(
  17. bloc: HomePageBloc(),
  18. child: _HomePageContainer(),
  19. );
  20. }
  21. }
  22. class _HomePageContainer extends StatefulWidget {
  23. _HomePageContainer({Key key}) : super(key: key);
  24. @override
  25. _HomePageContainerState createState() => _HomePageContainerState();
  26. }
  27. class _HomePageContainerState extends State<_HomePageContainer> {
  28. HomePageBloc _tabBloc;
  29. int _currentIndex = 0;
  30. @override
  31. void initState() {
  32. _tabBloc = BlocProvider.of<HomePageBloc>(context);
  33. _tabBloc.refresh();
  34. super.initState();
  35. }
  36. @override
  37. Widget build(BuildContext context) {
  38. print('home_page build');
  39. // 屏幕适配初始化
  40. ScreenUtil.init(context, width: 750, height: 1334);
  41. return StreamBuilder<List<Map<String, dynamic>>>(
  42. stream: _tabBloc.outData,
  43. builder: (BuildContext context, AsyncSnapshot snapshot) {
  44. List<Map<String, dynamic>> tabs = snapshot.data;
  45. if (tabs == null || tabs.length == 0) {
  46. return Scaffold();
  47. }
  48. List<Widget> contentWidgets = tabs.map((item) {
  49. WidgetModel model = WidgetModel.fromJson(item);
  50. return PageFactory.create(model.modName, item);
  51. }).toList();
  52. if (_currentIndex >= contentWidgets.length) {
  53. _currentIndex = 0;
  54. }
  55. return Scaffold(
  56. body: IndexedStack(
  57. index: _currentIndex,
  58. children: contentWidgets,
  59. ),
  60. //底部导航栏
  61. bottomNavigationBar: createBottomNavigationBar(tabs),
  62. );
  63. },
  64. );
  65. }
  66. Widget createBottomNavigationBar(List<Map<String, dynamic>> tabs) {
  67. List<BottomNavigationBarItem> items = List<BottomNavigationBarItem>();
  68. for (int i = 0; i < tabs.length; i++) {
  69. WidgetModel model = WidgetModel.fromJson(tabs[i]);
  70. items.add(BottomNavigationBarItem(
  71. icon: Container(
  72. width: 24,
  73. height: 24,
  74. margin: EdgeInsets.only(bottom: 4),
  75. child: CachedNetworkImage(
  76. imageUrl: model.icon,
  77. fit: BoxFit.fitWidth,
  78. ),
  79. ),
  80. title: Text(
  81. model.title,
  82. style: TextStyle(fontSize: 11),
  83. )));
  84. }
  85. if (items.length < 2) {
  86. return Container();
  87. }
  88. return BottomNavigationBar(
  89. backgroundColor: Colors.white,
  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. }