tab_page.dart 1.73 KB
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wow_english/pages/home/home_page.dart';
import 'package:wow_english/pages/lessons/lesson_page.dart';

import 'blocs/tab_bloc.dart';

class TabPage extends StatelessWidget {
  const TabPage({super.key});

  final _pages =const <Widget>[
    HomePage(),
    LessonPage()
  ];

  final _tabIcons = const <Icon>[
    Icon(Icons.ac_unit),
    Icon(Icons.ac_unit_outlined)
  ];

  final  _tabTexts = const <String>[
    '页面1',
    '页面2'
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _buildIndexedStack(),
      bottomNavigationBar: _buildBottomNavBars(),
    );
  }

  Widget _buildIndexedStack() => BlocBuilder<TabBloc,TabState>(
      builder: (context, state) => IndexedStack(
        index: state.index,
        children: _pages,
      )
  );

  Widget _buildBottomNavBars() => BlocBuilder<TabBloc,TabState>(
      builder: (context, state) => Container(
        decoration: BoxDecoration(
            boxShadow: <BoxShadow>[
              BoxShadow(
                  color: Theme.of(context).dividerColor,
                  blurRadius: .1
              )
            ]
        ),
        child: BottomNavigationBar(
          currentIndex: state.index,
          items: _buildBottomNavBarItems(context),
          onTap: (value) => context.read<TabBloc>().add(UpdateTabIndexEvent(value)),
        ),
      )
  );

  List<BottomNavigationBarItem> _buildBottomNavBarItems(BuildContext context) => _tabIcons.map((e) {
    final index = _tabIcons.indexOf(e);
    return BottomNavigationBarItem(
        label: _tabTexts[index],
        activeIcon: _tabIcons[index],
        icon: _tabIcons[index]
    );}).toList();
}