Files
AeroTrack/lib/screens/settings_screen.dart

308 lines
13 KiB
Dart

import 'package:flutter/material.dart';
import '../models/app_settings.dart';
import '../services/storage_service.dart';
import '../services/tts_service.dart';
import '../services/audio_service.dart';
import 'plan_config_screen.dart';
import 'lan_sync_screen.dart';
class SettingsScreen extends StatefulWidget {
final StorageService storageService;
const SettingsScreen({super.key, required this.storageService});
@override
State<SettingsScreen> createState() => _SettingsScreenState();
}
class _SettingsScreenState extends State<SettingsScreen> {
late AppSettings _settings;
@override
void initState() {
super.initState();
_settings = widget.storageService.getSettings();
}
Future<void> _save() async {
await widget.storageService.saveSettings(_settings);
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('设置已保存')),
);
}
}
Future<void> _resetFlixotide() async {
final confirmed = await showDialog<bool>(
context: context,
builder: (ctx) => AlertDialog(
title: const Text('启用新药瓶 - 辅舒酮'),
content: const Text('确认启用新的一瓶辅舒酮,将其剩余次数重置为 60 喷吗?'),
actions: [
TextButton(onPressed: () => Navigator.pop(ctx, false), child: const Text('取消')),
ElevatedButton(
onPressed: () => Navigator.pop(ctx, true),
child: const Text('确认重置为 60 喷'),
),
],
),
);
if (confirmed == true) {
final flixotide = widget.storageService.getPrimaryFlixotide();
if (flixotide != null) {
await widget.storageService.resetCanisterDoses(flixotide.id, 60);
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('已成功将辅舒酮重置为 60 喷!')),
);
}
}
}
}
Future<void> _resetVentolin() async {
final confirmed = await showDialog<bool>(
context: context,
builder: (ctx) => AlertDialog(
title: const Text('启用新药瓶 - 万托林'),
content: const Text('确认启用新的一瓶万托林,将其剩余次数重置为 200 喷吗?'),
actions: [
TextButton(onPressed: () => Navigator.pop(ctx, false), child: const Text('取消')),
ElevatedButton(
onPressed: () => Navigator.pop(ctx, true),
child: const Text('确认重置为 200 喷'),
),
],
),
);
if (confirmed == true) {
final ventolin = widget.storageService.getPrimaryVentolin();
if (ventolin != null) {
await widget.storageService.resetCanisterDoses(ventolin.id, 200);
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('已成功将万托林重置为 200 喷!')),
);
}
}
}
}
@override
Widget build(BuildContext context) {
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),
),
body: ListView(
padding: const EdgeInsets.all(20),
children: [
// 方案配置入口卡
Container(
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [Color(0xFF2563EB), Color(0xFF1D4ED8)],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
borderRadius: BorderRadius.circular(16),
),
child: ListTile(
contentPadding: const EdgeInsets.all(16),
leading: const Icon(Icons.medication_liquid, color: Colors.white, size: 28),
title: const Text(
'药物与雾化方案配置',
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 15),
),
subtitle: const Text(
'勾选当前使用的药物、喷数、吸气时间与多药间隔',
style: TextStyle(color: Colors.white70, fontSize: 12),
),
trailing: const Icon(Icons.arrow_forward_ios, color: Colors.white70, size: 16),
onTap: () async {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => PlanConfigScreen(storageService: widget.storageService),
),
);
setState(() {
_settings = widget.storageService.getSettings();
});
},
),
),
const SizedBox(height: 12),
// 局域网多设备同步入口卡
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16),
border: Border.all(color: const Color(0xFFBFDBFE)),
boxShadow: [
BoxShadow(color: Colors.black.withValues(alpha: 0.02), blurRadius: 6, offset: const Offset(0, 2)),
],
),
child: ListTile(
leading: const Icon(Icons.sync, color: Color(0xFF2563EB), size: 26),
title: const Text('局域网多设备同步', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14)),
subtitle: const Text('家庭私有化同步 • 离开家离线打卡,回家自动合并', style: TextStyle(fontSize: 11, color: Colors.grey)),
trailing: const Icon(Icons.chevron_right, color: Colors.grey),
onTap: () async {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => LanSyncScreen(storageService: widget.storageService),
),
);
},
),
),
const SizedBox(height: 24),
// 模块 2:提醒偏好
const Text('提醒与反馈', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 13, color: Colors.grey)),
const SizedBox(height: 10),
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16),
border: Border.all(color: const Color(0xFFE2E8F0)),
),
child: Column(
children: [
SwitchListTile(
title: const Text('提示音效', style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600)),
subtitle: const Text('各阶段完成及全部做完时的提示音(清脆铃声)', style: TextStyle(fontSize: 12)),
value: _settings.enableSound,
activeTrackColor: const Color(0xFF2563EB),
onChanged: (val) {
setState(() => _settings.enableSound = val);
_save();
},
),
Padding(
padding: const EdgeInsets.only(left: 16, right: 16, bottom: 8),
child: Align(
alignment: Alignment.centerLeft,
child: OutlinedButton.icon(
onPressed: () {
AudioService().playSessionComplete();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('已播放用药完成提示铃声 🔔'), duration: Duration(seconds: 1)),
);
},
style: OutlinedButton.styleFrom(
visualDensity: VisualDensity.compact,
side: const BorderSide(color: Color(0xFFBFDBFE)),
),
icon: const Icon(Icons.music_note, size: 16, color: Color(0xFF2563EB)),
label: const Text('🔔 点击试听用药完成提示音', style: TextStyle(fontSize: 12, color: Color(0xFF2563EB))),
),
),
),
const Divider(height: 1),
SwitchListTile(
title: const Text('触觉震动', style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600)),
subtitle: const Text('倒计时结束时震动提示', style: TextStyle(fontSize: 12)),
value: _settings.enableVibration,
activeTrackColor: const Color(0xFF2563EB),
onChanged: (val) {
setState(() => _settings.enableVibration = val);
_save();
},
),
const Divider(height: 1),
SwitchListTile(
title: const Text('真人语音播报', style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600)),
subtitle: const Text('各阶段步骤语音提醒与完成时漱口洗脸指引(需开启媒体音量)', style: TextStyle(fontSize: 12)),
value: _settings.enableVoicePrompt,
activeTrackColor: const Color(0xFF2563EB),
onChanged: (val) {
setState(() => _settings.enableVoicePrompt = val);
_save();
},
),
Padding(
padding: const EdgeInsets.only(left: 16, right: 16, bottom: 12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
OutlinedButton.icon(
onPressed: () async {
final ok = await TtsService().speak('本次雾化吸入已全部完成,请及时用清水漱口并洗脸!');
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(ok ? '已发送语音播放请求,请确认手机媒体音量已开启' : '语音播放请求已发送,如未听到请检查系统TTS设置与媒体音量'),
duration: const Duration(seconds: 2),
),
);
}
},
style: OutlinedButton.styleFrom(
visualDensity: VisualDensity.compact,
side: const BorderSide(color: Color(0xFFBFDBFE)),
),
icon: const Icon(Icons.volume_up, size: 16, color: Color(0xFF2563EB)),
label: const Text('🔊 点击试听语音播报效果', style: TextStyle(fontSize: 12, color: Color(0xFF2563EB))),
),
if (TtsService().selectedEngine != null) ...[
const SizedBox(height: 4),
Text(
'当前语音引擎: ${TtsService().selectedEngine}',
style: TextStyle(fontSize: 11, color: Colors.grey.shade500),
),
],
],
),
),
],
),
),
const SizedBox(height: 24),
// 模块 3:药瓶管理与新瓶重置
const Text('药瓶库存管理与开新瓶重置', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 13, color: Colors.grey)),
const SizedBox(height: 10),
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16),
border: Border.all(color: const Color(0xFFE2E8F0)),
),
child: Column(
children: [
ListTile(
leading: const Icon(Icons.refresh, color: Color(0xFF10B981)),
title: const Text('开封新瓶:辅舒酮 (重置为 60 喷)', style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600)),
subtitle: const Text('旧瓶用完或换新药瓶时点击重置', style: TextStyle(fontSize: 12)),
onTap: _resetFlixotide,
),
const Divider(height: 1),
ListTile(
leading: const Icon(Icons.refresh, color: Color(0xFF2563EB)),
title: const Text('开封新瓶:万托林 (重置为 200 喷)', style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600)),
subtitle: const Text('旧瓶用完或换新药瓶时点击重置', style: TextStyle(fontSize: 12)),
onTap: _resetVentolin,
),
],
),
),
],
),
);
}
}