新增epub书摘,高亮文字,适配平板

This commit is contained in:
DelLevin-Home
2026-07-02 05:22:21 +08:00
parent a0a146854b
commit a7e4845e46
27 changed files with 3712 additions and 505 deletions

View File

@@ -1,16 +1,22 @@
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../utils/epub/reader_dao.dart';
import '../../utils/epub/epub_parser.dart';
import '../../utils/epub/reader_models.dart';
import '../../utils/book/book_dao.dart';
import '../../utils/book/book_excerpt_dao.dart';
import '../../utils/toast_util.dart';
import '../../models/data_models.dart';
import '../book/book_detail_page.dart';
import '../book/book_excerpts_page.dart';
import 'book_link_page.dart';
import 'epub_edit_page.dart';
import 'epub_highlights_page.dart';
import 'highlight_detail_sheet.dart';
import 'reader_screen.dart';
/// EPUB 书籍详情页
@@ -37,6 +43,7 @@ class _EpubDetailPageState extends State<EpubDetailPage> {
EpubBookInfo? _bookInfo;
bool _descriptionExpanded = false;
Future<List<BookExcerpt>>? _excerptsFuture;
Future<List<Map<String, dynamic>>>? _highlightsFuture;
@override
void initState() {
@@ -47,6 +54,7 @@ class _EpubDetailPageState extends State<EpubDetailPage> {
if (linkedBookId.isNotEmpty) {
_excerptsFuture = BookExcerptDao().getExcerptsByBookId(linkedBookId);
}
_highlightsFuture = _dao.getHighlightsByBookId(widget.bookId);
}
Future<void> _loadBookInfo() async {
@@ -58,7 +66,18 @@ class _EpubDetailPageState extends State<EpubDetailPage> {
Future<void> _refreshBook() async {
final updated = await _dao.getReaderBookById(widget.bookId);
if (mounted && updated != null) setState(() => _book = updated);
if (mounted && updated != null) {
final linkedBookId = updated['book_id'] as String? ?? '';
setState(() {
_book = updated;
_highlightsFuture = _dao.getHighlightsByBookId(widget.bookId);
if (linkedBookId.isNotEmpty) {
_excerptsFuture = BookExcerptDao().getExcerptsByBookId(linkedBookId);
} else {
_excerptsFuture = null;
}
});
}
}
void _navigateToReader() {
@@ -202,12 +221,20 @@ class _EpubDetailPageState extends State<EpubDetailPage> {
child: _buildLinkedBookCard(colors),
),
// ── 句读(高亮)──
Divider(height: 0.5, thickness: 0.5, color: colors.outline),
_buildHighlightsSectionHeader(colors),
Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 24),
child: _buildHighlightsList(colors),
),
// ── 书籍摘抄(仅关联书籍时显示)──
if ((_book['book_id'] as String? ?? '').isNotEmpty) ...[
Divider(height: 0.5, thickness: 0.5, color: colors.outline),
_buildSectionHeader('书籍摘抄', colors),
_buildExcerptsSectionHeader(colors),
Padding(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 24),
padding: const EdgeInsets.fromLTRB(0, 0, 0, 24),
child: _buildExcerptsList(colors),
),
],
@@ -366,6 +393,7 @@ class _EpubDetailPageState extends State<EpubDetailPage> {
builder: (context, snapshot) {
if (!snapshot.hasData || snapshot.data!.isEmpty) {
return Container(
margin: const EdgeInsets.symmetric(horizontal: 16),
padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 16),
decoration: BoxDecoration(
color: colors.surfaceContainerHigh,
@@ -382,71 +410,398 @@ class _EpubDetailPageState extends State<EpubDetailPage> {
);
}
final excerpts = snapshot.data!;
final showCount = excerpts.length > 5 ? 5 : excerpts.length;
return Column(
children: [
for (int i = 0; i < showCount; i++)
Padding(
padding: EdgeInsets.only(bottom: i < showCount - 1 ? 8 : 0),
child: Container(
width: double.infinity,
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: colors.surfaceContainerHigh,
borderRadius: BorderRadius.circular(10),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
width: 3,
height: 16,
margin: const EdgeInsets.only(top: 2, right: 10),
decoration: BoxDecoration(
color: colors.primary.withValues(alpha: 0.6),
borderRadius: BorderRadius.circular(1.5),
),
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
excerpts[i].content,
maxLines: 3,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.left,
style: TextStyle(fontSize: 13, color: colors.onSurface.withValues(alpha: 0.75), height: 1.6),
),
if (excerpts[i].chapter.isNotEmpty)
Padding(
padding: const EdgeInsets.only(top: 6),
child: Text(
excerpts[i].chapter,
style: TextStyle(fontSize: 11, color: colors.onSurface.withValues(alpha: 0.35)),
),
),
],
),
),
],
),
),
),
if (excerpts.length > 5)
Padding(
padding: const EdgeInsets.only(top: 8),
child: Text(
'${excerpts.length} 条摘抄',
style: TextStyle(fontSize: 12, color: colors.onSurface.withValues(alpha: 0.35)),
),
),
],
return SizedBox(
height: 140,
child: ListView.separated(
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.symmetric(horizontal: 16),
itemCount: excerpts.length,
separatorBuilder: (_, __) => const SizedBox(width: 10),
itemBuilder: (context, i) => _buildExcerptCard(excerpts[i], colors),
),
);
},
);
}
Widget _buildExcerptCard(BookExcerpt excerpt, ColorScheme colors) {
final hasComment = excerpt.comment.isNotEmpty;
return GestureDetector(
onTap: () => _showExcerptDetail(excerpt, colors),
child: Container(
width: 160,
padding: const EdgeInsets.fromLTRB(14, 12, 14, 10),
decoration: BoxDecoration(
color: colors.surfaceContainerHigh,
borderRadius: BorderRadius.circular(12),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 顶部:图标 + 章节标签
Row(
children: [
Icon(Icons.menu_book_outlined, size: 14, color: colors.primary.withValues(alpha: 0.6)),
const Spacer(),
if (excerpt.chapter.isNotEmpty)
Container(
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
decoration: BoxDecoration(
color: colors.primary.withValues(alpha: 0.08),
borderRadius: BorderRadius.circular(8),
),
child: Text(
excerpt.chapter,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 9, color: colors.primary.withValues(alpha: 0.7), fontWeight: FontWeight.w500),
),
),
],
),
const SizedBox(height: 8),
// 摘抄内容
Expanded(
child: Text(
excerpt.content,
maxLines: hasComment ? 3 : 4,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 12, color: colors.onSurface.withValues(alpha: 0.8), height: 1.6),
),
),
// 底部:感悟标记 + 日期
Row(
children: [
if (hasComment) ...[
Icon(Icons.lightbulb_outline, size: 10, color: colors.primary.withValues(alpha: 0.4)),
const SizedBox(width: 3),
Text('有感悟', style: TextStyle(fontSize: 9, color: colors.primary.withValues(alpha: 0.4))),
],
const Spacer(),
Text(
_formatDate(excerpt.createdAt.toIso8601String()),
style: TextStyle(fontSize: 9, color: colors.onSurface.withValues(alpha: 0.3)),
),
],
),
],
),
),
);
}
// ── 句读(高亮)──
Widget _buildHighlightsSectionHeader(ColorScheme colors) {
return Padding(
padding: const EdgeInsets.fromLTRB(16, 14, 8, 10),
child: Row(children: [
Container(width: 4, height: 14,
decoration: BoxDecoration(color: colors.onSurface, borderRadius: BorderRadius.circular(2))),
const SizedBox(width: 8),
Text('句读', style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600, color: colors.onSurface)),
const Spacer(),
TextButton(
onPressed: _navigateToHighlightsPage,
style: TextButton.styleFrom(
minimumSize: const Size(0, 32),
padding: const EdgeInsets.symmetric(horizontal: 12),
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text('详情', style: TextStyle(fontSize: 12, color: colors.onSurface.withValues(alpha: 0.5))),
Icon(Icons.chevron_right, size: 16, color: colors.onSurface.withValues(alpha: 0.5)),
],
),
),
]),
);
}
// ── 书籍摘抄 ──
Widget _buildExcerptsSectionHeader(ColorScheme colors) {
return Padding(
padding: const EdgeInsets.fromLTRB(16, 14, 8, 10),
child: Row(children: [
Container(width: 4, height: 14,
decoration: BoxDecoration(color: colors.onSurface, borderRadius: BorderRadius.circular(2))),
const SizedBox(width: 8),
Text('书籍摘抄', style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600, color: colors.onSurface)),
const Spacer(),
TextButton(
onPressed: _navigateToExcerptsPage,
style: TextButton.styleFrom(
minimumSize: const Size(0, 32),
padding: const EdgeInsets.symmetric(horizontal: 12),
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text('详情', style: TextStyle(fontSize: 12, color: colors.onSurface.withValues(alpha: 0.5))),
Icon(Icons.chevron_right, size: 16, color: colors.onSurface.withValues(alpha: 0.5)),
],
),
),
]),
);
}
Widget _buildHighlightsList(ColorScheme colors) {
return FutureBuilder<List<Map<String, dynamic>>>(
future: _highlightsFuture,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const SizedBox.shrink();
}
final highlights = snapshot.data!
.where((h) => h['color'] != 'excerpt')
.toList();
if (highlights.isEmpty) {
return Container(
margin: const EdgeInsets.symmetric(horizontal: 16),
padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 16),
decoration: BoxDecoration(
color: colors.surfaceContainerHigh,
borderRadius: BorderRadius.circular(12),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.highlight_outlined, size: 18, color: colors.onSurface.withValues(alpha: 0.2)),
const SizedBox(width: 8),
Text('暂无句读', style: TextStyle(fontSize: 13, color: colors.onSurface.withValues(alpha: 0.35))),
],
),
);
}
return SizedBox(
height: 140,
child: ListView.separated(
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.symmetric(horizontal: 16),
itemCount: highlights.length,
separatorBuilder: (_, __) => const SizedBox(width: 10),
itemBuilder: (context, i) => _buildHighlightCard(highlights[i], colors),
),
);
},
);
}
Widget _buildHighlightCard(Map<String, dynamic> highlight, ColorScheme colors) {
final content = highlight['content'] as String? ?? '';
final chapter = highlight['chapter'] as String? ?? '';
final chapterNum = int.tryParse(chapter);
final createdAt = highlight['created_at'] as String? ?? '';
return GestureDetector(
onTap: () => _showHighlightDetail(highlight, colors),
child: Container(
width: 160,
padding: const EdgeInsets.fromLTRB(14, 12, 14, 10),
decoration: BoxDecoration(
color: colors.surfaceContainerHigh,
borderRadius: BorderRadius.circular(12),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 顶部:引号图标 + 章节标签
Row(
children: [
Icon(Icons.format_quote, size: 14, color: const Color(0xFFFFC107)),
const Spacer(),
if (chapterNum != null)
Container(
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
decoration: BoxDecoration(
color: const Color(0xFFFFEB3B).withValues(alpha: 0.15),
borderRadius: BorderRadius.circular(8),
),
child: Text(
'${chapterNum + 1}',
style: const TextStyle(fontSize: 9, color: Color(0xFF795548), fontWeight: FontWeight.w500),
),
),
],
),
const SizedBox(height: 8),
// 高亮文本
Expanded(
child: Text(
content,
maxLines: 4,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 12, color: colors.onSurface.withValues(alpha: 0.8), height: 1.6),
),
),
// 底部:日期
if (createdAt.isNotEmpty)
Padding(
padding: const EdgeInsets.only(top: 4),
child: Text(
_formatDate(createdAt),
style: TextStyle(fontSize: 9, color: colors.onSurface.withValues(alpha: 0.3)),
),
),
],
),
),
);
}
/// 摘抄详情弹窗
void _showExcerptDetail(BookExcerpt excerpt, ColorScheme colors) {
showExcerptDetailSheet(
context,
content: excerpt.content,
chapter: excerpt.chapter,
comment: excerpt.comment,
createdAt: excerpt.createdAt,
bookTitle: _book['title'] as String? ?? '',
onDelete: () => _deleteExcerpt(excerpt, colors),
);
}
/// 删除摘抄(同步删除蓝色高亮)
Future<void> _deleteExcerpt(BookExcerpt excerpt, ColorScheme colors) async {
final confirmed = showDialog<bool>(
context: context,
builder: (ctx) => AlertDialog(
title: const Text('删除摘抄', style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600)),
content: Text('确定删除这条摘抄吗?', style: TextStyle(fontSize: 14, color: colors.onSurface.withValues(alpha: 0.6))),
actions: [
TextButton(onPressed: () => Navigator.pop(ctx, false), child: Text('取消', style: TextStyle(color: colors.onSurface.withValues(alpha: 0.4)))),
TextButton(onPressed: () => Navigator.pop(ctx, true), child: Text('删除', style: TextStyle(color: colors.error))),
],
),
);
if (await confirmed != true) return;
// 同步删除蓝色高亮(用 books 表 ID 查找关联的 reader_book
final linkedBookId = _book['book_id'] as String? ?? '';
if (linkedBookId.isNotEmpty) {
final readerBook = await _dao.getReaderBookByBookId(linkedBookId);
if (readerBook != null) {
await _dao.deleteExcerptHighlightByContent(
readerBook['id'] as String,
excerpt.content,
);
}
}
// 删除摘抄记录
await BookExcerptDao().deleteExcerpt(excerpt.id);
if (mounted) {
setState(() {
_refreshBook();
});
ToastUtil.show(context, '已删除');
}
}
/// 句读详情弹窗(使用共享组件)
void _showHighlightDetail(Map<String, dynamic> highlight, ColorScheme colors) {
showHighlightDetailSheet(
context,
highlight: highlight,
book: _book,
onDelete: () => _deleteHighlight(highlight['id'] as int, colors),
);
}
Future<void> _deleteHighlight(int id, ColorScheme colors) async {
final confirmed = showDialog<bool>(
context: context,
builder: (ctx) => AlertDialog(
title: const Text('删除句读', style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600)),
content: Text('确定删除这条句读?', style: TextStyle(fontSize: 14, color: colors.onSurface.withValues(alpha: 0.6))),
actions: [
TextButton(
onPressed: () => Navigator.pop(ctx, false),
child: Text('取消', style: TextStyle(color: colors.onSurface.withValues(alpha: 0.4))),
),
TextButton(
onPressed: () => Navigator.pop(ctx, true),
child: Text('删除', style: TextStyle(color: colors.error)),
),
],
),
);
if (await confirmed != true) return;
await _dao.deleteHighlight(id);
if (mounted) {
setState(() {
_highlightsFuture = _dao.getHighlightsByBookId(widget.bookId);
});
}
}
String _formatDate(String isoString) {
try {
final dt = DateTime.parse(isoString);
return '${dt.year}-${dt.month.toString().padLeft(2, '0')}-${dt.day.toString().padLeft(2, '0')}';
} catch (_) {
return '';
}
}
void _navigateToHighlight(Map<String, dynamic> highlight) {
final chapter = int.tryParse(highlight['chapter'] as String? ?? '') ?? 0;
final xpath = _extractStartXPath(highlight['cfi'] as String? ?? '');
final text = highlight['content'] as String? ?? '';
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => ReaderScreen(
bookId: _book['id'] as String,
filePath: _book['file_path'] as String,
title: _book['title'] as String? ?? '',
coverPath: _book['cover_path'] as String?,
bookData: _book,
initialSpineIndex: chapter,
scrollToXPath: xpath,
scrollToText: text,
),
),
).then((_) => _refreshBook());
}
/// 从 cfi JSON 中提取 startXPath
String? _extractStartXPath(String cfi) {
if (cfi.isEmpty) return null;
try {
final decoded = jsonDecode(cfi) as Map<String, dynamic>;
return decoded['startXPath'] as String?;
} catch (_) {
return null;
}
}
void _navigateToHighlightsPage() {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => EpubHighlightsPage(bookId: widget.bookId, book: _book),
),
).then((_) => _refreshBook());
}
void _navigateToExcerptsPage() async {
final linkedBookId = _book['book_id'] as String? ?? '';
if (linkedBookId.isEmpty) return;
final book = await _bookDao.getBookById(linkedBookId);
if (!mounted || book == null) return;
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => BookExcerptsPage(book: book),
),
).then((_) => _refreshBook());
}
void _showLinkedBookActions(ColorScheme colors, String title) {
showModalBottomSheet(
context: context,