feat: 严格确保万托林先行吸入,退出会话界面不中断计时(支持后台持续倒计时与主页悬浮卡片)

This commit is contained in:
shen
2026-10-27 06:36:48 -07:00
parent f0f45cfb74
commit 394bb727c7
5 changed files with 883 additions and 513 deletions
+158 -5
View File
@@ -11,6 +11,7 @@ import 'settings_screen.dart';
import 'plan_config_screen.dart';
import 'lan_sync_screen.dart';
import '../services/sync_service.dart';
import '../services/active_session_service.dart';
class HomeScreen extends StatefulWidget {
final StorageService storageService;
@@ -27,10 +28,12 @@ class _HomeScreenState extends State<HomeScreen> {
late List<Canister> _activeCanisters;
late AppSettings _settings;
DoseRecord? _lastRecord;
final ActiveSessionService _sessionService = ActiveSessionService();
@override
void initState() {
super.initState();
_sessionService.addListener(_onSessionUpdate);
_loadData();
// 局域网静默尝试同步拉取最新状态
SyncService(widget.storageService).syncSilently().then((_) {
@@ -38,6 +41,16 @@ class _HomeScreenState extends State<HomeScreen> {
});
}
@override
void dispose() {
_sessionService.removeListener(_onSessionUpdate);
super.dispose();
}
void _onSessionUpdate() {
if (mounted) setState(() {});
}
void _loadData() {
setState(() {
_flixotide = widget.storageService.getPrimaryFlixotide()!;
@@ -60,8 +73,14 @@ class _HomeScreenState extends State<HomeScreen> {
}
}
String _formatTime(int totalSeconds) {
final m = (totalSeconds ~/ 60).toString().padLeft(2, '0');
final s = (totalSeconds % 60).toString().padLeft(2, '0');
return '$m:$s';
}
Future<void> _startSession() async {
if (_activeCanisters.isEmpty) {
if (_activeCanisters.isEmpty && !_sessionService.isActive) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('请先在方案配置中至少勾选一种使用的药物!')),
);
@@ -88,6 +107,8 @@ class _HomeScreenState extends State<HomeScreen> {
),
);
}
} else {
_loadData();
}
}
@@ -291,6 +312,12 @@ class _HomeScreenState extends State<HomeScreen> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// 模块 0:若有后台进行中的雾化,展示醒目实时卡片 (返回主页不中断计时)
if (_sessionService.isActive) ...[
_buildActiveSessionBanner(),
const SizedBox(height: 16),
],
// 模块 1:定时提醒状态条
_buildReminderBar(),
@@ -314,6 +341,124 @@ class _HomeScreenState extends State<HomeScreen> {
);
}
// 0. 进行中雾化实时悬浮卡片 (返回主页不中断计时)
Widget _buildActiveSessionBanner() {
final step = _sessionService.currentStep;
final isWaiting = _sessionService.isWaitingForNextMed;
final isFinished = _sessionService.isFinished;
Color bg;
Color border;
IconData icon;
String title;
String subtitle;
String actionLabel;
if (isFinished) {
bg = const Color(0xFFECFDF5);
border = const Color(0xFF6EE7B7);
icon = Icons.check_circle;
title = '雾化吸入已全部完成!';
subtitle = '请点击进入确认打卡并完成漱口洗脸';
actionLabel = '确认打卡 ↗';
} else if (isWaiting) {
bg = const Color(0xFFFFFBEB);
border = const Color(0xFFFCD34D);
icon = Icons.alarm_on;
final nextName = _sessionService.nextStep?.canister?.name.split(" ")[0] ?? '第二组药物';
title = '两药间隔等待已结束!';
subtitle = '气道已充分舒张,点击手动开始吸入【$nextName】';
actionLabel = '开始第二组 ↗';
} else {
bg = const Color(0xFF0F172A);
border = const Color(0xFF334155);
icon = Icons.hourglass_top_rounded;
title = '雾化计时进行中 • ${step?.title ?? ""}';
subtitle = '剩余 ${_formatTime(_sessionService.remainingSeconds)} • 后台持续计时中';
actionLabel = '全屏界面 ↗';
}
final isDark = !isFinished && !isWaiting;
return InkWell(
onTap: _startSession,
borderRadius: BorderRadius.circular(16),
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
decoration: BoxDecoration(
color: bg,
borderRadius: BorderRadius.circular(16),
border: Border.all(color: border, width: 1.5),
boxShadow: [
BoxShadow(
color: Colors.black.withValues(alpha: 0.04),
blurRadius: 8,
offset: const Offset(0, 3),
),
],
),
child: Row(
children: [
Container(
width: 40,
height: 40,
decoration: BoxDecoration(
color: isDark ? Colors.white10 : Colors.white,
shape: BoxShape.circle,
),
child: Icon(
icon,
color: isFinished
? const Color(0xFF059669)
: (isWaiting ? const Color(0xFFD97706) : const Color(0xFF38BDF8)),
size: 22,
),
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 14,
color: isDark ? Colors.white : (isFinished ? const Color(0xFF065F46) : const Color(0xFF92400E)),
),
),
const SizedBox(height: 2),
Text(
subtitle,
style: TextStyle(
fontSize: 12,
color: isDark ? const Color(0xFF94A3B8) : (isFinished ? const Color(0xFF047857) : const Color(0xFFB45309)),
),
),
],
),
),
Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
decoration: BoxDecoration(
color: isDark ? const Color(0xFF2563EB) : (isFinished ? const Color(0xFF10B981) : const Color(0xFFF59E0B)),
borderRadius: BorderRadius.circular(8),
),
child: Text(
actionLabel,
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 11,
),
),
),
],
),
),
);
}
// 1. 到点提醒状态条
Widget _buildReminderBar() {
final mTime = '${_settings.morningReminderHour.toString().padLeft(2, '0')}:${_settings.morningReminderMinute.toString().padLeft(2, '0')}';
@@ -418,11 +563,11 @@ class _HomeScreenState extends State<HomeScreen> {
const SizedBox(height: 20),
// 主行动按钮:开始雾化
// 主行动按钮:开始雾化 / 继续后台雾化
ElevatedButton(
onPressed: _startSession,
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF2563EB),
backgroundColor: _sessionService.isActive ? const Color(0xFF10B981) : const Color(0xFF2563EB),
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 16),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
@@ -431,10 +576,18 @@ class _HomeScreenState extends State<HomeScreen> {
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.play_circle_fill, size: 22),
Icon(_sessionService.isActive ? Icons.play_arrow_rounded : Icons.play_circle_fill, size: 22),
const SizedBox(width: 8),
Text(
_activeCanisters.length > 1 ? '按方案开始雾化 (带$crossMinutes分钟间隔)' : '开始吸入 ${_activeCanisters.first.name.split(" ")[0]}',
_sessionService.isActive
? (_sessionService.isWaitingForNextMed
? '两药间隔已结束:点击开始第二组'
: (_sessionService.isFinished
? '雾化已完成:点击确认打卡'
: '继续进行中雾化 (${_formatTime(_sessionService.remainingSeconds)})'))
: (_activeCanisters.length > 1
? '按方案开始雾化 (万托林先行 • 带$crossMinutes分钟间隔)'
: '开始吸入 ${_activeCanisters.first.name.split(" ")[0]}'),
style: const TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
),
],