index_page.dart 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:shared_preferences/shared_preferences.dart';
  4. import 'package:shuqi/public.dart';
  5. import 'package:shuqi/pages/home/home_scene.dart';
  6. import 'package:shuqi/pages/bookshelf/bookshelf_scene.dart';
  7. import 'package:shuqi/pages/me/mine_page.dart';
  8. class IndexPage extends StatefulWidget {
  9. @override
  10. State<StatefulWidget> createState() => IndexPageState();
  11. }
  12. class IndexPageState extends State<IndexPage> {
  13. int _tabIndex = 1;
  14. bool isFinishSetup = false;
  15. List<Image> _tabImages = [
  16. Image.asset('assets/img/tab_bookshelf_n.png'),
  17. Image.asset('assets/img/tab_bookstore_n.png'),
  18. Image.asset('assets/img/tab_me_n.png'),
  19. ];
  20. List<Image> _tabSelectedImages = [
  21. Image.asset('assets/img/tab_bookshelf_p.png'),
  22. Image.asset('assets/img/tab_bookstore_p.png'),
  23. Image.asset('assets/img/tab_me_p.png'),
  24. ];
  25. @override
  26. void initState() {
  27. super.initState();
  28. setupApp();
  29. eventBus.on(EventUserLogin, (arg) {
  30. setState(() {});
  31. });
  32. eventBus.on(EventUserLogout, (arg) {
  33. setState(() {});
  34. });
  35. eventBus.on(EventToggleTabBarIndex, (arg) {
  36. setState(() {
  37. _tabIndex = arg;
  38. });
  39. });
  40. }
  41. @override
  42. void dispose() {
  43. eventBus.off(EventUserLogin);
  44. eventBus.off(EventUserLogout);
  45. eventBus.off(EventToggleTabBarIndex);
  46. super.dispose();
  47. }
  48. setupApp() async {
  49. preferences = await SharedPreferences.getInstance();
  50. setState(() {
  51. isFinishSetup = true;
  52. });
  53. }
  54. @override
  55. Widget build(BuildContext context) {
  56. if (!isFinishSetup) {
  57. return Container();
  58. }
  59. return Scaffold(
  60. body: IndexedStack(
  61. children: <Widget>[
  62. BookshelfScene(),
  63. HomeScene(),
  64. MinePage(),
  65. ],
  66. index: _tabIndex,
  67. ),
  68. bottomNavigationBar: CupertinoTabBar(
  69. backgroundColor: Colors.white,
  70. activeColor: SQColor.primary,
  71. items: <BottomNavigationBarItem>[
  72. BottomNavigationBarItem(icon: getTabIcon(0), label: '书架'),
  73. BottomNavigationBarItem(icon: getTabIcon(1), label: '书城'),
  74. BottomNavigationBarItem(icon: getTabIcon(2), label: '我的'),
  75. ],
  76. currentIndex: _tabIndex,
  77. onTap: (index) {
  78. setState(() {
  79. _tabIndex = index;
  80. });
  81. },
  82. ),
  83. );
  84. }
  85. Image getTabIcon(int index) {
  86. if (index == _tabIndex) {
  87. return _tabSelectedImages[index];
  88. } else {
  89. return _tabImages[index];
  90. }
  91. }
  92. }