generated from dellevin/template
新增epub书摘,高亮文字,适配平板
This commit is contained in:
@@ -12,6 +12,7 @@ import 'book_reviews_page.dart';
|
||||
import 'book_excerpts_page.dart';
|
||||
import 'book_share_page.dart';
|
||||
import '../../utils/epub/reader_dao.dart';
|
||||
import '../epub_reader/epub_highlights_page.dart';
|
||||
import '../epub_reader/reader_screen.dart';
|
||||
|
||||
/// 书籍详情页 - 极简主义设计
|
||||
@@ -1007,6 +1008,15 @@ class _BookDetailPageState extends State<BookDetailPage> {
|
||||
unit: '条摘抄',
|
||||
onTap: () => _navigateToExcerpts(book),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
_buildExtraSectionItem(
|
||||
icon: Icons.highlight_outlined,
|
||||
title: '句读',
|
||||
subtitleFuture: _getEpubHighlightCount(book.id),
|
||||
emptyText: '暂无句读',
|
||||
unit: '条句读',
|
||||
onTap: () => _navigateToEpubHighlights(book),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
@@ -1036,6 +1046,15 @@ class _BookDetailPageState extends State<BookDetailPage> {
|
||||
unit: '条摘抄',
|
||||
onTap: () => _navigateToExcerpts(book),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
_buildFrostedExtraItem(
|
||||
icon: Icons.highlight_outlined,
|
||||
title: '句读',
|
||||
subtitleFuture: _getEpubHighlightCount(book.id),
|
||||
emptyText: '暂无句读',
|
||||
unit: '条句读',
|
||||
onTap: () => _navigateToEpubHighlights(book),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
@@ -1184,6 +1203,33 @@ class _BookDetailPageState extends State<BookDetailPage> {
|
||||
);
|
||||
}
|
||||
|
||||
/// 获取关联 EPUB 的句读(高亮)数量
|
||||
Future<int> _getEpubHighlightCount(String bookId) async {
|
||||
final readerBook = await ReaderDao().getReaderBookByBookId(bookId);
|
||||
if (readerBook == null) return 0;
|
||||
final highlights = await ReaderDao().getHighlightsByBookId(readerBook['id'] as String);
|
||||
return highlights.where((h) => h['color'] != 'excerpt').length;
|
||||
}
|
||||
|
||||
/// 跳转到 EPUB 句读管理页
|
||||
void _navigateToEpubHighlights(Book book) async {
|
||||
final readerBook = await ReaderDao().getReaderBookByBookId(book.id);
|
||||
if (!mounted) return;
|
||||
if (readerBook == null) {
|
||||
ToastUtil.show(context, '该书籍尚未关联EPUB数据,请关联后使用');
|
||||
return;
|
||||
}
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => EpubHighlightsPage(
|
||||
bookId: readerBook['id'] as String,
|
||||
book: readerBook,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _formatDate(DateTime date) {
|
||||
return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
|
||||
}
|
||||
@@ -1191,6 +1237,7 @@ class _BookDetailPageState extends State<BookDetailPage> {
|
||||
void _navigateToEdit(BuildContext context) {
|
||||
final provider = context.read<AppProvider>();
|
||||
Navigator.pushNamed(context, '/book-form', arguments: widget.book).then((_) {
|
||||
provider.setEditRefresh();
|
||||
provider.loadBooks();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -27,8 +27,11 @@ class _BookExcerptFormPageState extends State<BookExcerptFormPage> {
|
||||
final _commentController = TextEditingController();
|
||||
|
||||
bool _isLoading = false;
|
||||
String _bookTitle = '';
|
||||
|
||||
bool get _isEditing => widget.excerpt != null;
|
||||
int get _contentChars => _contentController.text.trim().length;
|
||||
int get _commentChars => _commentController.text.trim().length;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -38,6 +41,30 @@ class _BookExcerptFormPageState extends State<BookExcerptFormPage> {
|
||||
_contentController.text = widget.excerpt!.content;
|
||||
_commentController.text = widget.excerpt!.comment;
|
||||
}
|
||||
_contentController.addListener(() => setState(() {}));
|
||||
_commentController.addListener(() => setState(() {}));
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
_loadBookTitle();
|
||||
}
|
||||
|
||||
void _loadBookTitle() {
|
||||
setState(() {
|
||||
_bookTitle = _resolveBookTitle(widget.bookId);
|
||||
});
|
||||
}
|
||||
|
||||
String _resolveBookTitle(String bookId) {
|
||||
try {
|
||||
final provider = AppProvider();
|
||||
for (final b in provider.books) {
|
||||
if (b.id == bookId) return b.title;
|
||||
}
|
||||
} catch (_) {}
|
||||
return '';
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -56,102 +83,197 @@ class _BookExcerptFormPageState extends State<BookExcerptFormPage> {
|
||||
appBar: AppBar(
|
||||
title: Text(_isEditing ? '编辑摘抄' : '添加摘抄'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: _isLoading ? null : _saveExcerpt,
|
||||
child: _isLoading
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: Text(
|
||||
'保存',
|
||||
style: TextStyle(
|
||||
color: colors.onSurface,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
if (_isLoading)
|
||||
const Padding(
|
||||
padding: EdgeInsets.all(14),
|
||||
child: SizedBox(width: 18, height: 18, child: CircularProgressIndicator(strokeWidth: 2)),
|
||||
)
|
||||
else
|
||||
TextButton(
|
||||
onPressed: _saveExcerpt,
|
||||
child: Text('保存', style: TextStyle(fontSize: 15, fontWeight: FontWeight.w600, color: colors.primary)),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
],
|
||||
),
|
||||
body: Form(
|
||||
key: _formKey,
|
||||
child: ListView(
|
||||
padding: const EdgeInsets.all(24),
|
||||
children: [
|
||||
// 章节
|
||||
TextFormField(
|
||||
controller: _chapterController,
|
||||
decoration: InputDecoration(
|
||||
labelText: '章节(可选)',
|
||||
hintText: '例如:第一章、第3节等',
|
||||
border: const OutlineInputBorder(
|
||||
borderRadius: BorderRadius.zero,
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.zero,
|
||||
borderSide: BorderSide(color: colors.primary),
|
||||
),
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.fromLTRB(20, 8, 20, 40),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 所属书籍
|
||||
if (_bookTitle.isNotEmpty) ...[
|
||||
_buildBookInfo(colors),
|
||||
const SizedBox(height: 24),
|
||||
],
|
||||
|
||||
// 章节
|
||||
_buildLabel(colors, Icons.bookmark_outlined, '章节'),
|
||||
const SizedBox(height: 8),
|
||||
_buildInput(
|
||||
colors: colors,
|
||||
controller: _chapterController,
|
||||
hintText: '例如:第一章、第3节',
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
const SizedBox(height: 24),
|
||||
// 摘抄内容
|
||||
_buildLabel(colors, Icons.format_quote, '摘抄内容', required: true),
|
||||
const SizedBox(height: 8),
|
||||
_buildContentInput(colors),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// 摘抄内容
|
||||
TextFormField(
|
||||
controller: _contentController,
|
||||
maxLines: 8,
|
||||
decoration: InputDecoration(
|
||||
labelText: '摘抄内容',
|
||||
hintText: '输入你想要摘抄的内容...',
|
||||
alignLabelWithHint: true,
|
||||
border: const OutlineInputBorder(
|
||||
borderRadius: BorderRadius.zero,
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.zero,
|
||||
borderSide: BorderSide(color: colors.primary),
|
||||
),
|
||||
),
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return '请输入摘抄内容';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// 评论/感悟
|
||||
TextFormField(
|
||||
controller: _commentController,
|
||||
maxLines: 5,
|
||||
decoration: InputDecoration(
|
||||
labelText: '我的感悟(可选)',
|
||||
hintText: '记录你对这段内容的思考和感悟...',
|
||||
alignLabelWithHint: true,
|
||||
border: const OutlineInputBorder(
|
||||
borderRadius: BorderRadius.zero,
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.zero,
|
||||
borderSide: BorderSide(color: colors.primary),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
// 我的感悟
|
||||
_buildLabel(colors, Icons.lightbulb_outline, '我的感悟'),
|
||||
const SizedBox(height: 8),
|
||||
_buildCommentInput(colors),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// ── 所属书籍 ──
|
||||
|
||||
Widget _buildBookInfo(ColorScheme colors) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.primary.withValues(alpha: 0.08),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.menu_book_rounded, size: 18, color: colors.primary),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
_bookTitle,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(fontSize: 13, fontWeight: FontWeight.w500, color: colors.onSurface.withValues(alpha: 0.7)),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// ── 标签 ──
|
||||
|
||||
Widget _buildLabel(ColorScheme colors, IconData icon, String title, {bool required = false}) {
|
||||
return Row(
|
||||
children: [
|
||||
Icon(icon, size: 16, color: colors.onSurface.withValues(alpha: 0.4)),
|
||||
const SizedBox(width: 6),
|
||||
Text(title, style: TextStyle(fontSize: 13, fontWeight: FontWeight.w600, color: colors.onSurface.withValues(alpha: 0.5))),
|
||||
if (required) ...[
|
||||
const SizedBox(width: 2),
|
||||
Text(' *', style: TextStyle(fontSize: 13, color: colors.error)),
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
// ── 章节输入框 ──
|
||||
|
||||
Widget _buildInput({
|
||||
required ColorScheme colors,
|
||||
required TextEditingController controller,
|
||||
String? hintText,
|
||||
}) {
|
||||
return TextFormField(
|
||||
controller: controller,
|
||||
style: TextStyle(fontSize: 15, color: colors.onSurface),
|
||||
decoration: _inputDecoration(colors, hintText: hintText),
|
||||
);
|
||||
}
|
||||
|
||||
// ── 摘抄内容输入框 ──
|
||||
|
||||
Widget _buildContentInput(ColorScheme colors) {
|
||||
return TextFormField(
|
||||
controller: _contentController,
|
||||
maxLines: null,
|
||||
minLines: 6,
|
||||
style: TextStyle(fontSize: 15, height: 1.8, color: colors.onSurface),
|
||||
textAlignVertical: TextAlignVertical.top,
|
||||
decoration: _inputDecoration(
|
||||
colors,
|
||||
hintText: '在这里粘贴或输入书中的原文段落…',
|
||||
charCount: _contentChars,
|
||||
),
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) return '请输入摘抄内容';
|
||||
return null;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// ── 感悟输入框 ──
|
||||
|
||||
Widget _buildCommentInput(ColorScheme colors) {
|
||||
return TextFormField(
|
||||
controller: _commentController,
|
||||
maxLines: null,
|
||||
minLines: 3,
|
||||
style: TextStyle(fontSize: 15, height: 1.8, color: colors.onSurface),
|
||||
textAlignVertical: TextAlignVertical.top,
|
||||
decoration: _inputDecoration(
|
||||
colors,
|
||||
hintText: '记录思考、联想或评论…',
|
||||
charCount: _commentChars,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// ── 统一输入框样式 ──
|
||||
|
||||
InputDecoration _inputDecoration(ColorScheme colors, {String? hintText, int? charCount}) {
|
||||
return InputDecoration(
|
||||
hintText: hintText,
|
||||
hintStyle: TextStyle(fontSize: 14, color: colors.onSurface.withValues(alpha: 0.25)),
|
||||
filled: true,
|
||||
fillColor: colors.surfaceContainerHigh,
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 14, vertical: 14),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
borderSide: BorderSide.none,
|
||||
),
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
borderSide: BorderSide(color: colors.outlineVariant.withValues(alpha: 0.3), width: 1),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
borderSide: BorderSide(color: colors.primary, width: 1.5),
|
||||
),
|
||||
errorBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
borderSide: BorderSide(color: colors.error, width: 1),
|
||||
),
|
||||
focusedErrorBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
borderSide: BorderSide(color: colors.error, width: 1.5),
|
||||
),
|
||||
counterText: '',
|
||||
suffix: charCount != null
|
||||
? Padding(
|
||||
padding: const EdgeInsets.only(bottom: 2),
|
||||
child: Text('$charCount字', style: TextStyle(fontSize: 11, color: colors.onSurface.withValues(alpha: 0.3))),
|
||||
)
|
||||
: null,
|
||||
);
|
||||
}
|
||||
|
||||
// ─── 保存逻辑 ─────────────────────────────────────────────
|
||||
|
||||
Future<void> _saveExcerpt() async {
|
||||
if (!_formKey.currentState!.validate()) return;
|
||||
|
||||
setState(() => _isLoading = true);
|
||||
|
||||
try {
|
||||
final now = DateTime.now();
|
||||
final excerpt = BookExcerpt(
|
||||
@@ -164,25 +286,19 @@ class _BookExcerptFormPageState extends State<BookExcerptFormPage> {
|
||||
createdAt: _isEditing ? widget.excerpt!.createdAt : now,
|
||||
updatedAt: now,
|
||||
);
|
||||
|
||||
if (_isEditing) {
|
||||
await context.read<AppProvider>().updateBookExcerpt(excerpt);
|
||||
} else {
|
||||
await context.read<AppProvider>().addBookExcerpt(excerpt);
|
||||
}
|
||||
|
||||
if (mounted) {
|
||||
Navigator.pop(context);
|
||||
ToastUtil.show(context, _isEditing ? '摘抄已更新' : '摘抄已添加');
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ToastUtil.show(context, '保存失败: $e');
|
||||
}
|
||||
if (mounted) ToastUtil.show(context, '保存失败: $e');
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() => _isLoading = false);
|
||||
}
|
||||
if (mounted) setState(() => _isLoading = false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import 'package:provider/provider.dart';
|
||||
import '../../providers/app_provider.dart';
|
||||
import '../../models/data_models.dart';
|
||||
import '../../utils/toast_util.dart';
|
||||
import '../../utils/epub/reader_dao.dart';
|
||||
import 'book_excerpt_form_page.dart';
|
||||
|
||||
/// 书籍摘抄列表页面
|
||||
@@ -29,13 +30,15 @@ class _BookExcerptsPageState extends State<BookExcerptsPage> {
|
||||
setState(() => _isLoading = true);
|
||||
try {
|
||||
final excerpts = await context.read<AppProvider>().getBookExcerpts(widget.book.id);
|
||||
setState(() {
|
||||
_excerpts = excerpts;
|
||||
_isLoading = false;
|
||||
});
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_excerpts = excerpts;
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
setState(() => _isLoading = false);
|
||||
ToastUtil.show(context, '加载失败: $e');
|
||||
if (mounted) setState(() => _isLoading = false);
|
||||
if (mounted) ToastUtil.show(context, '加载失败: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,282 +48,280 @@ class _BookExcerptsPageState extends State<BookExcerptsPage> {
|
||||
return Scaffold(
|
||||
backgroundColor: colors.surface,
|
||||
appBar: AppBar(
|
||||
title: const Text('摘抄'),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.add),
|
||||
onPressed: () => _navigateToAddExcerpt(),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
],
|
||||
title: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text('摘抄', style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600)),
|
||||
if (_excerpts.isNotEmpty)
|
||||
Text('共 ${_excerpts.length} 条', style: TextStyle(fontSize: 11, color: colors.onSurface.withValues(alpha: 0.4))),
|
||||
],
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton.extended(
|
||||
onPressed: _navigateToAddExcerpt,
|
||||
icon: const Icon(Icons.add, size: 20),
|
||||
label: const Text('添加摘抄'),
|
||||
),
|
||||
body: _isLoading
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
? Center(child: CircularProgressIndicator(color: colors.primary))
|
||||
: _excerpts.isEmpty
|
||||
? _buildEmptyState()
|
||||
: _buildExcerptList(),
|
||||
? _buildEmptyState(colors)
|
||||
: _buildExcerptList(colors),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildEmptyState() {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
// ── 空状态 ──
|
||||
|
||||
Widget _buildEmptyState(ColorScheme colors) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Container(
|
||||
width: 80,
|
||||
height: 80,
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHighest,
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
child: Icon(
|
||||
Icons.format_quote_outlined,
|
||||
size: 40,
|
||||
color: colors.onSurface.withValues(alpha: 0.25),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
Text(
|
||||
'暂无摘抄',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
color: colors.onSurface.withValues(alpha: 0.4),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
InkWell(
|
||||
onTap: () => _navigateToAddExcerpt(),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(40),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Container(
|
||||
width: 80,
|
||||
height: 80,
|
||||
decoration: BoxDecoration(
|
||||
color: colors.primary,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text(
|
||||
'添加记录',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: colors.onPrimary,
|
||||
),
|
||||
color: colors.surfaceContainerHighest,
|
||||
borderRadius: BorderRadius.circular(24),
|
||||
),
|
||||
child: Icon(Icons.format_quote_outlined, size: 40, color: colors.onSurface.withValues(alpha: 0.2)),
|
||||
),
|
||||
),
|
||||
],
|
||||
const SizedBox(height: 20),
|
||||
Text('暂无摘抄', style: TextStyle(fontSize: 16, color: colors.onSurface.withValues(alpha: 0.4))),
|
||||
const SizedBox(height: 8),
|
||||
Text('记录书中触动人心的文字', style: TextStyle(fontSize: 13, color: colors.onSurface.withValues(alpha: 0.25))),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 按章节分组摘抄数据
|
||||
Map<String, List<BookExcerpt>> _groupExcerptsByChapter() {
|
||||
final Map<String, List<BookExcerpt>> groups = {};
|
||||
|
||||
for (final excerpt in _excerpts) {
|
||||
final chapter = excerpt.chapter.isEmpty ? '未分类' : excerpt.chapter;
|
||||
if (!groups.containsKey(chapter)) {
|
||||
groups[chapter] = [];
|
||||
}
|
||||
groups[chapter]!.add(excerpt);
|
||||
}
|
||||
|
||||
// 每个章节内的摘抄按时间排序(新的在前)
|
||||
for (final chapter in groups.keys) {
|
||||
groups[chapter]!.sort((a, b) => b.createdAt.compareTo(a.createdAt));
|
||||
}
|
||||
|
||||
return groups;
|
||||
}
|
||||
|
||||
Widget _buildExcerptList() {
|
||||
final groups = _groupExcerptsByChapter();
|
||||
final chapters = groups.keys.toList();
|
||||
// ── 摘抄列表 ──
|
||||
|
||||
Widget _buildExcerptList(ColorScheme colors) {
|
||||
final chapters = _groupExcerptsByChapter().keys.toList();
|
||||
return ListView.builder(
|
||||
padding: const EdgeInsets.all(16),
|
||||
padding: const EdgeInsets.fromLTRB(16, 8, 16, 80),
|
||||
itemCount: chapters.length,
|
||||
itemBuilder: (context, index) {
|
||||
final chapter = chapters[index];
|
||||
final excerpts = groups[chapter]!;
|
||||
return _buildChapterSection(chapter, excerpts);
|
||||
final excerpts = _groupExcerptsByChapter()[chapter]!;
|
||||
return _buildChapterSection(chapter, excerpts, colors, index);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildChapterSection(String chapter, List<BookExcerpt> excerpts) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 章节标题
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHighest,
|
||||
border: Border(
|
||||
left: BorderSide(color: colors.primary, width: 4),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
chapter,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: colors.onSurface,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
// 该章节下的摘抄列表
|
||||
...excerpts.map((excerpt) => _buildExcerptItem(excerpt)),
|
||||
const SizedBox(height: 24),
|
||||
],
|
||||
);
|
||||
/// 按章节分组
|
||||
Map<String, List<BookExcerpt>> _groupExcerptsByChapter() {
|
||||
final groups = <String, List<BookExcerpt>>{};
|
||||
for (final e in _excerpts) {
|
||||
final chapter = e.chapter.isEmpty ? '未分类' : e.chapter;
|
||||
(groups[chapter] ??= []).add(e);
|
||||
}
|
||||
for (final key in groups.keys) {
|
||||
groups[key]!.sort((a, b) => b.createdAt.compareTo(a.createdAt));
|
||||
}
|
||||
return groups;
|
||||
}
|
||||
|
||||
Widget _buildExcerptItem(BookExcerpt excerpt) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
return Container(
|
||||
margin: const EdgeInsets.only(bottom: 8),
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: colors.outline),
|
||||
),
|
||||
// ── 章节分组 ──
|
||||
|
||||
Widget _buildChapterSection(String chapter, List<BookExcerpt> excerpts, ColorScheme colors, int chapterIndex) {
|
||||
return Padding(
|
||||
padding: EdgeInsets.only(bottom: 20),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 摘抄内容
|
||||
Text(
|
||||
excerpt.content,
|
||||
maxLines: 3,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: colors.onSurface,
|
||||
height: 1.5,
|
||||
// 章节标题
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 4, bottom: 12),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 3,
|
||||
height: 14,
|
||||
decoration: BoxDecoration(
|
||||
color: colors.primary,
|
||||
borderRadius: BorderRadius.circular(1.5),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(chapter, style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600, color: colors.onSurface)),
|
||||
const SizedBox(width: 8),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 7, vertical: 1),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.primary.withValues(alpha: 0.1),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: Text(
|
||||
'${excerpts.length}',
|
||||
style: TextStyle(fontSize: 11, fontWeight: FontWeight.w600, color: colors.primary),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// 评论/感悟
|
||||
if (excerpt.comment.isNotEmpty) ...[
|
||||
const SizedBox(height: 8),
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHighest,
|
||||
),
|
||||
child: Text(
|
||||
excerpt.comment,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: colors.onSurface.withValues(alpha: 0.6),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
const SizedBox(height: 8),
|
||||
|
||||
// 底部:时间 + 操作按钮
|
||||
Row(
|
||||
children: [
|
||||
Text(
|
||||
_formatDate(excerpt.createdAt),
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: colors.onSurface.withValues(alpha: 0.4),
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
// 编辑按钮
|
||||
GestureDetector(
|
||||
onTap: () => _navigateToEditExcerpt(excerpt),
|
||||
child: Icon(
|
||||
Icons.edit_outlined,
|
||||
size: 16,
|
||||
color: colors.onSurface.withValues(alpha: 0.4),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
// 删除按钮
|
||||
GestureDetector(
|
||||
onTap: () => _showDeleteDialog(excerpt),
|
||||
child: Icon(
|
||||
Icons.delete_outline,
|
||||
size: 16,
|
||||
color: colors.error,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
// 摘抄卡片
|
||||
...excerpts.map((excerpt) => Padding(
|
||||
padding: const EdgeInsets.only(bottom: 10),
|
||||
child: _buildExcerptCard(excerpt, colors),
|
||||
)),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _formatDate(DateTime date) {
|
||||
return '${date.year}.${date.month.toString().padLeft(2, '0')}.${date.day.toString().padLeft(2, '0')}';
|
||||
// ── 摘抄卡片 ──
|
||||
|
||||
Widget _buildExcerptCard(BookExcerpt excerpt, ColorScheme colors) {
|
||||
return Dismissible(
|
||||
key: ValueKey(excerpt.id),
|
||||
direction: DismissDirection.endToStart,
|
||||
background: Container(
|
||||
alignment: Alignment.centerRight,
|
||||
padding: const EdgeInsets.only(right: 20),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.error,
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
),
|
||||
child: const Icon(Icons.delete_outline, color: Colors.white),
|
||||
),
|
||||
confirmDismiss: (direction) => _showDeleteDialog(excerpt),
|
||||
child: Material(
|
||||
color: colors.surfaceContainerHigh,
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: InkWell(
|
||||
onTap: () => _navigateToEditExcerpt(excerpt),
|
||||
child: Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.fromLTRB(16, 14, 14, 12),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 引号 + 摘抄内容
|
||||
Stack(
|
||||
children: [
|
||||
Positioned(
|
||||
left: -6,
|
||||
top: -6,
|
||||
child: Text(
|
||||
'"',
|
||||
style: TextStyle(
|
||||
fontSize: 32,
|
||||
color: colors.primary.withValues(alpha: 0.15),
|
||||
fontWeight: FontWeight.bold,
|
||||
height: 1,
|
||||
),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 10),
|
||||
child: Text(
|
||||
excerpt.content,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: colors.onSurface.withValues(alpha: 0.85),
|
||||
height: 1.8,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
// 感悟
|
||||
if (excerpt.comment.isNotEmpty) ...[
|
||||
const SizedBox(height: 10),
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surface,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border(
|
||||
left: BorderSide(color: colors.primary.withValues(alpha: 0.3), width: 2),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
excerpt.comment,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: colors.onSurface.withValues(alpha: 0.55),
|
||||
height: 1.6,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
// 底部:日期 + 操作
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
children: [
|
||||
Icon(Icons.access_time, size: 12, color: colors.onSurface.withValues(alpha: 0.25)),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
_formatDate(excerpt.createdAt),
|
||||
style: TextStyle(fontSize: 11, color: colors.onSurface.withValues(alpha: 0.3)),
|
||||
),
|
||||
const Spacer(),
|
||||
GestureDetector(
|
||||
onTap: () => _navigateToEditExcerpt(excerpt),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
child: Icon(Icons.edit_outlined, size: 16, color: colors.onSurface.withValues(alpha: 0.35)),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _formatDate(DateTime date) => '${date.year}.${date.month.toString().padLeft(2, '0')}.${date.day.toString().padLeft(2, '0')}';
|
||||
|
||||
void _navigateToAddExcerpt() {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => BookExcerptFormPage(bookId: widget.book.id),
|
||||
),
|
||||
).then((_) => _loadExcerpts());
|
||||
Navigator.push(context, MaterialPageRoute(builder: (ctx) => BookExcerptFormPage(bookId: widget.book.id))).then((_) => _loadExcerpts());
|
||||
}
|
||||
|
||||
void _navigateToEditExcerpt(BookExcerpt excerpt) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => BookExcerptFormPage(
|
||||
bookId: widget.book.id,
|
||||
excerpt: excerpt,
|
||||
),
|
||||
),
|
||||
).then((_) => _loadExcerpts());
|
||||
Navigator.push(context, MaterialPageRoute(builder: (ctx) => BookExcerptFormPage(bookId: widget.book.id, excerpt: excerpt))).then((_) => _loadExcerpts());
|
||||
}
|
||||
|
||||
void _showDeleteDialog(BookExcerpt excerpt) {
|
||||
showDialog(
|
||||
Future<bool?> _showDeleteDialog(BookExcerpt excerpt) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
return showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
return AlertDialog(
|
||||
backgroundColor: colors.surface,
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||||
title: Text('确认删除', style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600, color: colors.onSurface)),
|
||||
content: Text('确定要删除这条摘抄吗?删除后可在回收站恢复。',
|
||||
style: TextStyle(fontSize: 14, color: colors.onSurface.withValues(alpha: 0.6), height: 1.5)),
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14)),
|
||||
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(context, false), child: Text('取消', style: TextStyle(color: colors.onSurface.withValues(alpha: 0.4)))),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: Text('取消', style: TextStyle(color: colors.onSurface.withValues(alpha: 0.6))),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
Navigator.pop(context, true);
|
||||
final readerBook = await ReaderDao().getReaderBookByBookId(widget.book.id);
|
||||
if (readerBook != null) {
|
||||
await ReaderDao().deleteExcerptHighlightByContent(
|
||||
readerBook['id'] as String,
|
||||
excerpt.content,
|
||||
);
|
||||
}
|
||||
await context.read<AppProvider>().removeBookExcerpt(excerpt.id);
|
||||
Navigator.pop(context);
|
||||
_loadExcerpts();
|
||||
ToastUtil.show(context, '已删除');
|
||||
if (mounted) ToastUtil.show(context, '已删除');
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: colors.error, foregroundColor: colors.onError, elevation: 0,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
),
|
||||
child: const Text('删除'),
|
||||
child: Text('删除', style: TextStyle(color: colors.error, fontWeight: FontWeight.w600)),
|
||||
),
|
||||
],
|
||||
actionsPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -32,7 +32,9 @@ class _BookTabPageState extends State<BookTabPage> {
|
||||
late ScrollController _scrollController;
|
||||
AppProvider? _provider;
|
||||
int _lastScrollSignal = 0;
|
||||
int _lastEditRefreshCounter = 0;
|
||||
int _prevBookCount = -1;
|
||||
double _dragDelta = 0.0; // 当前拖动偏移量
|
||||
|
||||
void _onBookTap(Book book) {
|
||||
if (Breakpoint.isWideContent(context)) {
|
||||
@@ -79,10 +81,14 @@ class _BookTabPageState extends State<BookTabPage> {
|
||||
// 仅在数据实际变化时刷新列表,避免底部导航栏显隐等UI变化误触发重载
|
||||
final statusChanged = provider.bookStatusIndex != _lastStatusIndex;
|
||||
final countChanged = provider.books.length != _prevBookCount;
|
||||
if (statusChanged || countChanged) {
|
||||
final editRefreshed = provider.editRefreshCounter > _lastEditRefreshCounter;
|
||||
if (statusChanged || countChanged || editRefreshed) {
|
||||
_prevBookCount = provider.books.length;
|
||||
_loadFirst();
|
||||
}
|
||||
if (editRefreshed) {
|
||||
_lastEditRefreshCounter = provider.editRefreshCounter;
|
||||
}
|
||||
}
|
||||
|
||||
void _onScroll() {
|
||||
@@ -140,19 +146,49 @@ class _BookTabPageState extends State<BookTabPage> {
|
||||
|
||||
Widget _buildBody(BuildContext context) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
return Consumer<AppProvider>(builder: (context, provider, _) {
|
||||
if (_initialized && provider.bookStatusIndex != _lastStatusIndex) {
|
||||
_lastStatusIndex = provider.bookStatusIndex;
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => _loadFirst());
|
||||
}
|
||||
if (_items.isEmpty && _isLoading) return _buildSkeleton();
|
||||
if (_items.isEmpty) {
|
||||
return RefreshIndicator(onRefresh: _refresh, color: colors.primary, backgroundColor: colors.surface,
|
||||
child: ListView(physics: const AlwaysScrollableScrollPhysics(), children: [_buildEmptyState(context, provider.bookStatusIndex)]));
|
||||
}
|
||||
return RefreshIndicator(onRefresh: _refresh, color: colors.primary, backgroundColor: colors.surface,
|
||||
child: _layoutStyle == 1 ? _buildListView() : _buildGridView());
|
||||
});
|
||||
|
||||
// 用 GestureDetector 包裹,左右滑动切换状态
|
||||
return GestureDetector(
|
||||
onHorizontalDragStart: (_) => _dragDelta = 0.0,
|
||||
onHorizontalDragUpdate: (details) => setState(() => _dragDelta += details.primaryDelta ?? 0),
|
||||
onHorizontalDragEnd: (details) {
|
||||
final velocity = details.primaryVelocity;
|
||||
if ((velocity ?? 0).abs() < 80) {
|
||||
setState(() => _dragDelta = 0.0);
|
||||
return;
|
||||
}
|
||||
final direction = (velocity ?? 0) > 0 ? -1 : 1; // 右滑→上一个,左滑→下一个
|
||||
final provider = context.read<AppProvider>();
|
||||
final currentIndex = provider.bookStatusIndex;
|
||||
final newIndex = (currentIndex + direction + 3) % 3;
|
||||
setState(() => _dragDelta = 0.0);
|
||||
provider.setBookStatusIndex(newIndex);
|
||||
},
|
||||
child: TweenAnimationBuilder<double>(
|
||||
tween: Tween(begin: 0.0, end: _dragDelta.clamp(-100.0, 100.0)),
|
||||
duration: const Duration(milliseconds: 150),
|
||||
curve: Curves.easeOut,
|
||||
builder: (context, value, child) {
|
||||
return Transform.translate(offset: Offset(value, 0), child: child);
|
||||
},
|
||||
child: Consumer<AppProvider>(builder: (context, provider, _) {
|
||||
if (_initialized && provider.bookStatusIndex != _lastStatusIndex) {
|
||||
_lastStatusIndex = provider.bookStatusIndex;
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => _loadFirst());
|
||||
}
|
||||
final content = () {
|
||||
if (_items.isEmpty && _isLoading) return _buildSkeleton();
|
||||
if (_items.isEmpty) {
|
||||
return RefreshIndicator(onRefresh: _refresh, color: colors.primary, backgroundColor: colors.surface,
|
||||
child: ListView(physics: const AlwaysScrollableScrollPhysics(), children: [_buildEmptyState(context, provider.bookStatusIndex)]));
|
||||
}
|
||||
return RefreshIndicator(onRefresh: _refresh, color: colors.primary, backgroundColor: colors.surface,
|
||||
child: _layoutStyle == 1 ? _buildListView() : _buildGridView());
|
||||
}();
|
||||
return content;
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildGridView() {
|
||||
|
||||
Reference in New Issue
Block a user