import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:cached_network_image/cached_network_image.dart'; import 'package:zhiying_base_widget/pages/home_page/home_page_bloc.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; import 'package:zhiying_comm/util/base_bloc.dart'; class HomePage extends StatefulWidget { HomePage({Key key}) : super(key: key); @override _HomePageState createState() => _HomePageState(); } class _HomePageState extends State { @override Widget build(BuildContext context) { print('home_page build'); return BlocProvider( bloc: HomePageBloc(), child: _HomePageContainer(), ); } } class _HomePageContainer extends StatefulWidget { _HomePageContainer({Key key}) : super(key: key); @override _HomePageContainerState createState() => _HomePageContainerState(); } class _HomePageContainerState extends State<_HomePageContainer> { HomePageBloc _tabBloc; int _currentIndex = 0; @override void initState() { _tabBloc = BlocProvider.of(context); _tabBloc.refresh(); super.initState(); } @override Widget build(BuildContext context) { print('home_page build'); // 屏幕适配初始化 ScreenUtil.init(context, width: 750, height: 1334); return StreamBuilder>>( stream: _tabBloc.outData, builder: (BuildContext context, AsyncSnapshot snapshot) { List> tabs = snapshot.data; if (tabs == null || tabs.length == 0) { return Scaffold(); } List contentWidgets = tabs.map((item) { WidgetModel model = WidgetModel.fromJson(item); return PageFactory.create(model.modName, item); }).toList(); if (_currentIndex >= contentWidgets.length) { _currentIndex = 0; } return Scaffold( body: IndexedStack( index: _currentIndex, children: contentWidgets, ), //底部导航栏 bottomNavigationBar: createBottomNavigationBar(tabs), ); }, ); } Widget createBottomNavigationBar(List> tabs) { List items = List(); for (int i = 0; i < tabs.length; i++) { WidgetModel model = WidgetModel.fromJson(tabs[i]); items.add(BottomNavigationBarItem( icon: Container( width: 24, height: 24, margin: EdgeInsets.only(bottom: 4), child: CachedNetworkImage( imageUrl: model.icon, fit: BoxFit.fitWidth, ), ), title: Text( model.title, style: TextStyle(fontSize: 11), ))); } if (items.length < 2) { return Container(); } return BottomNavigationBar( backgroundColor: Colors.white, type: BottomNavigationBarType.fixed, selectedFontSize: 11, unselectedFontSize: 11, currentIndex: _currentIndex, elevation: 0, onTap: ((index) { setState(() { _currentIndex = index; }); }), //底部导航栏 items: items); } }