QinGang_interested/lib/pages/main_tab.dart

261 lines
7.8 KiB
Dart
Raw Normal View History

2025-12-12 09:11:30 +08:00
import 'package:flutter/material.dart';
2026-02-28 16:18:08 +08:00
import 'package:qhd_prevention/pages/badge_manager.dart';
2025-12-12 09:11:30 +08:00
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';
2026-02-28 16:18:08 +08:00
import 'package:qhd_prevention/pages/notif/notif_page.dart';
2025-12-12 09:11:30 +08:00
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>();
2026-02-28 16:18:08 +08:00
final GlobalKey<NotifPageState> _notifKey = GlobalKey<NotifPageState>();
2025-12-12 09:11:30 +08:00
// final GlobalKey<ApplicationPageTwoState> _appKey = GlobalKey<ApplicationPageTwoState>();
final GlobalKey<MinePageState> _mineKey = GlobalKey<MinePageState>();
late List<Widget> _pages;
late List<bool> _tabVisibility; // 存储每个Tab的显示状态
2026-02-28 16:18:08 +08:00
// 添加 BadgeManager 实例
late final BadgeManager _badgeManager;
2025-12-12 09:11:30 +08:00
@override
void initState() {
super.initState();
2026-02-28 16:18:08 +08:00
// 初始化 BadgeManager
_badgeManager = BadgeManager();
_badgeManager.initAllModules();
// 监听 BadgeManager 的变化
_badgeManager.addListener(_onBadgeChanged);
2025-12-12 09:11:30 +08:00
// 注册生命周期监听
WidgetsBinding.instance.addObserver(this);
// 初始化所有Tab
_tabVisibility = [true, true, true];
2026-01-14 09:55:23 +08:00
_pages = <Widget>[
HomePage(key: _homeKey, isChooseFirm: widget.isChooseFirm),
2026-02-28 16:18:08 +08:00
NotifPage(key: _notifKey),
2026-01-14 09:55:23 +08:00
MinePage(key: _mineKey, isChooseFirm: widget.isChooseFirm),
];
2025-12-12 09:11:30 +08:00
// 启动心跳服务
2026-02-28 14:38:07 +08:00
HeartbeatService().start();
2025-12-12 09:11:30 +08:00
}
@override
void dispose() {
2026-02-28 16:18:08 +08:00
_badgeManager.removeListener(_onBadgeChanged);
2025-12-12 09:11:30 +08:00
// 移除生命周期监听
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) {
2026-02-28 16:18:08 +08:00
final bm = _badgeManager;
2025-12-12 09:11:30 +08:00
// 构建可见的底部导航项
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:
2026-02-28 16:18:08 +08:00
visibleItems.add(BottomNavigationBarItem(
icon: _buildIconWithBadge(
icon: Image.asset('assets/tabbar/works.png', width: 24, height: 24),
badgeCount: bm.notifCount,
),
activeIcon: _buildIconWithBadge(
icon: Image.asset('assets/tabbar/works_cur.png', width: 24, height: 24),
badgeCount: bm.notifCount,
),
label: '通知',
));
visiblePages.add(_pages[i]);
break;
case 2:
2025-12-12 09:11:30 +08:00
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;
}
}
}
// 将当前索引映射到可见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隐藏底部导航栏
),
);
}
2026-02-28 16:18:08 +08:00
void _onBadgeChanged() {
// 当角标数据变化时,只更新需要重建的部分
// 但这里我们只需要触发 build 来更新底部导航栏的角标
if (mounted) {
setState(() {
// 只触发重建,不改变数据
});
}
}
2025-12-12 09:11:30 +08:00
}