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

116 lines
3.1 KiB

  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. return StreamBuilder<List<Map<String, dynamic>>>(
  40. stream: _tabBloc.outData,
  41. builder: (BuildContext context, AsyncSnapshot snapshot) {
  42. List<Map<String, dynamic>> tabs = snapshot.data;
  43. if (tabs == null || tabs.length == 0) {
  44. return Scaffold();
  45. }
  46. List<Widget> contentWidgets = tabs.map((item) {
  47. WidgetModel model = WidgetModel.fromJson(item);
  48. return PageFactory.create(model.modName, item);
  49. }).toList();
  50. if (_currentIndex >= contentWidgets.length) {
  51. _currentIndex = 0;
  52. }
  53. return Scaffold(
  54. body: IndexedStack(
  55. index: _currentIndex,
  56. children: contentWidgets,
  57. ),
  58. //底部导航栏
  59. bottomNavigationBar: createBottomNavigationBar(tabs),
  60. );
  61. },
  62. );
  63. }
  64. Widget createBottomNavigationBar(List<Map<String, dynamic>> tabs) {
  65. List<BottomNavigationBarItem> items = List<BottomNavigationBarItem>();
  66. for (int i = 0; i < tabs.length; i++) {
  67. WidgetModel model = WidgetModel.fromJson(tabs[i]);
  68. items.add(BottomNavigationBarItem(
  69. icon: Container(
  70. width: 24,
  71. height: 24,
  72. margin: EdgeInsets.only(bottom: 4),
  73. child: CachedNetworkImage(
  74. imageUrl: model.icon,
  75. fit: BoxFit.fitWidth,
  76. ),
  77. ),
  78. title: Text(
  79. model.title,
  80. style: TextStyle(fontSize: 11),
  81. )));
  82. }
  83. if (items.length < 2) {
  84. return Container();
  85. }
  86. return BottomNavigationBar(
  87. backgroundColor: Colors.white,
  88. type: BottomNavigationBarType.fixed,
  89. selectedFontSize: 11,
  90. unselectedFontSize: 11,
  91. currentIndex: _currentIndex,
  92. elevation: 0,
  93. onTap: ((index) {
  94. setState(() {
  95. _currentIndex = index;
  96. });
  97. }),
  98. //底部导航栏
  99. items: items);
  100. }
  101. }