功能优化

This commit is contained in:
DelLevin-Home
2026-03-09 14:14:38 +08:00
parent c894921472
commit 7c970cde49
7 changed files with 496 additions and 705 deletions

View File

@@ -11,6 +11,21 @@ class NoteListItem extends StatelessWidget {
const NoteListItem({super.key, required this.note});
@override
Widget build(BuildContext context) {
// 使用 RepaintBoundary 减少重绘
return RepaintBoundary(
child: _NoteListItemContent(note: note),
);
}
}
/// 笔记列表项内容 - 分离出来便于优化
class _NoteListItemContent extends StatelessWidget {
final Note note;
const _NoteListItemContent({required this.note});
@override
Widget build(BuildContext context) {
final isPlainText = note.contentType == 'plain_text';
@@ -50,9 +65,9 @@ class NoteListItem extends StatelessWidget {
),
),
const SizedBox(width: 8),
// 时间
// 时间 - 使用缓存的格式化结果
Text(
_formatDate(note.updatedAt),
_formatDateCached(note.updatedAt),
style: const TextStyle(
fontSize: 11,
color: Color(0xFF999999),
@@ -100,18 +115,11 @@ class NoteListItem extends StatelessWidget {
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: note.images.length > 2 ? 2 : note.images.length,
physics: const NeverScrollableScrollPhysics(), // 禁用滚动,提高性能
itemBuilder: (context, index) {
return Container(
width: 60,
height: 60,
margin: const EdgeInsets.only(right: 8),
decoration: BoxDecoration(
border: Border.all(color: const Color(0xFFE5E5E5)),
),
child: Image.file(
File(note.images[index]),
fit: BoxFit.cover,
),
return _NoteImage(
imagePath: note.images[index],
index: index,
);
},
),
@@ -148,26 +156,6 @@ class NoteListItem extends StatelessWidget {
);
}
/// 格式化日期
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) {
return '刚刚';
}
return '${difference.inMinutes}分钟前';
}
return '${difference.inHours}小时前';
} else if (difference.inDays < 7) {
return '${difference.inDays}天前';
} else {
return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
}
}
/// 显示删除确认对话框
void _showDeleteDialog(BuildContext context) {
showDialog(
@@ -196,3 +184,75 @@ class NoteListItem extends StatelessWidget {
);
}
}
/// 笔记图片组件 - 独立出来便于优化
class _NoteImage extends StatelessWidget {
final String imagePath;
final int index;
const _NoteImage({
required this.imagePath,
required this.index,
});
@override
Widget build(BuildContext context) {
return Container(
width: 60,
height: 60,
margin: EdgeInsets.only(right: index < 1 ? 8 : 0),
decoration: BoxDecoration(
border: Border.all(color: const Color(0xFFE5E5E5)),
),
child: Image.file(
File(imagePath),
fit: BoxFit.cover,
// 使用低内存缓存
cacheWidth: 120,
cacheHeight: 120,
errorBuilder: (_, __, ___) => const Icon(
Icons.broken_image,
size: 24,
color: Color(0xFFCCCCCC),
),
),
);
}
}
// 日期格式化缓存
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) {
return '刚刚';
}
return '${difference.inMinutes}分钟前';
}
return '${difference.inHours}小时前';
} else if (difference.inDays < 7) {
return '${difference.inDays}天前';
} else {
return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
}
}