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

126 lines
3.5 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:provider/provider.dart';
  5. import 'package:zhiying_base_widget/pages/home_page/home_page_bloc.dart';
  6. import 'package:zhiying_base_widget/pages/login_page/notifier/user_info_notifier.dart';
  7. import 'package:zhiying_comm/zhiying_comm.dart';
  8. import 'package:zhiying_comm/util/base_bloc.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. @override
  16. Widget build(BuildContext context) {
  17. print('home_page build');
  18. return MultiProvider(
  19. providers: [
  20. ChangeNotifierProvider.value(value: UserInfoNotifier()),
  21. ],
  22. child: BlocProvider<HomePageBloc>(
  23. bloc: HomePageBloc(),
  24. child: _HomePageContainer(),
  25. ),
  26. );
  27. }
  28. }
  29. class _HomePageContainer extends StatefulWidget {
  30. _HomePageContainer({Key key}) : super(key: key);
  31. @override
  32. _HomePageContainerState createState() => _HomePageContainerState();
  33. }
  34. class _HomePageContainerState extends State<_HomePageContainer> {
  35. HomePageBloc _tabBloc;
  36. int _currentIndex = 0;
  37. @override
  38. void initState() {
  39. _tabBloc = BlocProvider.of<HomePageBloc>(context);
  40. _tabBloc.refresh();
  41. super.initState();
  42. }
  43. @override
  44. Widget build(BuildContext context) {
  45. print('home_page build');
  46. // 屏幕适配初始化
  47. ScreenUtil.init(context, width: 750, height: 1334);
  48. return StreamBuilder<List<Map<String, dynamic>>>(
  49. stream: _tabBloc.outData,
  50. builder: (BuildContext context, AsyncSnapshot snapshot) {
  51. List<Map<String, dynamic>> tabs = snapshot.data;
  52. if (tabs == null || tabs.length == 0) {
  53. return Scaffold();
  54. }
  55. List<Widget> contentWidgets = tabs.map((item) {
  56. WidgetModel model = WidgetModel.fromJson(item);
  57. return PageFactory.create(model.modName, item);
  58. }).toList();
  59. if (_currentIndex >= contentWidgets.length) {
  60. _currentIndex = 0;
  61. }
  62. return Scaffold(
  63. body: IndexedStack(
  64. index: _currentIndex,
  65. children: contentWidgets,
  66. ),
  67. //底部导航栏
  68. bottomNavigationBar: createBottomNavigationBar(tabs),
  69. );
  70. },
  71. );
  72. }
  73. Widget createBottomNavigationBar(List<Map<String, dynamic>> tabs) {
  74. List<BottomNavigationBarItem> items = List<BottomNavigationBarItem>();
  75. for (int i = 0; i < tabs.length; i++) {
  76. WidgetModel model = WidgetModel.fromJson(tabs[i]);
  77. items.add(BottomNavigationBarItem(
  78. icon: Container(
  79. width: 24,
  80. height: 24,
  81. margin: EdgeInsets.only(bottom: 4),
  82. child: CachedNetworkImage(
  83. imageUrl: model.icon,
  84. fit: BoxFit.fitWidth,
  85. ),
  86. ),
  87. title: Text(
  88. model.title,
  89. style: TextStyle(fontSize: 11),
  90. )));
  91. }
  92. if (items.length < 2) {
  93. return Container();
  94. }
  95. return BottomNavigationBar(
  96. backgroundColor: Colors.white,
  97. type: BottomNavigationBarType.fixed,
  98. selectedFontSize: 11,
  99. unselectedFontSize: 11,
  100. currentIndex: _currentIndex,
  101. elevation: 0,
  102. onTap: ((index) {
  103. setState(() {
  104. _currentIndex = index;
  105. });
  106. }),
  107. //底部导航栏
  108. items: items);
  109. }
  110. }