import 'package:bot_toast/bot_toast.dart'; import 'package:flutter/material.dart'; import 'package:project_telephony/ui/user/user_page.dart'; import 'package:project_telephony/utils/headers.dart'; import '../utils/hive_store.dart'; import 'home/home_page.dart'; class TabNavigator extends StatefulWidget { final int? index; const TabNavigator({Key? key, this.index}) : super(key: key); @override _TabNavigatorState createState() => _TabNavigatorState(); } class _TabNavigatorState extends State with SingleTickerProviderStateMixin { TabController? _tabController; int _pageIndex = 0; DateTime? _lastPressed; // 页面列表 List _pages = []; @override void initState() { super.initState(); //页面加载调用 Future.delayed(const Duration(milliseconds: 0), () async { //Hive.initFlutter; await HiveStore.init(); }); _pages = [const HomePage(), const UserPage()]; _tabController = TabController( length: _pages.length, vsync: this, initialIndex: widget.index ?? 0); } //选中与未选中图标样式 _buildBottomBar(String title, String unSelected, String selected) { return BottomNavigationBarItem( icon: Image.asset( unSelected, height: 44.w, width: 44.w, ), activeIcon: Image.asset( selected, height: 44.w, width: 44.w, ), label: title, ); } @override Widget build(BuildContext context) { //底部导航来 List _bottomNav = [ _buildBottomBar("首页", Assets.icons.homeNoSelected.path, Assets.icons.homeSelected.path), _buildBottomBar( "我的", Assets.icons.myNoselected.path, Assets.icons.mySelected.path) ]; return Scaffold( body: WillPopScope( onWillPop: () async { if (_lastPressed == null || DateTime.now().difference(_lastPressed!) > const Duration(seconds: 1)) { //两次点击间隔超过1秒重新计算 _lastPressed = DateTime.now(); BotToast.showText(text: '再按一次即可退出程序'); return false; } // 否则关闭APP return true; }, child: TabBarView( controller: _tabController, physics: const NeverScrollableScrollPhysics(), children: _pages, ), ), bottomNavigationBar: BottomNavigationBar( items: _bottomNav, backgroundColor: Colors.white, currentIndex: _pageIndex, selectedFontSize: 20.sp, unselectedFontSize: 20.sp, onTap: (index) { _tabController!.animateTo(index, curve: Curves.easeInOutCubic); setState(() => _pageIndex = index); }, ), ); } }