You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.8 KiB

2 years ago
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
2 years ago
2 years ago
import 'package:project_telephony/ui/user/user_page.dart';
import 'package:project_telephony/utils/headers.dart';
2 years ago
import '../utils/hive_store.dart';
2 years ago
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<TabNavigator>
with SingleTickerProviderStateMixin {
TabController? _tabController;
int _pageIndex = 0;
DateTime? _lastPressed;
// 页面列表
List<Widget> _pages = <Widget>[];
@override
void initState() {
super.initState();
//页面加载调用
Future.delayed(const Duration(milliseconds: 0), () async {
//Hive.initFlutter;
await HiveStore.init();
2 years ago
});
_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<BottomNavigationBarItem> _bottomNav = <BottomNavigationBarItem>[
_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;
}
2 years ago
2 years ago
// 否则关闭APP
return true;
},
child: TabBarView(
children: _pages,
controller: _tabController,
physics: const NeverScrollableScrollPhysics(),
),
),
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);
},
),
);
}
}