import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../providers/app_provider.dart'; import '../models/data_models.dart'; /// 笔记详情页 class NoteDetailPage extends StatefulWidget { final Note note; const NoteDetailPage({super.key, required this.note}); @override State createState() => _NoteDetailPageState(); } class _NoteDetailPageState extends State { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.note.title), actions: [ IconButton( icon: const Icon(Icons.edit), onPressed: () => _navigateToEdit(context), ), IconButton( icon: const Icon(Icons.delete_outline), onPressed: () => _showDeleteDialog(context), ), ], ), body: SingleChildScrollView( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // 标签区域 if (widget.note.tags.isNotEmpty) _buildTagsSection(context), // 内容区域 _buildContentSection(context), // 时间信息 _buildTimeSection(context), ], ), ), ); } /// 构建标签区域 Widget _buildTagsSection(BuildContext context) { return Container( padding: const EdgeInsets.all(16), child: Wrap( spacing: 8, runSpacing: 8, children: widget.note.tags.map((tag) { return Chip( label: Text(tag), backgroundColor: Theme.of(context).colorScheme.primaryContainer, labelStyle: TextStyle( color: Theme.of(context).colorScheme.onPrimaryContainer, ), ); }).toList(), ), ); } /// 构建内容区域 Widget _buildContentSection(BuildContext context) { return Padding( padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( widget.note.content, style: Theme.of(context).textTheme.bodyLarge?.copyWith( height: 1.8, ), ), ], ), ); } /// 构建时间信息区域 Widget _buildTimeSection(BuildContext context) { return Container( margin: const EdgeInsets.all(16), padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Theme.of(context).colorScheme.surfaceContainerHighest, borderRadius: BorderRadius.circular(12), ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Icon( Icons.access_time, size: 18, color: Theme.of(context).colorScheme.onSurfaceVariant, ), const SizedBox(width: 8), Text( '创建时间', style: Theme.of(context).textTheme.titleSmall?.copyWith( fontWeight: FontWeight.bold, ), ), ], ), const SizedBox(height: 8), Text( _formatDateTime(widget.note.createdAt), style: Theme.of(context).textTheme.bodyMedium?.copyWith( color: Theme.of(context).colorScheme.onSurfaceVariant, ), ), const SizedBox(height: 16), Row( children: [ Icon( Icons.edit, size: 18, color: Theme.of(context).colorScheme.onSurfaceVariant, ), const SizedBox(width: 8), Text( '更新时间', style: Theme.of(context).textTheme.titleSmall?.copyWith( fontWeight: FontWeight.bold, ), ), ], ), const SizedBox(height: 8), Text( _formatDateTime(widget.note.updatedAt), style: Theme.of(context).textTheme.bodyMedium?.copyWith( color: Theme.of(context).colorScheme.onSurfaceVariant, ), ), ], ), ); } /// 格式化日期时间 String _formatDateTime(DateTime dateTime) { return '${dateTime.year}-${dateTime.month.toString().padLeft(2, '0')}-${dateTime.day.toString().padLeft(2, '0')} ${dateTime.hour.toString().padLeft(2, '0')}:${dateTime.minute.toString().padLeft(2, '0')}'; } /// 跳转到编辑页面 void _navigateToEdit(BuildContext context) { Navigator.pushNamed(context, '/note-form', arguments: widget.note).then((_) { context.read().loadNotes(); }); } /// 显示删除对话框 void _showDeleteDialog(BuildContext context) { showDialog( context: context, builder: (context) => AlertDialog( title: const Text('确认删除'), content: Text('确定要删除"${widget.note.title}"吗?此操作不可恢复。'), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: const Text('取消'), ), TextButton( onPressed: () async { await context.read().removeNote(widget.note.id); if (!mounted) return; Navigator.pop(context); Navigator.pop(context); ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('已删除'), behavior: SnackBarBehavior.floating, ), ); }, child: const Text( '删除', style: TextStyle(color: Colors.red), ), ), ], ), ); } }