import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import '../models/dose_record.dart'; import '../services/storage_service.dart'; class HistoryScreen extends StatefulWidget { final StorageService storageService; const HistoryScreen({super.key, required this.storageService}); @override State createState() => _HistoryScreenState(); } class _HistoryScreenState extends State { late List _records; @override void initState() { super.initState(); _loadRecords(); } void _loadRecords() { setState(() { _records = widget.storageService.getRecords(); }); } Future _deleteRecord(DoseRecord record) async { final confirmed = await showDialog( 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.red), child: const Text('删除', style: TextStyle(color: Colors.white)), ), ], ), ); if (confirmed == true) { await widget.storageService.deleteRecord(record.id); _loadRecords(); } } @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: _records.isEmpty ? const Center( child: Text('暂无吸入流水记录', style: TextStyle(color: Colors.grey, fontSize: 14)), ) : ListView.separated( padding: const EdgeInsets.all(16), itemCount: _records.length, separatorBuilder: (context, index) => const SizedBox(height: 12), itemBuilder: (context, index) { final r = _records[index]; final dateStr = DateFormat('yyyy-MM-dd HH:mm').format(r.startTime); final durationSecs = r.duration.inSeconds; String typeBadge = '辅舒酮 ${r.flixotidePuffs}喷'; if (r.isPrime) { typeBadge = '试喷 -${r.flixotidePuffs}喷'; } else if (r.ventolinPuffs > 0) { typeBadge = '双药联合 (万+辅各${r.flixotidePuffs}喷)'; } return Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(16), border: Border.all(color: const Color(0xFFE2E8F0)), ), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( padding: const EdgeInsets.all(10), decoration: BoxDecoration( color: r.isPrime ? const Color(0xFFF1F5F9) : const Color(0xFFEFF6FF), shape: BoxShape.circle, ), child: Icon( r.isPrime ? Icons.air : Icons.medication, color: r.isPrime ? Colors.grey : const Color(0xFF2563EB), size: 20, ), ), const SizedBox(width: 12), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( dateStr, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 13, color: Color(0xFF1E293B)), ), Container( padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2), decoration: BoxDecoration( color: const Color(0xFFF1F5F9), borderRadius: BorderRadius.circular(6), ), child: Text( typeBadge, style: const TextStyle(fontSize: 11, fontWeight: FontWeight.w600, color: Color(0xFF475569)), ), ), ], ), const SizedBox(height: 6), if (!r.isPrime) Text( '吸入耗时: $durationSecs秒 • ${r.rinsedMouth ? "已漱口" : "未漱口"} • ${r.washedFace ? "已洗脸" : "未洗脸"}', style: const TextStyle(color: Color(0xFF64748B), fontSize: 12), ) else Text( r.note ?? '新瓶试喷', style: const TextStyle(color: Color(0xFF94A3B8), fontSize: 12), ), ], ), ), IconButton( icon: const Icon(Icons.delete_outline, size: 18, color: Colors.grey), onPressed: () => _deleteRecord(r), ), ], ), ); }, ), ); } }