From c9f478fe25f2b224cf406f91dc6f25464ead0dbc Mon Sep 17 00:00:00 2001 From: shen <> Date: Wed, 28 Oct 2026 00:04:37 -0700 Subject: [PATCH] =?UTF-8?q?feat(ui):=20=E5=8D=87=E7=BA=A7=E4=B8=BA?= =?UTF-8?q?=E5=BA=95=E9=83=A8Tab=E5=AF=BC=E8=88=AA=E6=A0=B7=E5=BC=8F?= =?UTF-8?q?=EF=BC=8C=E5=88=86=E5=88=AB=E6=8F=90=E4=BE=9B=E3=80=90=E9=9B=BE?= =?UTF-8?q?=E5=8C=96=E5=85=A5=E5=8F=A3=E3=80=91=E4=B8=8E=E3=80=90=E5=AE=B6?= =?UTF-8?q?=E5=BA=AD=E8=8D=AF=E7=AE=B1=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/main.dart | 4 +- lib/screens/home_screen.dart | 27 +++---- lib/screens/main_navigation_screen.dart | 93 +++++++++++++++++++++++++ test/medicine_cabinet_test.dart | 3 +- test/widget_test.dart | 30 ++++++++ 5 files changed, 137 insertions(+), 20 deletions(-) create mode 100644 lib/screens/main_navigation_screen.dart diff --git a/lib/main.dart b/lib/main.dart index 2837a4f..f2f6831 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,7 +2,7 @@ import 'package:flutter/material.dart'; import 'services/storage_service.dart'; import 'services/tts_service.dart'; import 'services/audio_service.dart'; -import 'screens/home_screen.dart'; +import 'screens/main_navigation_screen.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); @@ -37,7 +37,7 @@ class AeroTrackApp extends StatelessWidget { centerTitle: false, ), ), - home: HomeScreen(storageService: storageService), + home: MainNavigationScreen(storageService: storageService), ); } } diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart index a39f6ac..b8ac2a3 100644 --- a/lib/screens/home_screen.dart +++ b/lib/screens/home_screen.dart @@ -17,8 +17,13 @@ import '../services/active_session_service.dart'; class HomeScreen extends StatefulWidget { final StorageService storageService; + final VoidCallback? onNavigateToCabinet; - const HomeScreen({super.key, required this.storageService}); + const HomeScreen({ + super.key, + required this.storageService, + this.onNavigateToCabinet, + }); @override State createState() => _HomeScreenState(); @@ -286,22 +291,6 @@ class _HomeScreenState extends State { ], ), actions: [ - IconButton( - icon: const Icon(Icons.medical_services_outlined, color: Color(0xFF059669)), - tooltip: '家庭药箱', - visualDensity: VisualDensity.compact, - onPressed: () async { - await Navigator.of(context).push( - MaterialPageRoute( - builder: (_) => MedicineCabinetScreen( - storageService: widget.storageService, - syncService: SyncService(widget.storageService), - ), - ), - ); - _loadData(); - }, - ), IconButton( icon: const Icon(Icons.sync, color: Color(0xFF2563EB)), tooltip: '局域网同步', @@ -413,6 +402,10 @@ class _HomeScreenState extends State { ), child: InkWell( onTap: () async { + if (widget.onNavigateToCabinet != null) { + widget.onNavigateToCabinet!(); + return; + } await Navigator.of(context).push( MaterialPageRoute( builder: (_) => MedicineCabinetScreen( diff --git a/lib/screens/main_navigation_screen.dart b/lib/screens/main_navigation_screen.dart new file mode 100644 index 0000000..13edb50 --- /dev/null +++ b/lib/screens/main_navigation_screen.dart @@ -0,0 +1,93 @@ +import 'package:flutter/material.dart'; +import '../models/medicine_item.dart'; +import '../services/storage_service.dart'; +import '../services/sync_service.dart'; +import 'home_screen.dart'; +import 'medicine_cabinet_screen.dart'; + +class MainNavigationScreen extends StatefulWidget { + final StorageService storageService; + + const MainNavigationScreen({ + super.key, + required this.storageService, + }); + + @override + State createState() => _MainNavigationScreenState(); +} + +class _MainNavigationScreenState extends State { + int _currentIndex = 0; + late final SyncService _syncService; + + @override + void initState() { + super.initState(); + _syncService = SyncService(widget.storageService); + } + + int _getAttentionCount() { + final meds = widget.storageService.getMedicines(); + return meds.where((m) => + m.healthStatus == MedicineHealthStatus.overdueInUse || + m.healthStatus == MedicineHealthStatus.expired || + m.healthStatus == MedicineHealthStatus.expiringSoon).length; + } + + @override + Widget build(BuildContext context) { + final attentionCount = _getAttentionCount(); + + return Scaffold( + body: IndexedStack( + index: _currentIndex, + children: [ + HomeScreen( + storageService: widget.storageService, + onNavigateToCabinet: () { + setState(() => _currentIndex = 1); + }, + ), + MedicineCabinetScreen( + storageService: widget.storageService, + syncService: _syncService, + ), + ], + ), + bottomNavigationBar: NavigationBar( + selectedIndex: _currentIndex, + onDestinationSelected: (index) { + setState(() => _currentIndex = index); + }, + backgroundColor: Colors.white, + elevation: 3, + indicatorColor: const Color(0xFFEFF6FF), + destinations: [ + const NavigationDestination( + icon: Icon(Icons.air_outlined, color: Color(0xFF64748B)), + selectedIcon: Icon(Icons.air, color: Color(0xFF2563EB)), + label: '雾化入口', + ), + NavigationDestination( + icon: attentionCount > 0 + ? Badge( + label: Text('$attentionCount'), + backgroundColor: const Color(0xFFEF4444), + child: const Icon(Icons.medical_services_outlined, color: Color(0xFF64748B)), + ) + : const Icon(Icons.medical_services_outlined, color: Color(0xFF64748B)), + selectedIcon: attentionCount > 0 + ? Badge( + label: Text('$attentionCount'), + backgroundColor: const Color(0xFFEF4444), + child: const Icon(Icons.medical_services, color: Color(0xFF2563EB)), + ) + : const Icon(Icons.medical_services, color: Color(0xFF2563EB)), + label: '家庭药箱', + ), + ], + ), + ); + } +} diff --git a/test/medicine_cabinet_test.dart b/test/medicine_cabinet_test.dart index 17faaca..ddcb00a 100644 --- a/test/medicine_cabinet_test.dart +++ b/test/medicine_cabinet_test.dart @@ -38,7 +38,8 @@ void main() { ); expect(med.healthStatus, MedicineHealthStatus.expiringSoon); - expect(med.healthStatusLabel, contains('即将到期 (15天)')); + expect(med.healthStatusLabel, contains('即将到期')); + expect(med.daysUntilExpiry, inInclusiveRange(14, 15)); }); test('Medicine past expiryDate triggers expired', () { diff --git a/test/widget_test.dart b/test/widget_test.dart index 2fb50e6..10055c7 100644 --- a/test/widget_test.dart +++ b/test/widget_test.dart @@ -1,8 +1,11 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'package:aero_track/models/canister.dart'; import 'package:aero_track/models/app_settings.dart'; +import 'package:aero_track/services/storage_service.dart'; import 'package:aero_track/widgets/puff_progress_ring.dart'; +import 'package:aero_track/screens/main_navigation_screen.dart'; void main() { TestWidgetsFlutterBinding.ensureInitialized(); @@ -75,4 +78,31 @@ void main() { expect(list.first.name, '万托林'); expect(list.last.name, '辅舒酮'); }); + + testWidgets('Bottom Navigation Bar renders 雾化入口 and 家庭药箱 tabs and switches correctly', (WidgetTester tester) async { + SharedPreferences.setMockInitialValues({}); + final storageService = await StorageService.init(); + + await tester.pumpWidget( + MaterialApp( + home: MainNavigationScreen(storageService: storageService), + ), + ); + await tester.pumpAndSettle(); + + // 底部必须出现两个核心 Tab + expect(find.text('雾化入口'), findsOneWidget); + expect(find.text('家庭药箱'), findsOneWidget); + + // 默认展示雾化打卡主页内容 + expect(find.text('舒雾记'), findsOneWidget); + + // 点击【家庭药箱】Tab + await tester.tap(find.text('家庭药箱')); + await tester.pumpAndSettle(); + + // 页面无缝切换到家庭药箱模块 + expect(find.text('家庭智能药箱'), findsOneWidget); + expect(find.text('扫码录药'), findsOneWidget); + }); }