flutter_integrated_whb/lib/pages/main_tab.dart

174 lines
5.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:qhd_prevention/pages/home/scan_page.dart';
import 'package:qhd_prevention/pages/my_appbar.dart';
import '../customWidget/nfc_test_page.dart';
import '../http/ApiService.dart';
import 'home/home_page.dart';
import 'app/application_page.dart';
import 'mine/mine_page.dart';
import 'notif/notif_page.dart';
class MainPage extends StatefulWidget {
const MainPage({super.key});
@override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
int _currentIndex = 0;
/// 应用中心未读数
int _appBadge = 0;
/// 通知公告未读数
int _notifBadge = 0;
final List<Widget> _pages = const [
HomePage(),
ApplicationPage(),
NotifPage(),
MinePage(),
];
final List<String> _titles = ['首页', '应用中心', '通知公告', '我的'];
@override
void initState() {
super.initState();
_loadBadges();
}
Future<void> _loadBadges() async {
try {
// 并发请求两个接口
final results = await Future.wait([
ApiService.getWork(),
ApiService.getNotifRedPoint(),
]);
final workJson = results[0];
final notifJson = results[1];
setState(() {
final hidCount = workJson['hidCount'] ?? {};
final dys = hidCount['dys'] ?? 0;
final dzg = hidCount['dzg'] ?? 0;
_appBadge = (dys as int) + (dzg as int);
_notifBadge = notifJson['count'] as int? ?? 0;
});
} catch (e) {
print('拉取未读数失败:$e');
}
}
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: EdgeInsets.symmetric(horizontal: 5, vertical: 1),
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(8),
),
constraints: BoxConstraints(minWidth: 16, minHeight: 16),
child: Center(
child: Text(
badgeCount > 99 ? '99+' : '$badgeCount',
style: TextStyle(
color: Colors.white,
fontSize: 11,
height: 1,
),
textAlign: TextAlign.center,
),
)
),
)
],
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: _currentIndex == 1
? null
: MyAppbar(
title: _currentIndex == 0 ? "泰盛安全首页" : _titles[_currentIndex],
backgroundColor: Colors.blue,
isBack: false,
actions: [
if (_currentIndex == 0)
IconButton(
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (_) => NfcTestPage()),
),
icon: Image.asset("assets/images/ai_img.png", width: 20, height: 20),
),
IconButton(
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (_) => ScanPage()),
),
icon: Image.asset("assets/images/scan.png", width: 20, height: 20),
),
],
),
body: _pages[_currentIndex],
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
type: BottomNavigationBarType.fixed,
selectedItemColor: Colors.blue,
unselectedItemColor: Colors.grey,
onTap: (i) => setState(() => _currentIndex = i),
items: [
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: '首页',
),
BottomNavigationBarItem(
icon: _buildIconWithBadge(
icon: Image.asset('assets/tabbar/application.png', width: 24, height: 24),
badgeCount: _appBadge,
),
activeIcon: _buildIconWithBadge(
icon: Image.asset('assets/tabbar/application_cur.png', width: 24, height: 24),
badgeCount: _appBadge,
),
label: '应用',
),
BottomNavigationBarItem(
icon: _buildIconWithBadge(
icon: Image.asset('assets/tabbar/works.png', width: 24, height: 24),
badgeCount: _notifBadge,
),
activeIcon: _buildIconWithBadge(
icon: Image.asset('assets/tabbar/works_cur.png', width: 24, height: 24),
badgeCount: _notifBadge,
),
label: '通知',
),
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: '我的',
),
],
),
);
}
}