import 'package:bot_toast/bot_toast.dart'; import 'package:cloud_car_internal/gen/assets.gen.dart'; import 'package:cloud_car_internal/ui/personal/personal_page.dart'; import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import '../utils/user_tool.dart'; import 'home/home_page.dart'; class TabNavigator extends StatefulWidget { final int? index; const TabNavigator({ super.key, this.index, }); @override _TabNavigatorState createState() => _TabNavigatorState(); } class _TabNavigatorState extends State with SingleTickerProviderStateMixin { TabController? _tabController; int _currentIndex = 0; DateTime? _lastPressed; //页面列表 List get _pages => [ const HomePage(), const PersonalPage(), ]; @override void initState() { super.initState(); _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, //color: Colors.black38, ), activeIcon: Image.asset( selected, height: 44.w, width: 44.w, ), label: title, ); } late bool isOpen = UserTool.userProvider.userInfo.level < 0; @override Widget build(BuildContext context) { //底部导航来 List bottomNav = [ _buildBottomBar( '工作台', Assets.icons.workStageUnselect.path, Assets.icons.workStageSelect.path, ), _buildBottomBar( '我的', Assets.icons.personalUnselect.path, Assets.icons.personalSelect.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: _currentIndex, selectedFontSize: 12.sp, unselectedFontSize: 12.sp, onTap: (index) { _tabController!.animateTo(index, curve: Curves.easeInOutCubic); setState(() => _currentIndex = index); }), ); } }