123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- 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<StatefulWidget> createState() => IndexPageState();
- }
- class IndexPageState extends State<IndexPage> {
- int _tabIndex = 1;
- bool isFinishSetup = false;
- List<Image> _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<Image> _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: <Widget>[
- BookshelfScene(),
- HomeScene(),
- MinePage(),
- ],
- index: _tabIndex,
- ),
- bottomNavigationBar: CupertinoTabBar(
- backgroundColor: Colors.white,
- activeColor: SQColor.primary,
- items: <BottomNavigationBarItem>[
- 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];
- }
- }
- }
|