Files
AeroTrack/lib/screens/session_screen.dart

660 lines
24 KiB
Dart

import 'package:flutter/material.dart';
import '../models/canister.dart';
import '../services/storage_service.dart';
import '../services/active_session_service.dart';
class SessionScreen extends StatefulWidget {
final StorageService storageService;
final List<Canister> activeMedications;
const SessionScreen({
super.key,
required this.storageService,
required this.activeMedications,
});
@override
State<SessionScreen> createState() => _SessionScreenState();
}
class _SessionScreenState extends State<SessionScreen> {
final ActiveSessionService _sessionService = ActiveSessionService();
@override
void initState() {
super.initState();
_sessionService.addListener(_onSessionUpdate);
// 若当前未在执行雾化,则开启新的会话
if (!_sessionService.isActive && widget.activeMedications.isNotEmpty) {
final settings = widget.storageService.getSettings();
_sessionService.startNewSession(
medications: widget.activeMedications,
settings: settings,
);
}
}
@override
void dispose() {
_sessionService.removeListener(_onSessionUpdate);
// 注意:退出界面绝不销毁计时器!ActiveSessionService 在后台继续计时与语音提醒
super.dispose();
}
void _onSessionUpdate() {
if (mounted) setState(() {});
}
Future<void> _confirmAbortSession() async {
final confirmed = await showDialog<bool>(
context: context,
builder: (ctx) => AlertDialog(
title: const Text('放弃本次雾化?'),
content: const Text('放弃后将重置当前的雾化与倒计时进度,不会扣除药瓶剩余喷数。'),
actions: [
TextButton(
onPressed: () => Navigator.pop(ctx, false),
child: const Text('继续雾化'),
),
ElevatedButton(
onPressed: () => Navigator.pop(ctx, true),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.redAccent,
foregroundColor: Colors.white,
),
child: const Text('确认放弃'),
),
],
),
);
if (confirmed == true) {
_sessionService.cancelSession();
if (mounted) {
Navigator.of(context).pop(false);
}
}
}
Future<void> _saveAndExit() async {
await _sessionService.confirmAndSave(widget.storageService);
if (mounted) {
Navigator.of(context).pop(true);
}
}
String _formatTime(int totalSeconds) {
final m = (totalSeconds ~/ 60).toString().padLeft(2, '0');
final s = (totalSeconds % 60).toString().padLeft(2, '0');
return '$m:$s';
}
@override
Widget build(BuildContext context) {
if (!_sessionService.isActive) {
return Scaffold(
backgroundColor: const Color(0xFF0F172A),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text('当前无进行中的雾化会话', style: TextStyle(color: Colors.white)),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('返回首页'),
),
],
),
),
);
}
if (_sessionService.isFinished) {
return _buildFinishedView();
}
if (_sessionService.isWaitingForNextMed && _sessionService.nextStep != null) {
return _buildWaitingForNextMedView(_sessionService.nextStep!);
}
final step = _sessionService.currentStep;
if (step == null) {
return _buildFinishedView();
}
final isCross = step.type == StepType.crossDrugWait;
return PopScope(
canPop: true, // 允许返回主页,后台继续计时
child: Scaffold(
backgroundColor: const Color(0xFF0F172A),
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
foregroundColor: Colors.white,
leading: IconButton(
icon: const Icon(Icons.arrow_back),
tooltip: '返回主页 (后台继续计时)',
onPressed: () => Navigator.of(context).pop(),
),
title: Text(
'雾化中 (${_sessionService.currentStepIndex + 1}/${_sessionService.steps.length})',
style: const TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
),
actions: [
TextButton(
onPressed: _confirmAbortSession,
child: const Text('放弃本次', style: TextStyle(color: Colors.redAccent, fontSize: 13)),
),
],
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
child: Column(
children: [
// 顶部步骤徽章
_buildStepBadge(step),
const SizedBox(height: 20),
Text(
step.title,
textAlign: TextAlign.center,
style: const TextStyle(color: Colors.white, fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
Text(
step.subtitle,
textAlign: TextAlign.center,
style: const TextStyle(color: Colors.white60, fontSize: 13),
),
const Spacer(),
// 倒计时核心大表盘
_buildTimerDial(isCross),
const SizedBox(height: 16),
// 动态调节时间微调器
_buildTimeAdjustControls(isCross),
const Spacer(),
// 提示文字
Text(
isCross ? '💡 返回主页计时不会中断,到点将自动响铃与语音提醒' : '💡 返回主页后台持续计时,再次点击即可返回全屏',
textAlign: TextAlign.center,
style: const TextStyle(color: Colors.white38, fontSize: 11),
),
const SizedBox(height: 10),
// 下一步按钮
Row(
children: [
Expanded(
child: ElevatedButton(
onPressed: () => _sessionService.completeCurrentStep(),
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF2563EB),
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 16),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
elevation: 0,
),
child: Text(
_sessionService.currentStepIndex + 1 < _sessionService.steps.length ? '下一步 / 提前进入下一阶段' : '完成雾化',
style: const TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
),
),
),
],
),
],
),
),
),
),
);
}
Widget _buildStepBadge(SessionStep step) {
Color bg = Colors.blue.withValues(alpha: 0.2);
Color fg = Colors.blueAccent;
String label = '';
if (step.type == StepType.inhale) {
if (step.canister?.isSteroid ?? false) {
bg = const Color(0xFF10B981).withValues(alpha: 0.2);
fg = const Color(0xFF34D399);
label = '辅舒酮 (激素吸入)';
} else {
bg = Colors.cyan.withValues(alpha: 0.2);
fg = Colors.cyanAccent;
label = '万托林 (舒张气道)';
}
} else if (step.type == StepType.intraPuffWait) {
label = '同药两喷间隔 (30秒)';
} else if (step.type == StepType.crossDrugWait) {
bg = Colors.amber.withValues(alpha: 0.2);
fg = Colors.amberAccent;
label = '⭐ 多药物间隔中 (后台提醒已就绪)';
}
return Container(
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 6),
decoration: BoxDecoration(
color: bg,
borderRadius: BorderRadius.circular(20),
border: Border.all(color: fg.withValues(alpha: 0.3)),
),
child: Text(
label,
style: TextStyle(color: fg, fontSize: 12, fontWeight: FontWeight.bold),
),
);
}
Widget _buildTimerDial(bool isCross) {
final progress = _sessionService.totalStageSeconds > 0
? (_sessionService.remainingSeconds / _sessionService.totalStageSeconds).clamp(0.0, 1.0)
: 0.0;
return Center(
child: SizedBox(
width: 220,
height: 220,
child: Stack(
alignment: Alignment.center,
children: [
SizedBox(
width: 220,
height: 220,
child: CircularProgressIndicator(
value: progress,
strokeWidth: 8,
backgroundColor: Colors.white10,
valueColor: AlwaysStoppedAnimation<Color>(
isCross ? Colors.amberAccent : const Color(0xFF38BDF8),
),
),
),
Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
_formatTime(_sessionService.remainingSeconds),
style: const TextStyle(
fontSize: 48,
fontWeight: FontWeight.w900,
fontFamily: 'monospace',
color: Colors.white,
letterSpacing: -1,
),
),
Text(
isCross ? '两药间隔到点提示' : '吸入倒计时',
style: const TextStyle(color: Colors.white38, fontSize: 12),
),
],
),
],
),
),
);
}
Widget _buildTimeAdjustControls(bool isCross) {
final delta = isCross ? 60 : 5;
final unitText = isCross ? '1分钟' : '5秒';
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
OutlinedButton.icon(
onPressed: () => _sessionService.adjustCurrentTime(-delta),
style: OutlinedButton.styleFrom(
foregroundColor: Colors.white70,
side: const BorderSide(color: Colors.white24),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
),
icon: const Icon(Icons.remove, size: 16),
label: Text('-$unitText'),
),
const SizedBox(width: 16),
OutlinedButton.icon(
onPressed: () => _sessionService.adjustCurrentTime(delta),
style: OutlinedButton.styleFrom(
foregroundColor: Colors.white70,
side: const BorderSide(color: Colors.white24),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
),
icon: const Icon(Icons.add, size: 16),
label: Text('+$unitText'),
),
],
);
}
Widget _buildWaitingForNextMedView(SessionStep nextStep) {
final nextMed = nextStep.canister;
final nextName = nextMed?.name ?? '第二种药物';
final shortName = nextName.split(' ')[0];
final isSteroid = nextMed?.isSteroid ?? false;
return PopScope(
canPop: true,
child: Scaffold(
backgroundColor: const Color(0xFF0F172A),
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
foregroundColor: Colors.white,
leading: IconButton(
icon: const Icon(Icons.arrow_back),
tooltip: '返回主页',
onPressed: () => Navigator.of(context).pop(),
),
title: const Text(
'两药间隔等待结束',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
actions: [
TextButton(
onPressed: _confirmAbortSession,
child: const Text('放弃本次', style: TextStyle(color: Colors.redAccent, fontSize: 13)),
),
],
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
child: Column(
children: [
// 状态标志
Container(
width: 64,
height: 64,
decoration: BoxDecoration(
color: const Color(0xFF10B981).withValues(alpha: 0.15),
shape: BoxShape.circle,
border: Border.all(color: const Color(0xFF10B981), width: 2),
),
child: const Icon(Icons.check, color: Color(0xFF10B981), size: 36),
),
const SizedBox(height: 16),
const Text(
'两药间隔倒计时已结束',
style: TextStyle(color: Colors.white, fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 6),
const Text(
'气道已充分舒张吸收,已暂停等待手动开始',
style: TextStyle(color: Color(0xFF94A3B8), fontSize: 13),
),
const SizedBox(height: 24),
// 第二组药物卡片
Container(
width: double.infinity,
padding: const EdgeInsets.all(18),
decoration: BoxDecoration(
color: const Color(0xFF1E293B),
borderRadius: BorderRadius.circular(20),
border: Border.all(
color: isSteroid ? const Color(0xFF059669) : const Color(0xFF2563EB),
width: 1.5,
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Container(
width: 40,
height: 40,
decoration: BoxDecoration(
color: isSteroid
? const Color(0xFF10B981).withValues(alpha: 0.2)
: const Color(0xFF2563EB).withValues(alpha: 0.2),
borderRadius: BorderRadius.circular(10),
),
child: Center(
child: Text(
isSteroid ? '辅' : '万',
style: TextStyle(
color: isSteroid ? const Color(0xFF34D399) : const Color(0xFF60A5FA),
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
),
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'准备开始第二组用药',
style: TextStyle(
fontSize: 12,
color: isSteroid ? const Color(0xFF34D399) : const Color(0xFF60A5FA),
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 2),
Text(
nextName,
style: const TextStyle(
fontSize: 16,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
],
),
),
],
),
const SizedBox(height: 14),
const Divider(color: Colors.white12, height: 1),
const SizedBox(height: 14),
Text(
'计划用药:${nextMed?.puffsPerSession ?? 1} 喷 • 单喷吸气时长 ${nextMed?.inhalationSeconds ?? 15} 秒',
style: const TextStyle(fontSize: 13, color: Color(0xFFCBD5E1), fontWeight: FontWeight.w600),
),
const SizedBox(height: 12),
_buildCheckTip('1. 请取下前序药物,换上【$shortName】并充分摇匀'),
_buildCheckTip('2. 将储雾罐面罩紧贴口鼻,保持自然平静呼吸'),
_buildCheckTip('3. 一切就绪后,按压药瓶并点击下方按钮开始吸入'),
],
),
),
const Spacer(),
// 手动开始第二组大按钮
SizedBox(
width: double.infinity,
child: ElevatedButton.icon(
onPressed: () => _sessionService.startNextMedication(),
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF2563EB),
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 18),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
elevation: 4,
),
icon: const Icon(Icons.play_arrow_rounded, size: 26),
label: Text(
'开始吸入【$shortName】第 1 喷',
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
),
),
],
),
),
),
),
);
}
Widget _buildCheckTip(String text) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 3),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('• ', style: TextStyle(color: Color(0xFF38BDF8), fontSize: 13, fontWeight: FontWeight.bold)),
Expanded(
child: Text(
text,
style: const TextStyle(color: Color(0xFF94A3B8), fontSize: 12, height: 1.4),
),
),
],
),
);
}
Widget _buildFinishedView() {
final startTime = _sessionService.sessionStartTime ?? DateTime.now();
final endTime = _sessionService.sessionEndTime ?? DateTime.now();
final duration = endTime.difference(startTime);
final minutes = duration.inMinutes;
final seconds = duration.inSeconds % 60;
final hasSteroid = _sessionService.activeMedications.any((m) => m.isSteroid);
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: const Text('用药完成提醒', style: TextStyle(fontWeight: FontWeight.bold)),
automaticallyImplyLeading: false,
backgroundColor: Colors.white,
elevation: 0,
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
children: [
const SizedBox(height: 10),
Container(
width: 72,
height: 72,
decoration: const BoxDecoration(
color: Color(0xFFD1FAE5),
shape: BoxShape.circle,
),
child: const Icon(Icons.check, color: Color(0xFF059669), size: 40),
),
const SizedBox(height: 16),
const Text(
'本次用药方案已完成!',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 4),
Text(
'总耗时 $minutes 分 $seconds 秒',
style: const TextStyle(color: Colors.grey, fontSize: 13),
),
const SizedBox(height: 24),
// 漱口洗脸 Checklist
if (hasSteroid) ...[
Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: const Color(0xFFFFF1F2),
borderRadius: BorderRadius.circular(16),
border: Border.all(color: const Color(0xFFFECDD3)),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Row(
children: [
Icon(Icons.warning_amber_rounded, color: Color(0xFFE11D48), size: 18),
SizedBox(width: 6),
Text(
'必须完成的收尾工作 (防激素副作用)',
style: TextStyle(color: Color(0xFFBE123C), fontWeight: FontWeight.bold, fontSize: 13),
),
],
),
const SizedBox(height: 10),
CheckboxListTile(
contentPadding: EdgeInsets.zero,
value: _sessionService.rinsedMouth,
activeColor: const Color(0xFF10B981),
title: const Text('清水彻底漱口并吐出 (防鹅口疮/声音嘶哑)', style: TextStyle(fontSize: 13, fontWeight: FontWeight.w600)),
onChanged: (val) => _sessionService.rinsedMouth = val ?? true,
),
CheckboxListTile(
contentPadding: EdgeInsets.zero,
value: _sessionService.washedFace,
activeColor: const Color(0xFF10B981),
title: const Text('温水擦洗面部口鼻区域 (防皮炎)', style: TextStyle(fontSize: 13, fontWeight: FontWeight.w600)),
onChanged: (val) => _sessionService.washedFace = val ?? true,
),
],
),
),
const SizedBox(height: 16),
],
// 扣减清单
Container(
padding: const EdgeInsets.all(14),
decoration: BoxDecoration(
color: const Color(0xFFF8FAFC),
borderRadius: BorderRadius.circular(14),
border: Border.all(color: const Color(0xFFE2E8F0)),
),
child: Column(
children: _sessionService.activeMedications.map((m) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(m.name, style: const TextStyle(fontSize: 13, color: Color(0xFF334155))),
Text('扣除 ${m.puffsPerSession} 喷', style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 13, color: Color(0xFF0F172A))),
],
),
);
}).toList(),
),
),
const Spacer(),
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: _saveAndExit,
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF0F172A),
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 16),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
),
child: const Text('确认并保存记录', style: TextStyle(fontWeight: FontWeight.bold)),
),
),
],
),
),
),
);
}
}