适配深色模式

This commit is contained in:
DelLevin-Home
2026-05-26 05:10:21 +08:00
parent b36f50827f
commit 73f3795e65
53 changed files with 3846 additions and 3211 deletions

View File

@@ -14,14 +14,12 @@ class NoteListItem extends StatelessWidget {
@override
Widget build(BuildContext context) {
// 使用 RepaintBoundary 减少重绘
return RepaintBoundary(
child: _NoteListItemContent(note: note),
);
}
}
/// 笔记列表项内容 - 分离出来便于优化
class _NoteListItemContent extends StatelessWidget {
final Note note;
@@ -29,10 +27,10 @@ class _NoteListItemContent extends StatelessWidget {
@override
Widget build(BuildContext context) {
final colors = Theme.of(context).colorScheme;
return InkWell(
onTap: () {
Navigator.pushNamed(context, '/note-detail', arguments: note).then((_) async {
// 返回时刷新笔记列表
await context.read<AppProvider>().loadNotes();
});
},
@@ -41,40 +39,37 @@ class _NoteListItemContent extends StatelessWidget {
margin: const EdgeInsets.only(bottom: 8),
padding: const EdgeInsets.fromLTRB(12, 10, 12, 10),
decoration: BoxDecoration(
color: const Color(0xFFFAFAFA),
color: colors.surfaceContainerHigh,
borderRadius: BorderRadius.circular(8),
border: Border.all(color: const Color(0xFFE8E8E8), width: 0.5),
border: Border.all(color: colors.outlineVariant, width: 0.5),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
// 顶部:时间 + MD标记
Row(
children: [
// 时间
Text(
_formatDateCached(note.updatedAt),
style: const TextStyle(
style: TextStyle(
fontSize: 11,
color: Color(0xFF999999),
color: colors.onSurface.withValues(alpha: 0.4),
),
),
const SizedBox(width: 8),
// MD标记
Container(
padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 1),
decoration: BoxDecoration(
color: Colors.white,
color: colors.surface,
borderRadius: BorderRadius.circular(3),
border: Border.all(color: const Color(0xFFE8E8E8), width: 0.5),
border: Border.all(color: colors.outlineVariant, width: 0.5),
),
child: const Text(
child: Text(
'MD',
style: TextStyle(
fontSize: 9,
fontWeight: FontWeight.w600,
color: Color(0xFF999999),
color: colors.onSurface.withValues(alpha: 0.4),
),
),
),
@@ -83,14 +78,13 @@ class _NoteListItemContent extends StatelessWidget {
const SizedBox(height: 6),
// 标题
if (note.title.isNotEmpty) ...[
Text(
note.title,
style: const TextStyle(
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w600,
color: Color(0xFF1A1A1A),
color: colors.onSurface,
height: 1.4,
),
maxLines: 1,
@@ -99,20 +93,18 @@ class _NoteListItemContent extends StatelessWidget {
const SizedBox(height: 4),
],
// 内容摘要去除Markdown标记内容为空则不显示
if (_collapseBlankLines(_cleanMarkdown(note.content).trim()).isNotEmpty)
Text(
_collapseBlankLines(_cleanMarkdown(note.content).trim()),
style: const TextStyle(
style: TextStyle(
fontSize: 13,
color: Color(0xFF666666),
color: colors.onSurface.withValues(alpha: 0.6),
height: 1.5,
),
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
// 底部标签
if (note.tags.isNotEmpty) ...[
const SizedBox(height: 6),
Wrap(
@@ -122,15 +114,15 @@ class _NoteListItemContent extends StatelessWidget {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 7, vertical: 2),
decoration: BoxDecoration(
color: Colors.white,
color: colors.surface,
borderRadius: BorderRadius.circular(4),
border: Border.all(color: const Color(0xFFE8E8E8), width: 0.5),
border: Border.all(color: colors.outlineVariant, width: 0.5),
),
child: Text(
tag,
style: const TextStyle(
style: TextStyle(
fontSize: 11,
color: Color(0xFF666666),
color: colors.onSurface.withValues(alpha: 0.6),
),
),
);
@@ -138,10 +130,9 @@ class _NoteListItemContent extends StatelessWidget {
),
],
// 图片预览
if (note.images.isNotEmpty) ...[
const SizedBox(height: 8),
_buildImagePreviewRow(),
_buildImagePreviewRow(colors),
],
],
),
@@ -149,8 +140,7 @@ class _NoteListItemContent extends StatelessWidget {
);
}
/// 图片预览行
Widget _buildImagePreviewRow() {
Widget _buildImagePreviewRow(ColorScheme colors) {
final images = note.images;
final count = images.length.clamp(0, 3);
return Row(
@@ -162,14 +152,14 @@ class _NoteListItemContent extends StatelessWidget {
margin: const EdgeInsets.only(right: 6),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(6),
border: Border.all(color: const Color(0xFFE8E8E8), width: 0.5),
border: Border.all(color: colors.outlineVariant, width: 0.5),
),
clipBehavior: Clip.antiAlias,
child: FadeInLocalImage(
path: images[i],
fit: BoxFit.cover,
errorWidget: Container(
color: const Color(0xFFF5F5F5),
color: colors.surfaceContainerHighest,
),
),
),
@@ -178,13 +168,13 @@ class _NoteListItemContent extends StatelessWidget {
width: 48,
height: 48,
decoration: BoxDecoration(
color: const Color(0xFFF5F5F5),
color: colors.surfaceContainerHighest,
borderRadius: BorderRadius.circular(6),
),
child: Center(
child: Text(
'+${images.length - 3}',
style: const TextStyle(fontSize: 12, color: Color(0xFF999999), fontWeight: FontWeight.w500),
style: TextStyle(fontSize: 12, color: colors.onSurface.withValues(alpha: 0.4), fontWeight: FontWeight.w500),
),
),
),
@@ -192,45 +182,44 @@ class _NoteListItemContent extends StatelessWidget {
);
}
/// 清理 Markdown 标记,提取纯文本
String _cleanMarkdown(String text) {
return text
.replaceAll(RegExp(r'^#+\s+', multiLine: true), '') // 标题
.replaceAll(RegExp(r'\*\*(.+?)\*\*'), r'$1') // 粗体
.replaceAll(RegExp(r'\*(.+?)\*'), r'$1') // 斜体
.replaceAll(RegExp(r'`(.+?)`'), r'$1') // 行内代码
.replaceAll(RegExp(r'^\s*[-*+]\s', multiLine: true), '') // 列表
.replaceAll(RegExp(r'^\s*>\s', multiLine: true), '') // 引用
.replaceAll(RegExp(r'\[([^\]]+)\]\([^)]+\)'), r'$1') // 链接
.replaceAll(RegExp(r'!\[([^\]]*)\]\([^)]+\)'), '') // 图片
.replaceAll(RegExp(r'^#+\s+', multiLine: true), '')
.replaceAll(RegExp(r'\*\*(.+?)\*\*'), r'$1')
.replaceAll(RegExp(r'\*(.+?)\*'), r'$1')
.replaceAll(RegExp(r'`(.+?)`'), r'$1')
.replaceAll(RegExp(r'^\s*[-*+]\s', multiLine: true), '')
.replaceAll(RegExp(r'^\s*>\s', multiLine: true), '')
.replaceAll(RegExp(r'\[([^\]]+)\]\([^)]+\)'), r'$1')
.replaceAll(RegExp(r'!\[([^\]]*)\]\([^)]+\)'), '')
.trim();
}
/// 合并连续空行为单行
String _collapseBlankLines(String text) {
return text.replaceAll(RegExp(r'\n\s*\n+'), '\n');
}
/// 显示删除确认对话框
void _showDeleteDialog(BuildContext context) {
final colors = Theme.of(context).colorScheme;
showDialog(
context: context,
builder: (context) => AlertDialog(
backgroundColor: Colors.white,
backgroundColor: colors.surface,
elevation: 0,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
title: const Text(
title: Text(
'确认删除',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w600,
color: colors.onSurface,
),
),
content: const Text(
content: Text(
'确定要删除这条笔记吗?删除后可在回收站恢复。',
style: TextStyle(
fontSize: 14,
color: Color(0xFF666666),
color: colors.onSurface.withValues(alpha: 0.6),
height: 1.5,
),
),
@@ -238,7 +227,7 @@ class _NoteListItemContent extends StatelessWidget {
TextButton(
onPressed: () => Navigator.pop(context),
style: TextButton.styleFrom(
foregroundColor: const Color(0xFF666666),
foregroundColor: colors.onSurface.withValues(alpha: 0.6),
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
),
child: const Text('取消'),
@@ -250,8 +239,8 @@ class _NoteListItemContent extends StatelessWidget {
ToastUtil.show(context, '已删除');
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
foregroundColor: Colors.white,
backgroundColor: colors.error,
foregroundColor: colors.onError,
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
@@ -267,28 +256,24 @@ class _NoteListItemContent extends StatelessWidget {
}
}
// 日期格式化缓存
final Map<DateTime, String> _dateFormatCache = {};
/// 格式化日期(带缓存)
String _formatDateCached(DateTime date) {
// 使用日期部分作为缓存键(忽略时分秒)
final cacheKey = DateTime(date.year, date.month, date.day);
if (_dateFormatCache.containsKey(cacheKey)) {
return _dateFormatCache[cacheKey]!;
}
final result = _formatDate(date);
_dateFormatCache[cacheKey] = result;
return result;
}
/// 格式化日期
String _formatDate(DateTime date) {
final now = DateTime.now();
final difference = now.difference(date);
if (difference.inDays == 0) {
if (difference.inHours == 0) {
if (difference.inMinutes == 0) {