221 lines
6.6 KiB
Dart
221 lines
6.6 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:qhd_prevention/pages/home/home_page.dart';
|
||
import 'package:qhd_prevention/pages/mine/mine_page.dart';
|
||
import 'package:qhd_prevention/pages/my_appbar.dart';
|
||
import 'package:qhd_prevention/services/heartbeat_service.dart';
|
||
|
||
/// 用于向子树公布当前 tab 索引
|
||
class CurrentTabNotifier extends InheritedWidget {
|
||
final int currentIndex;
|
||
|
||
const CurrentTabNotifier({
|
||
required this.currentIndex,
|
||
required Widget child,
|
||
Key? key,
|
||
}) : super(key: key, child: child);
|
||
|
||
static CurrentTabNotifier? of(BuildContext context) {
|
||
return context.dependOnInheritedWidgetOfExactType<CurrentTabNotifier>();
|
||
}
|
||
|
||
@override
|
||
bool updateShouldNotify(covariant CurrentTabNotifier oldWidget) {
|
||
return oldWidget.currentIndex != currentIndex;
|
||
}
|
||
}
|
||
|
||
class MainPage extends StatefulWidget {
|
||
const MainPage({Key? key, required this.isChooseFirm}) : super(key: key);
|
||
final bool isChooseFirm;
|
||
|
||
@override
|
||
_MainPageState createState() => _MainPageState();
|
||
}
|
||
|
||
class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
||
int _currentIndex = 0;
|
||
final GlobalKey<HomePageState> _homeKey = GlobalKey<HomePageState>();
|
||
|
||
// final GlobalKey<ApplicationPageTwoState> _appKey = GlobalKey<ApplicationPageTwoState>();
|
||
final GlobalKey<MinePageState> _mineKey = GlobalKey<MinePageState>();
|
||
late List<Widget> _pages;
|
||
late List<bool> _tabVisibility; // 存储每个Tab的显示状态
|
||
|
||
final List<String> _titles = ['首页', '通知', '我的'];
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
// 注册生命周期监听
|
||
WidgetsBinding.instance.addObserver(this);
|
||
// 初始化所有Tab
|
||
_tabVisibility = [true, true, true];
|
||
_pages = <Widget>[HomePage(key: _homeKey, isChooseFirm: widget.isChooseFirm,), MinePage(key: _mineKey)];
|
||
// 启动心跳服务
|
||
// HeartbeatService().start();
|
||
}
|
||
|
||
@override
|
||
void dispose() {
|
||
// BadgeManager().removeListener(_onBadgeChanged);
|
||
// 移除生命周期监听
|
||
WidgetsBinding.instance.removeObserver(this);
|
||
|
||
// 停止心跳服务
|
||
HeartbeatService().stop();
|
||
|
||
super.dispose();
|
||
}
|
||
|
||
@override
|
||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||
super.didChangeAppLifecycleState(state);
|
||
|
||
switch (state) {
|
||
case AppLifecycleState.resumed:
|
||
// 应用回到前台,恢复心跳
|
||
HeartbeatService().resume();
|
||
break;
|
||
case AppLifecycleState.paused:
|
||
case AppLifecycleState.inactive:
|
||
case AppLifecycleState.detached:
|
||
case AppLifecycleState.hidden:
|
||
// 应用进入后台,暂停心跳
|
||
HeartbeatService().pause();
|
||
break;
|
||
}
|
||
}
|
||
|
||
Widget _buildIconWithBadge({required Widget icon, required int badgeCount}) {
|
||
if (badgeCount <= 0) return icon;
|
||
return Stack(
|
||
clipBehavior: Clip.none,
|
||
children: [
|
||
icon,
|
||
Positioned(
|
||
right: -12,
|
||
top: -4,
|
||
child: Container(
|
||
padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 1),
|
||
decoration: BoxDecoration(
|
||
color: Colors.red,
|
||
borderRadius: BorderRadius.circular(8),
|
||
),
|
||
constraints: const BoxConstraints(minWidth: 16, minHeight: 16),
|
||
child: Center(
|
||
child: Text(
|
||
'$badgeCount',
|
||
style: const TextStyle(
|
||
color: Colors.white,
|
||
fontSize: 11,
|
||
height: 1,
|
||
),
|
||
textAlign: TextAlign.center,
|
||
),
|
||
),
|
||
),
|
||
),
|
||
],
|
||
);
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
// final bm = BadgeManager();
|
||
|
||
// 构建可见的底部导航项
|
||
final List<BottomNavigationBarItem> visibleItems = [];
|
||
final List<Widget> visiblePages = [];
|
||
|
||
for (int i = 0; i < _tabVisibility.length; i++) {
|
||
if (_tabVisibility[i]) {
|
||
switch (i) {
|
||
case 0:
|
||
visibleItems.add(
|
||
BottomNavigationBarItem(
|
||
icon: Image.asset(
|
||
'assets/tabbar/basics.png',
|
||
width: 24,
|
||
height: 24,
|
||
),
|
||
activeIcon: Image.asset(
|
||
'assets/tabbar/basics_cur.png',
|
||
width: 24,
|
||
height: 24,
|
||
),
|
||
label: '首页',
|
||
),
|
||
);
|
||
visiblePages.add(_pages[i]);
|
||
break;
|
||
case 1:
|
||
visibleItems.add(
|
||
BottomNavigationBarItem(
|
||
icon: Image.asset(
|
||
'assets/tabbar/my.png',
|
||
width: 24,
|
||
height: 24,
|
||
),
|
||
activeIcon: Image.asset(
|
||
'assets/tabbar/my_cur.png',
|
||
width: 24,
|
||
height: 24,
|
||
),
|
||
label: '我的',
|
||
),
|
||
);
|
||
visiblePages.add(_pages[i]);
|
||
break;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
// 将当前索引映射到可见Tab的索引
|
||
int getVisibleIndex(int originalIndex) {
|
||
int visibleIndex = 0;
|
||
for (int i = 0; i <= originalIndex; i++) {
|
||
if (_tabVisibility[i]) {
|
||
if (i == originalIndex) return visibleIndex;
|
||
visibleIndex++;
|
||
}
|
||
}
|
||
return 0; // 默认返回第一个可见Tab
|
||
}
|
||
|
||
final visibleCurrentIndex = getVisibleIndex(_currentIndex);
|
||
|
||
return CurrentTabNotifier(
|
||
currentIndex: _currentIndex,
|
||
child: Scaffold(
|
||
appBar: null,
|
||
body: IndexedStack(index: visibleCurrentIndex, children: visiblePages),
|
||
bottomNavigationBar:
|
||
visibleItems.length >= 2
|
||
? BottomNavigationBar(
|
||
currentIndex: visibleCurrentIndex,
|
||
type: BottomNavigationBarType.fixed,
|
||
selectedItemColor: Colors.blue,
|
||
unselectedItemColor: Colors.grey,
|
||
onTap: (visibleIndex) {
|
||
int originalIndex = 0;
|
||
int count = 0;
|
||
for (int i = 0; i < _tabVisibility.length; i++) {
|
||
if (_tabVisibility[i]) {
|
||
if (count == visibleIndex) {
|
||
originalIndex = i;
|
||
break;
|
||
}
|
||
count++;
|
||
}
|
||
}
|
||
setState(() => _currentIndex = originalIndex);
|
||
},
|
||
items: visibleItems,
|
||
)
|
||
: null, // 如果没有可见的Tab,隐藏底部导航栏
|
||
),
|
||
);
|
||
}
|
||
}
|