import 'package:flutter/material.dart'; import '../models/canister.dart'; import '../models/app_settings.dart'; import '../services/storage_service.dart'; class PlanConfigScreen extends StatefulWidget { final StorageService storageService; const PlanConfigScreen({super.key, required this.storageService}); @override State createState() => _PlanConfigScreenState(); } class _PlanConfigScreenState extends State { late List _canisters; late AppSettings _settings; @override void initState() { super.initState(); _load(); } void _load() { setState(() { _canisters = widget.storageService.getCanisters(); _settings = widget.storageService.getSettings(); }); } Future _saveAll() async { await widget.storageService.saveCanisters(_canisters); await widget.storageService.saveSettings(_settings); if (mounted) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('用药方案已更新并保存!'), backgroundColor: Color(0xFF10B981), ), ); Navigator.of(context).pop(true); } } int get _enabledCount => _canisters.where((c) => c.isEnabled).length; @override Widget build(BuildContext context) { final crossMinutes = _settings.crossDrugIntervalSeconds ~/ 60; return Scaffold( backgroundColor: const Color(0xFFF8FAFC), appBar: AppBar( title: const Text('用药方案与参数配置', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16)), backgroundColor: Colors.white, elevation: 0, foregroundColor: const Color(0xFF0F172A), actions: [ TextButton( onPressed: _saveAll, child: const Text('保存', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15, color: Color(0xFF2563EB))), ), ], ), body: ListView( padding: const EdgeInsets.all(20), children: [ // 顶部指引说明 Container( padding: const EdgeInsets.all(14), decoration: BoxDecoration( color: const Color(0xFFEFF6FF), borderRadius: BorderRadius.circular(14), border: Border.all(color: const Color(0xFFBFDBFE)), ), child: const Row( children: [ Icon(Icons.info_outline, color: Color(0xFF2563EB), size: 20), SizedBox(width: 10), Expanded( child: Text( '请勾选当前医嘱使用的药物,并针对每种药物分别配置喷数与雾化时长。', style: TextStyle(fontSize: 12, color: Color(0xFF1E3A8A), height: 1.4), ), ), ], ), ), const SizedBox(height: 20), // 模块 1:药物列表与单药参数 const Text('选择当前使用的药物及参数', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14, color: Color(0xFF475569))), const SizedBox(height: 12), ..._canisters.map((canister) => _buildCanisterCard(canister)), const SizedBox(height: 16), // 模块 2:多药物间隔时间(仅当勾选 >= 2 种药物时显示) if (_enabledCount >= 2) ...[ const Text('多药物联合雾化间隔时间', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14, color: Color(0xFF475569))), const SizedBox(height: 12), Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(18), border: Border.all(color: const Color(0xFFFDE68A)), boxShadow: [ BoxShadow(color: Colors.black.withValues(alpha: 0.02), blurRadius: 6, offset: const Offset(0, 2)), ], ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ const Expanded( child: Row( children: [ Icon(Icons.hourglass_top, color: Color(0xFFD97706), size: 20), SizedBox(width: 8), Flexible( child: Text( '两药之间的等待时间', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14, color: Color(0xFF92400E)), ), ), ], ), ), Container( padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4), decoration: BoxDecoration( color: const Color(0xFFFEF3C7), borderRadius: BorderRadius.circular(10), ), child: Text( '$crossMinutes 分钟', style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 14, color: Color(0xFFB45309)), ), ), ], ), const SizedBox(height: 8), const Text( '先用万托林充分舒张气道,等待数分钟后气管扩张完全,再吸辅舒酮进入深部肺泡。', style: TextStyle(fontSize: 12, color: Color(0xFF78350F), height: 1.4), ), const SizedBox(height: 16), Row( mainAxisAlignment: MainAxisAlignment.center, children: [ IconButton.filledTonal( onPressed: crossMinutes > 1 ? () => setState(() => _settings.crossDrugIntervalSeconds = (crossMinutes - 1) * 60) : null, icon: const Icon(Icons.remove), ), const SizedBox(width: 20), Text( '$crossMinutes 分钟', style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: Color(0xFF0F172A)), ), const SizedBox(width: 20), IconButton.filledTonal( onPressed: crossMinutes < 15 ? () => setState(() => _settings.crossDrugIntervalSeconds = (crossMinutes + 1) * 60) : null, icon: const Icon(Icons.add), ), ], ), ], ), ), ] else ...[ Container( padding: const EdgeInsets.all(12), decoration: BoxDecoration( color: Colors.grey.shade100, borderRadius: BorderRadius.circular(12), ), child: const Row( children: [ Icon(Icons.check_circle_outline, color: Colors.grey, size: 18), SizedBox(width: 8), Text('当前仅勾选了 1 种药物,无需设置多药间隔时间。', style: TextStyle(color: Colors.grey, fontSize: 12)), ], ), ), ], const SizedBox(height: 32), // 保存按钮 ElevatedButton( onPressed: _saveAll, style: ElevatedButton.styleFrom( backgroundColor: const Color(0xFF2563EB), foregroundColor: Colors.white, padding: const EdgeInsets.symmetric(vertical: 16), shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)), elevation: 1, ), child: const Text('保存并应用方案', style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold)), ), ], ), ); } Widget _buildCanisterCard(Canister canister) { final isFlixotide = canister.isSteroid; return Container( margin: const EdgeInsets.only(bottom: 16), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(18), border: Border.all( color: canister.isEnabled ? const Color(0xFF2563EB) : const Color(0xFFE2E8F0), width: canister.isEnabled ? 1.5 : 1, ), boxShadow: [ BoxShadow( color: Colors.black.withValues(alpha: 0.02), blurRadius: 8, offset: const Offset(0, 2), ), ], ), child: Column( children: [ // 头部:开关与药名 Padding( padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), child: Row( children: [ Container( width: 36, height: 36, decoration: BoxDecoration( color: isFlixotide ? const Color(0xFFECFDF5) : const Color(0xFFEFF6FF), borderRadius: BorderRadius.circular(10), ), child: Center( child: Text( isFlixotide ? '辅' : '万', style: TextStyle( color: isFlixotide ? const Color(0xFF059669) : const Color(0xFF2563EB), fontWeight: FontWeight.bold, fontSize: 15, ), ), ), ), const SizedBox(width: 12), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( canister.name, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 14), ), const SizedBox(height: 2), Text( isFlixotide ? '糖皮质激素 • 每瓶标称60喷' : '支气管舒张剂 • 200喷', style: const TextStyle(color: Colors.grey, fontSize: 11), ), ], ), ), Switch( value: canister.isEnabled, activeTrackColor: const Color(0xFF2563EB), onChanged: (val) { setState(() { canister.isEnabled = val; }); }, ), ], ), ), // 若已启用,展开配置具体参数(喷数、雾化时间) if (canister.isEnabled) ...[ const Divider(height: 1), Padding( padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // 配置 1:每次用药喷数 Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ const Text( '每次用药喷数', style: TextStyle(fontWeight: FontWeight.w600, fontSize: 13, color: Color(0xFF334155)), ), Container( padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2), decoration: BoxDecoration( color: const Color(0xFFEFF6FF), borderRadius: BorderRadius.circular(6), ), child: Text( '${canister.puffsPerSession} 喷', style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 12, color: Color(0xFF2563EB)), ), ), ], ), const SizedBox(height: 10), Row( children: [1, 2, 3, 4].map((puff) { final selected = canister.puffsPerSession == puff; return Expanded( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 3), child: InkWell( onTap: () { setState(() { canister.puffsPerSession = puff; }); }, borderRadius: BorderRadius.circular(10), child: Container( padding: const EdgeInsets.symmetric(vertical: 9), decoration: BoxDecoration( color: selected ? const Color(0xFF2563EB) : const Color(0xFFF8FAFC), borderRadius: BorderRadius.circular(10), border: Border.all( color: selected ? const Color(0xFF2563EB) : const Color(0xFFE2E8F0), width: selected ? 1.5 : 1, ), ), alignment: Alignment.center, child: Text( '$puff 喷', style: TextStyle( color: selected ? Colors.white : const Color(0xFF475569), fontWeight: FontWeight.bold, fontSize: 13, ), ), ), ), ), ); }).toList(), ), const SizedBox(height: 16), // 配置 2:每次雾化/吸气时长 Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Row( children: [ const Text( '单喷雾化吸入时间', style: TextStyle(fontWeight: FontWeight.w600, fontSize: 13, color: Color(0xFF334155)), ), const SizedBox(width: 6), Text( '(面罩密闭吸气)', style: TextStyle(fontSize: 11, color: Colors.grey.shade500), ), ], ), Container( padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2), decoration: BoxDecoration( color: const Color(0xFFF1F5F9), borderRadius: BorderRadius.circular(6), ), child: Text( '${canister.inhalationSeconds} 秒', style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 12, color: Color(0xFF0F172A)), ), ), ], ), const SizedBox(height: 10), Row( children: [10, 15, 30, 60].map((sec) { final selected = canister.inhalationSeconds == sec; return Expanded( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 3), child: InkWell( onTap: () { setState(() { canister.inhalationSeconds = sec; }); }, borderRadius: BorderRadius.circular(10), child: Container( padding: const EdgeInsets.symmetric(vertical: 9), decoration: BoxDecoration( color: selected ? const Color(0xFF0F172A) : const Color(0xFFF8FAFC), borderRadius: BorderRadius.circular(10), border: Border.all( color: selected ? const Color(0xFF0F172A) : const Color(0xFFE2E8F0), width: selected ? 1.5 : 1, ), ), alignment: Alignment.center, child: Text( '$sec 秒', style: TextStyle( color: selected ? Colors.white : const Color(0xFF475569), fontWeight: FontWeight.bold, fontSize: 13, ), ), ), ), ), ); }).toList(), ), ], ), ), ], ], ), ); } }