import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:shuqi/public.dart'; import 'package:shuqi/pages/home/home_scene.dart'; import 'package:shuqi/pages/bookshelf/bookshelf_scene.dart'; import 'package:shuqi/pages/me/mine_page.dart'; class IndexPage extends StatefulWidget { @override State createState() => IndexPageState(); } class IndexPageState extends State { int _tabIndex = 1; bool isFinishSetup = false; List _tabImages = [ Image.asset('assets/img/tab_bookshelf_n.png'), Image.asset('assets/img/tab_bookstore_n.png'), Image.asset('assets/img/tab_me_n.png'), ]; List _tabSelectedImages = [ Image.asset('assets/img/tab_bookshelf_p.png'), Image.asset('assets/img/tab_bookstore_p.png'), Image.asset('assets/img/tab_me_p.png'), ]; @override void initState() { super.initState(); setupApp(); eventBus.on(EventUserLogin, (arg) { setState(() {}); }); eventBus.on(EventUserLogout, (arg) { setState(() {}); }); eventBus.on(EventToggleTabBarIndex, (arg) { setState(() { _tabIndex = arg; }); }); } @override void dispose() { eventBus.off(EventUserLogin); eventBus.off(EventUserLogout); eventBus.off(EventToggleTabBarIndex); super.dispose(); } setupApp() async { preferences = await SharedPreferences.getInstance(); setState(() { isFinishSetup = true; }); } @override Widget build(BuildContext context) { if (!isFinishSetup) { return Container(); } return Scaffold( body: IndexedStack( children: [ BookshelfScene(), HomeScene(), MinePage(), ], index: _tabIndex, ), bottomNavigationBar: CupertinoTabBar( backgroundColor: Colors.white, activeColor: SQColor.primary, items: [ BottomNavigationBarItem(icon: getTabIcon(0), label: '书架'), BottomNavigationBarItem(icon: getTabIcon(1), label: '书城'), BottomNavigationBarItem(icon: getTabIcon(2), label: '我的'), ], currentIndex: _tabIndex, onTap: (index) { setState(() { _tabIndex = index; }); }, ), ); } Image getTabIcon(int index) { if (index == _tabIndex) { return _tabSelectedImages[index]; } else { return _tabImages[index]; } } }