generated from dellevin/template
代码优化,epub阅读标签
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../utils/book/book_dao.dart';
|
||||
import '../../utils/epub/reader_dao.dart';
|
||||
import '../../utils/toast_util.dart';
|
||||
import '../../models/data_models.dart';
|
||||
|
||||
/// 选择关联书籍页面(带搜索功能)
|
||||
@@ -63,9 +64,7 @@ class _BookLinkPageState extends State<BookLinkPage> {
|
||||
Future<void> _selectBook(Book book) async {
|
||||
await _readerDao.linkToBook(widget.readerBookId, book.id);
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('已关联《${book.title}》')),
|
||||
);
|
||||
ToastUtil.show(context, '已关联《${book.title}》');
|
||||
Navigator.pop(context, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,13 @@ class ControlPanel extends StatefulWidget {
|
||||
final ValueChanged<double> onMarginBottomChanged;
|
||||
final ValueChanged<double> onMarginLeftChanged;
|
||||
final ValueChanged<double> onMarginRightChanged;
|
||||
final int themeIndex;
|
||||
final int customBgColor;
|
||||
final int customTextColor;
|
||||
final ValueChanged<int> onThemeIndexChanged;
|
||||
final void Function(int bgColor, int textColor) onCustomColorChanged;
|
||||
final bool currentPageHasBookmark;
|
||||
final VoidCallback onBookmarkToggle;
|
||||
|
||||
const ControlPanel({
|
||||
super.key,
|
||||
@@ -64,6 +71,13 @@ class ControlPanel extends StatefulWidget {
|
||||
required this.onMarginBottomChanged,
|
||||
required this.onMarginLeftChanged,
|
||||
required this.onMarginRightChanged,
|
||||
required this.themeIndex,
|
||||
required this.customBgColor,
|
||||
required this.customTextColor,
|
||||
required this.onThemeIndexChanged,
|
||||
required this.onCustomColorChanged,
|
||||
required this.currentPageHasBookmark,
|
||||
required this.onBookmarkToggle,
|
||||
});
|
||||
|
||||
bool get isVertical => direction == 1;
|
||||
@@ -246,6 +260,11 @@ class _ControlPanelState extends State<ControlPanel> {
|
||||
onMarginBottomChanged: widget.onMarginBottomChanged,
|
||||
onMarginLeftChanged: widget.onMarginLeftChanged,
|
||||
onMarginRightChanged: widget.onMarginRightChanged,
|
||||
themeIndex: widget.themeIndex,
|
||||
customBgColor: widget.customBgColor,
|
||||
customTextColor: widget.customTextColor,
|
||||
onThemeIndexChanged: widget.onThemeIndexChanged,
|
||||
onCustomColorChanged: widget.onCustomColorChanged,
|
||||
),
|
||||
),
|
||||
],
|
||||
@@ -293,6 +312,16 @@ class _ControlPanelState extends State<ControlPanel> {
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: Icon(
|
||||
widget.currentPageHasBookmark
|
||||
? Icons.bookmark
|
||||
: Icons.bookmark_outline,
|
||||
),
|
||||
onPressed: widget.onBookmarkToggle,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -7,7 +7,9 @@ import '../../utils/epub/epub_parser.dart';
|
||||
import '../../utils/epub/reader_models.dart';
|
||||
import '../../utils/book/book_dao.dart';
|
||||
import '../../models/data_models.dart';
|
||||
import '../book/book_detail_page.dart';
|
||||
import 'book_link_page.dart';
|
||||
import 'epub_edit_page.dart';
|
||||
import 'reader_screen.dart';
|
||||
|
||||
/// EPUB 书籍详情页
|
||||
@@ -68,80 +70,14 @@ class _EpubDetailPageState extends State<EpubDetailPage> {
|
||||
).then((_) => _refreshBook());
|
||||
}
|
||||
|
||||
// ─── 编辑对话框 ─────────────────────────────────────────────
|
||||
|
||||
void _showEditDialog() {
|
||||
final titleCtrl = TextEditingController(text: _book['title'] as String? ?? '');
|
||||
final authorCtrl = TextEditingController(text: _book['author'] as String? ?? '');
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (ctx) => 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: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
TextField(
|
||||
controller: titleCtrl,
|
||||
decoration: InputDecoration(
|
||||
labelText: '标题',
|
||||
labelStyle: TextStyle(fontSize: 13, color: colors.onSurface.withValues(alpha: 0.4)),
|
||||
border: OutlineInputBorder(borderRadius: BorderRadius.circular(8)),
|
||||
),
|
||||
style: const TextStyle(fontSize: 14),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
controller: authorCtrl,
|
||||
decoration: InputDecoration(
|
||||
labelText: '作者',
|
||||
labelStyle: TextStyle(fontSize: 13, color: colors.onSurface.withValues(alpha: 0.4)),
|
||||
border: OutlineInputBorder(borderRadius: BorderRadius.circular(8)),
|
||||
),
|
||||
style: const TextStyle(fontSize: 14),
|
||||
),
|
||||
],
|
||||
),
|
||||
actionsPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
actions: [
|
||||
TextButton(
|
||||
style: TextButton.styleFrom(
|
||||
foregroundColor: colors.onSurface.withValues(alpha: 0.6),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
),
|
||||
onPressed: () => Navigator.pop(ctx),
|
||||
child: const Text('取消'),
|
||||
),
|
||||
ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: colors.primary,
|
||||
foregroundColor: colors.onPrimary,
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
),
|
||||
onPressed: () async {
|
||||
final newTitle = titleCtrl.text.trim();
|
||||
final newAuthor = authorCtrl.text.trim();
|
||||
if (newTitle.isEmpty) return;
|
||||
await _dao.updateReaderBook(widget.bookId, {
|
||||
'title': newTitle,
|
||||
'author': newAuthor,
|
||||
'updated_at': DateTime.now().toIso8601String(),
|
||||
});
|
||||
if (ctx.mounted) Navigator.pop(ctx);
|
||||
await _refreshBook();
|
||||
},
|
||||
child: const Text('保存'),
|
||||
),
|
||||
],
|
||||
void _navigateToEdit() async {
|
||||
final changed = await Navigator.push<bool>(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => EpubEditPage(bookId: widget.bookId, book: _book),
|
||||
),
|
||||
);
|
||||
if (changed == true && mounted) await _refreshBook();
|
||||
}
|
||||
|
||||
// ─── Build ──────────────────────────────────────────────────
|
||||
@@ -166,7 +102,7 @@ class _EpubDetailPageState extends State<EpubDetailPage> {
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: Icon(Icons.edit_outlined, size: 20, color: colors.onSurface.withValues(alpha: 0.6)),
|
||||
onPressed: _showEditDialog,
|
||||
onPressed: _navigateToEdit,
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
],
|
||||
@@ -316,21 +252,35 @@ class _EpubDetailPageState extends State<EpubDetailPage> {
|
||||
Widget _buildLinkedBookCard(ColorScheme colors) {
|
||||
final linkedBookId = _book['book_id'] as String? ?? '';
|
||||
if (linkedBookId.isEmpty) {
|
||||
return InkWell(
|
||||
return GestureDetector(
|
||||
onTap: _navigateToLinkPage,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12),
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHighest,
|
||||
color: colors.surfaceContainerHigh,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(color: colors.outlineVariant, width: 0.5),
|
||||
),
|
||||
child: Row(children: [
|
||||
Icon(Icons.link_outlined, size: 18, color: colors.onSurface.withValues(alpha: 0.4)),
|
||||
const SizedBox(width: 10),
|
||||
Container(
|
||||
width: 40, height: 40,
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surface,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: colors.outlineVariant, width: 0.5),
|
||||
),
|
||||
child: Icon(Icons.link_outlined, size: 20, color: colors.onSurface.withValues(alpha: 0.6)),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Text('选择关联书籍',
|
||||
style: TextStyle(fontSize: 14, color: colors.onSurface.withValues(alpha: 0.5))),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('关联书籍', style: TextStyle(fontSize: 15, fontWeight: FontWeight.w600, color: colors.onSurface)),
|
||||
const SizedBox(height: 4),
|
||||
Text('点击选择要关联的书籍', style: TextStyle(fontSize: 13, color: colors.onSurface.withValues(alpha: 0.4))),
|
||||
],
|
||||
),
|
||||
),
|
||||
Icon(Icons.chevron_right, size: 18, color: colors.onSurface.withValues(alpha: 0.25)),
|
||||
]),
|
||||
@@ -342,35 +292,97 @@ class _EpubDetailPageState extends State<EpubDetailPage> {
|
||||
future: _bookDao.getBookById(linkedBookId),
|
||||
builder: (context, snapshot) {
|
||||
final linkedTitle = snapshot.data?.title ?? '未知书籍';
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHighest,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: Row(children: [
|
||||
Icon(Icons.link_outlined, size: 18, color: colors.primary),
|
||||
const SizedBox(width: 10),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('已关联', style: TextStyle(fontSize: 11, color: colors.onSurface.withValues(alpha: 0.4))),
|
||||
Text(linkedTitle, maxLines: 1, overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(fontSize: 14, fontWeight: FontWeight.w500, color: colors.onSurface)),
|
||||
],
|
||||
final linkedAuthor = snapshot.data?.authors.take(2).join(' / ') ?? '';
|
||||
return GestureDetector(
|
||||
onTap: () => _showLinkedBookActions(colors, linkedTitle),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHigh,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(color: colors.outlineVariant, width: 0.5),
|
||||
),
|
||||
child: Row(children: [
|
||||
Container(
|
||||
width: 40, height: 40,
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surface,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: colors.outlineVariant, width: 0.5),
|
||||
),
|
||||
child: Icon(Icons.link, size: 20, color: colors.primary),
|
||||
),
|
||||
),
|
||||
GestureDetector(
|
||||
onTap: _unlinkBook,
|
||||
child: Icon(Icons.close, size: 16, color: colors.onSurface.withValues(alpha: 0.35)),
|
||||
),
|
||||
]),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(linkedTitle, maxLines: 1, overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(fontSize: 15, fontWeight: FontWeight.w600, color: colors.onSurface)),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
linkedAuthor.isNotEmpty ? '已关联 · $linkedAuthor' : '已关联',
|
||||
style: TextStyle(fontSize: 13, color: colors.onSurface.withValues(alpha: 0.4)),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Icon(Icons.chevron_right, size: 18, color: colors.onSurface.withValues(alpha: 0.25)),
|
||||
]),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _showLinkedBookActions(ColorScheme colors, String title) {
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
backgroundColor: colors.surface,
|
||||
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.vertical(top: Radius.circular(16))),
|
||||
builder: (ctx) => SafeArea(
|
||||
child: Column(mainAxisSize: MainAxisSize.min, children: [
|
||||
Container(width: 36, height: 4, margin: const EdgeInsets.only(top: 12, bottom: 16),
|
||||
decoration: BoxDecoration(color: colors.onSurface.withValues(alpha: 0.15), borderRadius: BorderRadius.circular(2))),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 20),
|
||||
child: Align(alignment: Alignment.centerLeft,
|
||||
child: Text(title, maxLines: 1, overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: colors.onSurface))),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Divider(height: 0.5, color: colors.outlineVariant),
|
||||
ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 20),
|
||||
leading: Container(width: 36, height: 36,
|
||||
decoration: BoxDecoration(color: colors.surfaceContainerHighest, borderRadius: BorderRadius.circular(10)),
|
||||
child: Icon(Icons.menu_book_outlined, size: 18, color: colors.onSurface.withValues(alpha: 0.6))),
|
||||
title: Text('查看书籍详情', style: TextStyle(fontSize: 13, color: colors.onSurface)),
|
||||
onTap: () async {
|
||||
Navigator.pop(ctx);
|
||||
final bookId = _book['book_id'] as String? ?? '';
|
||||
if (bookId.isEmpty) return;
|
||||
final book = await _bookDao.getBookById(bookId);
|
||||
if (book != null && mounted) {
|
||||
Navigator.push(context, MaterialPageRoute(builder: (_) => BookDetailPage(book: book)));
|
||||
}
|
||||
},
|
||||
),
|
||||
Divider(height: 0.5, indent: 20, endIndent: 20, color: colors.outlineVariant),
|
||||
ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 20),
|
||||
leading: Container(width: 36, height: 36,
|
||||
decoration: BoxDecoration(color: colors.surfaceContainerHighest, borderRadius: BorderRadius.circular(10)),
|
||||
child: Icon(Icons.link_off, size: 18, color: colors.error)),
|
||||
title: Text('取消关联', style: TextStyle(fontSize: 13, color: colors.error)),
|
||||
onTap: () { Navigator.pop(ctx); _unlinkBook(); },
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
]),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _navigateToLinkPage() async {
|
||||
final linked = await Navigator.push<bool>(
|
||||
context,
|
||||
|
||||
338
lib/pages/epub_reader/epub_edit_page.dart
Normal file
338
lib/pages/epub_reader/epub_edit_page.dart
Normal file
@@ -0,0 +1,338 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
import '../../utils/epub/reader_dao.dart';
|
||||
import '../../widgets/text_input_panel.dart';
|
||||
|
||||
/// EPUB 书籍编辑页
|
||||
class EpubEditPage extends StatefulWidget {
|
||||
final String bookId;
|
||||
final Map<String, dynamic> book;
|
||||
|
||||
const EpubEditPage({
|
||||
super.key,
|
||||
required this.bookId,
|
||||
required this.book,
|
||||
});
|
||||
|
||||
@override
|
||||
State<EpubEditPage> createState() => _EpubEditPageState();
|
||||
}
|
||||
|
||||
class _EpubEditPageState extends State<EpubEditPage> {
|
||||
final ReaderDao _dao = ReaderDao();
|
||||
|
||||
late TextEditingController _titleCtrl;
|
||||
late TextEditingController _authorCtrl;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_titleCtrl = TextEditingController(text: widget.book['title'] as String? ?? '');
|
||||
_authorCtrl = TextEditingController(text: widget.book['author'] as String? ?? '');
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_titleCtrl.dispose();
|
||||
_authorCtrl.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _save() async {
|
||||
final newTitle = _titleCtrl.text.trim();
|
||||
if (newTitle.isEmpty) return;
|
||||
await _dao.updateReaderBook(widget.bookId, {
|
||||
'title': newTitle,
|
||||
'author': _authorCtrl.text.trim(),
|
||||
'updated_at': DateTime.now().toIso8601String(),
|
||||
});
|
||||
if (mounted) Navigator.pop(context, true);
|
||||
}
|
||||
|
||||
Future<void> _pickCover() async {
|
||||
final picker = ImagePicker();
|
||||
final picked = await picker.pickImage(source: ImageSource.gallery, imageQuality: 85);
|
||||
if (picked == null || !mounted) return;
|
||||
|
||||
final appDir = await getApplicationDocumentsDirectory();
|
||||
final bookDir = Directory(p.join(appDir.path, 'epub_books', widget.bookId));
|
||||
if (!await bookDir.exists()) await bookDir.create(recursive: true);
|
||||
|
||||
final existing = bookDir.listSync().whereType<File>().where((f) {
|
||||
final name = p.basenameWithoutExtension(f.path);
|
||||
return name.startsWith('cover_') && RegExp(r'^cover_\d+$').hasMatch(name);
|
||||
}).toList();
|
||||
int nextIndex = 1;
|
||||
if (existing.isNotEmpty) {
|
||||
final indices = existing.map((f) {
|
||||
return int.tryParse(p.basenameWithoutExtension(f.path).substring(6)) ?? 0;
|
||||
}).toList();
|
||||
indices.sort();
|
||||
nextIndex = indices.last + 1;
|
||||
}
|
||||
|
||||
final ext = p.extension(picked.path).toLowerCase();
|
||||
final destPath = p.join(bookDir.path, 'cover_$nextIndex$ext');
|
||||
await File(picked.path).copy(destPath);
|
||||
|
||||
await _dao.updateReaderBook(widget.bookId, {
|
||||
'cover_path': destPath,
|
||||
'updated_at': DateTime.now().toIso8601String(),
|
||||
});
|
||||
if (mounted) Navigator.pop(context, true);
|
||||
}
|
||||
|
||||
Future<void> _revertCover() async {
|
||||
final appDir = await getApplicationDocumentsDirectory();
|
||||
final bookDir = Directory(p.join(appDir.path, 'epub_books', widget.bookId));
|
||||
|
||||
final existing = bookDir.listSync().whereType<File>().where((f) {
|
||||
final name = p.basenameWithoutExtension(f.path);
|
||||
return name.startsWith('cover_') && RegExp(r'^cover_\d+$').hasMatch(name);
|
||||
}).toList();
|
||||
|
||||
if (existing.isEmpty) return;
|
||||
|
||||
existing.sort((a, b) {
|
||||
final ia = int.tryParse(p.basenameWithoutExtension(a.path).substring(6)) ?? 0;
|
||||
final ib = int.tryParse(p.basenameWithoutExtension(b.path).substring(6)) ?? 0;
|
||||
return ia.compareTo(ib);
|
||||
});
|
||||
final currentMax = existing.last;
|
||||
final currentIndex = int.tryParse(p.basenameWithoutExtension(currentMax.path).substring(6)) ?? 0;
|
||||
await currentMax.delete();
|
||||
|
||||
if (currentIndex <= 1) {
|
||||
final coverFile = bookDir.listSync().whereType<File>().where((f) {
|
||||
final name = p.basenameWithoutExtension(f.path);
|
||||
return name == 'cover';
|
||||
}).firstOrNull;
|
||||
await _dao.updateReaderBook(widget.bookId, {
|
||||
'cover_path': coverFile?.path ?? '',
|
||||
'updated_at': DateTime.now().toIso8601String(),
|
||||
});
|
||||
} else {
|
||||
final prev = existing.where((f) {
|
||||
final idx = int.tryParse(p.basenameWithoutExtension(f.path).substring(6)) ?? 0;
|
||||
return idx == currentIndex - 1;
|
||||
}).firstOrNull;
|
||||
await _dao.updateReaderBook(widget.bookId, {
|
||||
'cover_path': prev?.path ?? '',
|
||||
'updated_at': DateTime.now().toIso8601String(),
|
||||
});
|
||||
}
|
||||
if (mounted) Navigator.pop(context, true);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
final coverPath = widget.book['cover_path'] as String?;
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: colors.surface,
|
||||
appBar: AppBar(
|
||||
backgroundColor: colors.surface,
|
||||
elevation: 0,
|
||||
title: Text('编辑书籍信息',
|
||||
style: TextStyle(fontSize: 17, fontWeight: FontWeight.w600, color: colors.onSurface)),
|
||||
leading: IconButton(
|
||||
icon: Icon(Icons.close, size: 20, color: colors.onSurface.withValues(alpha: 0.6)),
|
||||
onPressed: () => Navigator.pop(context),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: _save,
|
||||
child: Text('保存', style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600, color: colors.primary)),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
],
|
||||
),
|
||||
body: ListView(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 16),
|
||||
children: [
|
||||
// 封面选择
|
||||
Center(child: _buildCoverPicker(coverPath, colors)),
|
||||
const SizedBox(height: 24),
|
||||
// 信息卡片
|
||||
Wrap(
|
||||
spacing: 12,
|
||||
runSpacing: 12,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: (MediaQuery.of(context).size.width - 52) / 2,
|
||||
height: 90,
|
||||
child: _buildInfoCard(
|
||||
label: '标题',
|
||||
value: _titleCtrl.text,
|
||||
required: true,
|
||||
icon: Icons.auto_stories_outlined,
|
||||
onTap: () async {
|
||||
final result = await TextInputPanel.show(
|
||||
context: context,
|
||||
title: '书名',
|
||||
initialValue: _titleCtrl.text,
|
||||
hint: '请输入书名',
|
||||
);
|
||||
if (result != null) setState(() => _titleCtrl.text = result);
|
||||
},
|
||||
colors: colors,
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
width: (MediaQuery.of(context).size.width - 52) / 2,
|
||||
height: 90,
|
||||
child: _buildInfoCard(
|
||||
label: '作者',
|
||||
value: _authorCtrl.text,
|
||||
icon: Icons.person_outline,
|
||||
onTap: () async {
|
||||
final result = await TextInputPanel.show(
|
||||
context: context,
|
||||
title: '作者',
|
||||
initialValue: _authorCtrl.text,
|
||||
hint: '请输入作者',
|
||||
);
|
||||
if (result != null) setState(() => _authorCtrl.text = result);
|
||||
},
|
||||
colors: colors,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
// 封面操作
|
||||
Text('封面操作',
|
||||
style: TextStyle(fontSize: 11, fontWeight: FontWeight.w600,
|
||||
color: colors.onSurface.withValues(alpha: 0.4))),
|
||||
const SizedBox(height: 10),
|
||||
Row(children: [
|
||||
Expanded(child: _buildActionCard(
|
||||
icon: Icons.add_photo_alternate_outlined, title: '更换封面', subtitle: '从相册选择',
|
||||
color: colors.primary,
|
||||
onTap: _pickCover,
|
||||
)),
|
||||
const SizedBox(width: 10),
|
||||
Expanded(child: _buildActionCard(
|
||||
icon: Icons.undo, title: '恢复上次', subtitle: '回退到上一个封面',
|
||||
color: colors.onSurface.withValues(alpha: 0.5),
|
||||
onTap: _revertCover,
|
||||
)),
|
||||
]),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// ─── 构建组件 ────────────────────────────────────────────────
|
||||
|
||||
Widget _buildCoverPicker(String? coverPath, ColorScheme colors) {
|
||||
return GestureDetector(
|
||||
onTap: _pickCover,
|
||||
child: Container(
|
||||
width: 110, height: 154,
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHighest,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: colors.outlineVariant, width: 0.5),
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: coverPath != null && coverPath.isNotEmpty && File(coverPath).existsSync()
|
||||
? Image.file(File(coverPath), fit: BoxFit.cover)
|
||||
: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.add_photo_alternate_outlined, size: 28,
|
||||
color: colors.onSurface.withValues(alpha: 0.25)),
|
||||
const SizedBox(height: 6),
|
||||
Text('点击更换',
|
||||
style: TextStyle(fontSize: 11, color: colors.onSurface.withValues(alpha: 0.3))),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildInfoCard({
|
||||
required String label, required String value, required IconData icon,
|
||||
required VoidCallback onTap, required ColorScheme colors,
|
||||
bool required = false,
|
||||
}) {
|
||||
return GestureDetector(
|
||||
onTap: onTap,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHigh,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(color: colors.outlineVariant, width: 0.5),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(children: [
|
||||
Icon(icon, size: 14, color: colors.onSurface.withValues(alpha: 0.4)),
|
||||
const SizedBox(width: 6),
|
||||
Text(label, style: TextStyle(fontSize: 11, color: colors.onSurface.withValues(alpha: 0.4))),
|
||||
if (required)
|
||||
Text(' *', style: TextStyle(fontSize: 11, color: colors.error)),
|
||||
]),
|
||||
const Spacer(),
|
||||
Text(
|
||||
value.isEmpty ? '未设置' : value,
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(
|
||||
fontSize: 14, fontWeight: FontWeight.w500,
|
||||
color: value.isEmpty ? colors.onSurface.withValues(alpha: 0.2) : colors.onSurface,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildActionCard({
|
||||
required IconData icon, required String title, required String subtitle,
|
||||
required Color color, required VoidCallback onTap,
|
||||
}) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
return GestureDetector(
|
||||
onTap: onTap,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(14),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHigh,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(color: colors.outlineVariant, width: 0.5),
|
||||
),
|
||||
child: Row(children: [
|
||||
Container(
|
||||
width: 36, height: 36,
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surface,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: colors.outlineVariant, width: 0.5),
|
||||
),
|
||||
child: Icon(icon, size: 18, color: color),
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
Expanded(child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(title, style: TextStyle(fontSize: 13, fontWeight: FontWeight.w500, color: colors.onSurface)),
|
||||
const SizedBox(height: 2),
|
||||
Text(subtitle, style: TextStyle(fontSize: 11, color: colors.onSurface.withValues(alpha: 0.4))),
|
||||
],
|
||||
)),
|
||||
]),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import '../../utils/epub/reader_dao.dart';
|
||||
@@ -48,6 +49,14 @@ class _EpubLibraryPageState extends State<EpubLibraryPage> {
|
||||
if (result == null || result.files.isEmpty) return;
|
||||
final path = result.files.single.path;
|
||||
if (path == null) return;
|
||||
if (!path.toLowerCase().endsWith('.epub')) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('仅支持导入 .epub 格式的文件')),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!mounted) return;
|
||||
showDialog(
|
||||
@@ -210,29 +219,123 @@ class _EpubLibraryPageState extends State<EpubLibraryPage> {
|
||||
}
|
||||
|
||||
Widget _buildGrid(ColorScheme colors) {
|
||||
final bool isRelaxed = _viewMode == ViewMode.relaxed;
|
||||
final maxExtent = isRelaxed ? 180.0 : 120.0;
|
||||
final aspectRatio = isRelaxed ? 0.55 : 0.68;
|
||||
final spacing = isRelaxed ? 16.0 : 8.0;
|
||||
final bool showList = _viewMode == ViewMode.compact;
|
||||
|
||||
if (showList) {
|
||||
return _buildListView(colors);
|
||||
}
|
||||
return _buildGridView(colors);
|
||||
}
|
||||
|
||||
Widget _buildGridView(ColorScheme colors) {
|
||||
return GridView.builder(
|
||||
padding: const EdgeInsets.fromLTRB(16, 16, 16, 100),
|
||||
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
|
||||
maxCrossAxisExtent: maxExtent,
|
||||
childAspectRatio: aspectRatio,
|
||||
crossAxisSpacing: spacing,
|
||||
mainAxisSpacing: spacing,
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 3,
|
||||
crossAxisSpacing: 12,
|
||||
mainAxisSpacing: 16,
|
||||
childAspectRatio: 0.55,
|
||||
),
|
||||
itemCount: _books.length,
|
||||
itemBuilder: (context, index) {
|
||||
final book = _books[index];
|
||||
return BookGridItem(
|
||||
book: book,
|
||||
viewMode: _viewMode,
|
||||
viewMode: ViewMode.relaxed,
|
||||
onTap: () => _openBook(book),
|
||||
onLongPress: () => _deleteBook(book),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildListView(ColorScheme colors) {
|
||||
return ListView.builder(
|
||||
padding: const EdgeInsets.fromLTRB(16, 8, 16, 100),
|
||||
itemCount: _books.length,
|
||||
itemBuilder: (context, index) {
|
||||
final book = _books[index];
|
||||
final title = book['title'] as String? ?? '';
|
||||
final author = book['author'] as String? ?? '';
|
||||
final coverPath = book['cover_path'] as String?;
|
||||
final progress = (book['reading_percentage'] as num?)?.toDouble() ?? 0.0;
|
||||
|
||||
return GestureDetector(
|
||||
onTap: () => _openBook(book),
|
||||
onLongPress: () => _deleteBook(book),
|
||||
child: Container(
|
||||
margin: const EdgeInsets.only(bottom: 12),
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHigh,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(color: colors.outlineVariant, width: 0.5),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
// 封面
|
||||
SizedBox(
|
||||
width: 56, height: 80,
|
||||
child: _buildCover(coverPath, colors),
|
||||
),
|
||||
const SizedBox(width: 14),
|
||||
// 信息
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(title, maxLines: 1, overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(fontSize: 15, fontWeight: FontWeight.w600, color: colors.onSurface)),
|
||||
if (author.isNotEmpty) ...[
|
||||
const SizedBox(height: 4),
|
||||
Text(author, maxLines: 1, overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(fontSize: 13, color: colors.onSurface.withValues(alpha: 0.5))),
|
||||
],
|
||||
const SizedBox(height: 8),
|
||||
// 标签
|
||||
Wrap(spacing: 6, runSpacing: 4, children: [
|
||||
_buildTag('EPUB', colors),
|
||||
if (progress > 0) _buildTag('${(progress * 100).toInt()}%', colors),
|
||||
]),
|
||||
],
|
||||
),
|
||||
),
|
||||
Icon(Icons.chevron_right, size: 18, color: colors.onSurface.withValues(alpha: 0.25)),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildCover(String? path, ColorScheme colors) {
|
||||
if (path != null && path.isNotEmpty && File(path).existsSync()) {
|
||||
return ClipRRect(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
child: Image.file(File(path), fit: BoxFit.cover,
|
||||
width: double.infinity, height: double.infinity),
|
||||
);
|
||||
}
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHighest,
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
),
|
||||
child: Icon(Icons.auto_stories_outlined, size: 24,
|
||||
color: colors.onSurface.withValues(alpha: 0.25)),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTag(String label, ColorScheme colors) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHighest,
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
child: Text(label,
|
||||
style: TextStyle(fontSize: 10, color: colors.onSurface.withValues(alpha: 0.5))),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,6 +129,9 @@ class _ReaderScreenState extends State<ReaderScreen>
|
||||
bool styleDrawerOpen = false;
|
||||
AppLifecycleState? lastLifecycleState = AppLifecycleState.resumed;
|
||||
|
||||
// 书签
|
||||
final List<Map<String, dynamic>> _bookmarks = [];
|
||||
|
||||
// Services
|
||||
final EpubStreamService _streamService = EpubStreamService();
|
||||
final ReaderDao _readerDao = ReaderDao();
|
||||
@@ -311,6 +314,7 @@ class _ReaderScreenState extends State<ReaderScreen>
|
||||
currentSpineItemIndex = bookSession.initialChapterIndex;
|
||||
});
|
||||
updateProgressDebounced();
|
||||
_loadBookmarks();
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
@@ -322,6 +326,84 @@ class _ReaderScreenState extends State<ReaderScreen>
|
||||
}
|
||||
}
|
||||
|
||||
// ─── 书签 ──────────────────────────────────────────────────────
|
||||
|
||||
Future<void> _loadBookmarks() async {
|
||||
final list = await _readerDao.getBookmarksByBookId(widget.bookId);
|
||||
if (mounted) setState(() => _bookmarks..clear()..addAll(list));
|
||||
}
|
||||
|
||||
bool get _currentPageHasBookmark {
|
||||
final cfi = '$currentSpineItemIndex:${(currentPageInChapter / (totalPagesInChapter > 0 ? totalPagesInChapter : 1)).toStringAsFixed(4)}';
|
||||
return _bookmarks.any((bm) => (bm['cfi'] as String? ?? '') == cfi);
|
||||
}
|
||||
|
||||
Future<void> _toggleBookmark() async {
|
||||
// 检查当前页是否已有书签
|
||||
final cfi = '$currentSpineItemIndex:${(currentPageInChapter / (totalPagesInChapter > 0 ? totalPagesInChapter : 1)).toStringAsFixed(4)}';
|
||||
final existing = _bookmarks.where((bm) => (bm['cfi'] as String? ?? '') == cfi).firstOrNull;
|
||||
|
||||
if (existing != null) {
|
||||
// 删除已有书签
|
||||
await _readerDao.deleteBookmark(existing['id'] as int);
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('已移除书签'), duration: Duration(seconds: 1)),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// 添加书签
|
||||
final chapterTitle = bookSession.spine.isNotEmpty &&
|
||||
currentSpineItemIndex < bookSession.spine.length
|
||||
? bookSession.spine[currentSpineItemIndex].href
|
||||
: '';
|
||||
// 尝试从 TOC 找更友好的标题
|
||||
String title = chapterTitle;
|
||||
for (final toc in bookSession.toc) {
|
||||
if (toc.spineIndex == currentSpineItemIndex) {
|
||||
title = toc.label;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
await _readerDao.insertBookmark({
|
||||
'book_id': widget.bookId,
|
||||
'content': title,
|
||||
'cfi': cfi,
|
||||
'chapter': currentSpineItemIndex.toString(),
|
||||
'created_at': DateTime.now().toIso8601String(),
|
||||
'updated_at': DateTime.now().toIso8601String(),
|
||||
});
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('已添加书签'), duration: Duration(seconds: 1)),
|
||||
);
|
||||
}
|
||||
}
|
||||
await _loadBookmarks();
|
||||
}
|
||||
|
||||
void _jumpToBookmark(Map<String, dynamic> bookmark) {
|
||||
final cfi = bookmark['cfi'] as String? ?? '';
|
||||
if (cfi.isEmpty) return;
|
||||
final parts = cfi.split(':');
|
||||
if (parts.isEmpty) return;
|
||||
final chapterIndex = int.tryParse(parts[0]) ?? 0;
|
||||
final scrollRatio = parts.length >= 2 ? (double.tryParse(parts[1]) ?? 0.0) : 0.0;
|
||||
|
||||
if (chapterIndex >= 0 && chapterIndex < bookSession.spine.length) {
|
||||
setState(() {
|
||||
currentSpineItemIndex = chapterIndex;
|
||||
});
|
||||
loadCarousel(restoreScrollRatio: scrollRatio);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _deleteBookmark(int id) async {
|
||||
await _readerDao.deleteBookmark(id);
|
||||
await _loadBookmarks();
|
||||
}
|
||||
|
||||
void toggleControls() {
|
||||
if (showControls) {
|
||||
hideBottomNavigationBar();
|
||||
@@ -399,6 +481,9 @@ class _ReaderScreenState extends State<ReaderScreen>
|
||||
onTocItemSelected: navigateToTocItem,
|
||||
onCoverTap: navigateToFirstTocItemFirstPage,
|
||||
themeData: themeData,
|
||||
bookmarks: _bookmarks,
|
||||
onBookmarkTap: _jumpToBookmark,
|
||||
onBookmarkDelete: _deleteBookmark,
|
||||
),
|
||||
onDrawerChanged: (isOpened) {
|
||||
tocDrawerOpen = isOpened;
|
||||
@@ -530,6 +615,28 @@ class _ReaderScreenState extends State<ReaderScreen>
|
||||
readerSettings.save();
|
||||
updateWebViewTheme();
|
||||
},
|
||||
themeIndex: readerSettings.themeIndex,
|
||||
customBgColor: readerSettings.customBgColor,
|
||||
customTextColor: readerSettings.customTextColor,
|
||||
onThemeIndexChanged: (index) {
|
||||
setState(() {
|
||||
readerSettings = readerSettings.copyWith(themeIndex: index);
|
||||
});
|
||||
readerSettings.save();
|
||||
updateWebViewTheme();
|
||||
},
|
||||
onCustomColorChanged: (bgColor, textColor) {
|
||||
setState(() {
|
||||
readerSettings = readerSettings.copyWith(
|
||||
customBgColor: bgColor,
|
||||
customTextColor: textColor,
|
||||
);
|
||||
});
|
||||
readerSettings.save();
|
||||
updateWebViewTheme();
|
||||
},
|
||||
currentPageHasBookmark: _currentPageHasBookmark,
|
||||
onBookmarkToggle: _toggleBookmark,
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../utils/epub/epub_theme.dart';
|
||||
import 'widgets/integer_stepper.dart';
|
||||
import 'widgets/reader_scale_slider.dart';
|
||||
|
||||
@@ -18,6 +19,11 @@ class ReaderStyleSheet extends StatefulWidget {
|
||||
final ValueChanged<double> onMarginLeftChanged;
|
||||
final ValueChanged<double> onMarginRightChanged;
|
||||
final ValueChanged<double> onFontSizeChanged;
|
||||
final int themeIndex;
|
||||
final int customBgColor;
|
||||
final int customTextColor;
|
||||
final ValueChanged<int> onThemeIndexChanged;
|
||||
final void Function(int bgColor, int textColor) onCustomColorChanged;
|
||||
|
||||
const ReaderStyleSheet({
|
||||
super.key,
|
||||
@@ -33,6 +39,11 @@ class ReaderStyleSheet extends StatefulWidget {
|
||||
required this.onMarginLeftChanged,
|
||||
required this.onMarginRightChanged,
|
||||
required this.onFontSizeChanged,
|
||||
required this.themeIndex,
|
||||
required this.customBgColor,
|
||||
required this.customTextColor,
|
||||
required this.onThemeIndexChanged,
|
||||
required this.onCustomColorChanged,
|
||||
});
|
||||
|
||||
@override
|
||||
@@ -46,6 +57,9 @@ class _ReaderStyleSheetState extends State<ReaderStyleSheet> {
|
||||
late int _leftMargin;
|
||||
late int _rightMargin;
|
||||
late double _fontSize;
|
||||
late int _themeIndex;
|
||||
late int _customBgColor;
|
||||
late int _customTextColor;
|
||||
|
||||
static const int _marginMin = 0;
|
||||
static const int _marginMax = 64;
|
||||
@@ -62,6 +76,9 @@ class _ReaderStyleSheetState extends State<ReaderStyleSheet> {
|
||||
_leftMargin = widget.marginLeft.toInt();
|
||||
_rightMargin = widget.marginRight.toInt();
|
||||
_fontSize = widget.fontSize;
|
||||
_themeIndex = widget.themeIndex;
|
||||
_customBgColor = widget.customBgColor;
|
||||
_customTextColor = widget.customTextColor;
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -257,6 +274,13 @@ class _ReaderStyleSheetState extends State<ReaderStyleSheet> {
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// -- 阅读主题 --
|
||||
const _SectionTitle(label: '阅读主题'),
|
||||
const SizedBox(height: 10),
|
||||
_buildThemePresets(colorScheme),
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -264,6 +288,223 @@ class _ReaderStyleSheetState extends State<ReaderStyleSheet> {
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildThemePresets(ColorScheme colorScheme) {
|
||||
final presets = ReaderThemePresets.presets;
|
||||
return SizedBox(
|
||||
height: 56,
|
||||
child: ListView.separated(
|
||||
scrollDirection: Axis.horizontal,
|
||||
itemCount: presets.length + 1, // +1 for custom
|
||||
separatorBuilder: (_, __) => const SizedBox(width: 12),
|
||||
itemBuilder: (ctx, i) {
|
||||
final isSelected = _themeIndex == i;
|
||||
final borderColor = isSelected
|
||||
? colorScheme.primary
|
||||
: colorScheme.outlineVariant;
|
||||
if (i < presets.length) {
|
||||
final preset = presets[i];
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
setState(() => _themeIndex = i);
|
||||
widget.onThemeIndexChanged(i);
|
||||
},
|
||||
child: AnimatedContainer(
|
||||
duration: const Duration(milliseconds: 200),
|
||||
width: 56,
|
||||
height: 56,
|
||||
decoration: BoxDecoration(
|
||||
color: i == 0
|
||||
? colorScheme.surfaceContainerHighest
|
||||
: preset.surface,
|
||||
borderRadius: BorderRadius.circular(28),
|
||||
border: Border.all(
|
||||
color: borderColor,
|
||||
width: isSelected ? 2.5 : 1,
|
||||
),
|
||||
),
|
||||
child: Center(
|
||||
child: isSelected
|
||||
? Icon(
|
||||
Icons.check,
|
||||
size: 20,
|
||||
color: i == 0
|
||||
? colorScheme.onSurface
|
||||
: preset.onSurface,
|
||||
)
|
||||
: i == 0
|
||||
? Icon(
|
||||
Icons.phone_android,
|
||||
size: 18,
|
||||
color: colorScheme.onSurfaceVariant,
|
||||
)
|
||||
: Container(
|
||||
width: 16,
|
||||
height: 16,
|
||||
decoration: BoxDecoration(
|
||||
color: preset.onSurface,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
// Custom color chip
|
||||
return GestureDetector(
|
||||
onTap: () => _showCustomColorPicker(ctx),
|
||||
child: AnimatedContainer(
|
||||
duration: const Duration(milliseconds: 200),
|
||||
width: 56,
|
||||
height: 56,
|
||||
decoration: BoxDecoration(
|
||||
color: Color(_customBgColor),
|
||||
borderRadius: BorderRadius.circular(28),
|
||||
border: Border.all(
|
||||
color: borderColor,
|
||||
width: isSelected ? 2.5 : 1,
|
||||
),
|
||||
),
|
||||
child: Center(
|
||||
child: isSelected
|
||||
? Icon(Icons.check, size: 20, color: Color(_customTextColor))
|
||||
: Icon(Icons.palette_outlined, size: 18, color: Color(_customTextColor)),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _showCustomColorPicker(BuildContext context) {
|
||||
final cs = Theme.of(context).colorScheme;
|
||||
Color bgColor = Color(_customBgColor);
|
||||
Color textColor = Color(_customTextColor);
|
||||
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (ctx) {
|
||||
return StatefulBuilder(
|
||||
builder: (ctx, setDialogState) {
|
||||
return AlertDialog(
|
||||
backgroundColor: cs.surface,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
|
||||
title: Text('自定义颜色', style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: cs.onSurface)),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// Preview
|
||||
Container(
|
||||
width: double.infinity,
|
||||
height: 64,
|
||||
decoration: BoxDecoration(
|
||||
color: bgColor,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: cs.outlineVariant, width: 0.5),
|
||||
),
|
||||
child: Center(
|
||||
child: Text('预览文字 Aa 字体',
|
||||
style: TextStyle(color: textColor, fontSize: 16)),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
// Background color
|
||||
_buildColorRow(
|
||||
label: '背景色',
|
||||
color: bgColor,
|
||||
onChanged: (c) => setDialogState(() => bgColor = c),
|
||||
cs: cs,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
// Text color
|
||||
_buildColorRow(
|
||||
label: '文字色',
|
||||
color: textColor,
|
||||
onChanged: (c) => setDialogState(() => textColor = c),
|
||||
cs: cs,
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(ctx),
|
||||
child: Text('取消', style: TextStyle(color: cs.onSurfaceVariant)),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_customBgColor = bgColor.value;
|
||||
_customTextColor = textColor.value;
|
||||
_themeIndex = 9;
|
||||
});
|
||||
widget.onCustomColorChanged(bgColor.value, textColor.value);
|
||||
widget.onThemeIndexChanged(9);
|
||||
Navigator.pop(ctx);
|
||||
},
|
||||
child: Text('确定', style: TextStyle(fontWeight: FontWeight.w600, color: cs.primary)),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildColorRow({
|
||||
required String label,
|
||||
required Color color,
|
||||
required ValueChanged<Color> onChanged,
|
||||
required ColorScheme cs,
|
||||
}) {
|
||||
return Row(
|
||||
children: [
|
||||
Text(label, style: TextStyle(fontSize: 13, color: cs.onSurfaceVariant)),
|
||||
const Spacer(),
|
||||
// Preset color chips
|
||||
..._presetColors.map((c) {
|
||||
final selected = c.$2 == color;
|
||||
return GestureDetector(
|
||||
onTap: () => onChanged(c.$2),
|
||||
child: Container(
|
||||
width: 28,
|
||||
height: 28,
|
||||
margin: const EdgeInsets.only(left: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: c.$2,
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
border: Border.all(
|
||||
color: selected ? cs.primary : cs.outlineVariant,
|
||||
width: selected ? 2 : 1,
|
||||
),
|
||||
),
|
||||
child: selected
|
||||
? Icon(Icons.check, size: 14,
|
||||
color: ThemeData.estimateBrightnessForColor(c.$2) == Brightness.dark
|
||||
? Colors.white : Colors.black)
|
||||
: null,
|
||||
),
|
||||
);
|
||||
}),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
static const _presetColors = [
|
||||
('白色', Color(0xFFFFFFFF)),
|
||||
('浅灰', Color(0xFFF5F5F5)),
|
||||
('护眼', Color(0xFFF4ECD8)),
|
||||
('抹茶', Color(0xFFF6FBF5)),
|
||||
('樱花', Color(0xFFFFF8F8)),
|
||||
('浅蓝', Color(0xFFF0F4FF)),
|
||||
('深灰', Color(0xFF333333)),
|
||||
('深褐', Color(0xFF2C2418)),
|
||||
('深蓝', Color(0xFF1A2A3A)),
|
||||
('深绿', Color(0xFF1A2E1A)),
|
||||
('黑色', Color(0xFF1A1A1A)),
|
||||
('深红', Color(0xFF3A1A1A)),
|
||||
];
|
||||
}
|
||||
|
||||
/// Section title (equivalent to lumina's SettingsSectionTitle)
|
||||
|
||||
@@ -29,6 +29,11 @@ class TocDrawer extends StatefulWidget {
|
||||
final VoidCallback? onCoverTap;
|
||||
final ThemeData themeData;
|
||||
|
||||
// 书签相关
|
||||
final List<Map<String, dynamic>> bookmarks;
|
||||
final void Function(Map<String, dynamic> bookmark)? onBookmarkTap;
|
||||
final void Function(int bookmarkId)? onBookmarkDelete;
|
||||
|
||||
const TocDrawer({
|
||||
super.key,
|
||||
required this.bookTitle,
|
||||
@@ -40,20 +45,25 @@ class TocDrawer extends StatefulWidget {
|
||||
required this.onTocItemSelected,
|
||||
this.onCoverTap,
|
||||
required this.themeData,
|
||||
this.bookmarks = const [],
|
||||
this.onBookmarkTap,
|
||||
this.onBookmarkDelete,
|
||||
});
|
||||
|
||||
@override
|
||||
State<TocDrawer> createState() => _TocDrawerState();
|
||||
}
|
||||
|
||||
class _TocDrawerState extends State<TocDrawer> {
|
||||
final ScrollController _scrollController = ScrollController();
|
||||
class _TocDrawerState extends State<TocDrawer> with SingleTickerProviderStateMixin {
|
||||
final ScrollController _tocScrollController = ScrollController();
|
||||
final Set<TocEntry> _expandedItems = {};
|
||||
List<_TocRowItem> _visibleItems = [];
|
||||
late TabController _tabController;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_tabController = TabController(length: 2, vsync: this);
|
||||
_initExpansionState();
|
||||
_regenerateVisibleItems();
|
||||
|
||||
@@ -80,7 +90,8 @@ class _TocDrawerState extends State<TocDrawer> {
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_scrollController.dispose();
|
||||
_tocScrollController.dispose();
|
||||
_tabController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -161,10 +172,10 @@ class _TocDrawerState extends State<TocDrawer> {
|
||||
(row) => widget.activeTocItems.contains(row.item),
|
||||
);
|
||||
|
||||
if (index != -1 && _scrollController.hasClients) {
|
||||
if (index != -1 && _tocScrollController.hasClients) {
|
||||
final offset = (index * 56.0) - (56.0 * 4);
|
||||
_scrollController.jumpTo(
|
||||
offset.clamp(0.0, _scrollController.position.maxScrollExtent),
|
||||
_tocScrollController.jumpTo(
|
||||
offset.clamp(0.0, _tocScrollController.position.maxScrollExtent),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -172,6 +183,7 @@ class _TocDrawerState extends State<TocDrawer> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isDark = widget.themeData.brightness == Brightness.dark;
|
||||
final colors = widget.themeData.colorScheme;
|
||||
|
||||
return Drawer(
|
||||
backgroundColor: widget.themeData.scaffoldBackgroundColor,
|
||||
@@ -179,19 +191,36 @@ class _TocDrawerState extends State<TocDrawer> {
|
||||
child: Column(
|
||||
children: [
|
||||
_buildHeader(context, isDark),
|
||||
// Tab bar
|
||||
Container(
|
||||
decoration: BoxDecoration(
|
||||
border: Border(
|
||||
bottom: BorderSide(color: colors.outlineVariant, width: 0.5),
|
||||
),
|
||||
),
|
||||
child: TabBar(
|
||||
controller: _tabController,
|
||||
labelColor: colors.onSurface,
|
||||
unselectedLabelColor: colors.onSurfaceVariant,
|
||||
labelStyle: const TextStyle(fontSize: 14, fontWeight: FontWeight.w600),
|
||||
unselectedLabelStyle: const TextStyle(fontSize: 14, fontWeight: FontWeight.w400),
|
||||
indicatorColor: colors.onSurface,
|
||||
indicatorSize: TabBarIndicatorSize.label,
|
||||
indicatorWeight: 2,
|
||||
dividerColor: Colors.transparent,
|
||||
tabs: const [
|
||||
Tab(text: '目录'),
|
||||
Tab(text: '书签'),
|
||||
],
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
controller: _scrollController,
|
||||
itemCount: _visibleItems.length + 1,
|
||||
itemExtent: 56.0,
|
||||
itemBuilder: (context, index) {
|
||||
if (index == _visibleItems.length) {
|
||||
return const SizedBox(height: 56);
|
||||
}
|
||||
|
||||
final row = _visibleItems[index];
|
||||
return _buildRowItem(context, row, isDark);
|
||||
},
|
||||
child: TabBarView(
|
||||
controller: _tabController,
|
||||
children: [
|
||||
_buildTocTab(context, isDark),
|
||||
_buildBookmarkTab(context, isDark),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
@@ -200,6 +229,24 @@ class _TocDrawerState extends State<TocDrawer> {
|
||||
);
|
||||
}
|
||||
|
||||
// ─── 目录 Tab ────────────────────────────────────────────────
|
||||
|
||||
Widget _buildTocTab(BuildContext context, bool isDark) {
|
||||
return ListView.builder(
|
||||
controller: _tocScrollController,
|
||||
itemCount: _visibleItems.length + 1,
|
||||
itemExtent: 56.0,
|
||||
itemBuilder: (context, index) {
|
||||
if (index == _visibleItems.length) {
|
||||
return const SizedBox(height: 56);
|
||||
}
|
||||
|
||||
final row = _visibleItems[index];
|
||||
return _buildRowItem(context, row, isDark);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildRowItem(BuildContext context, _TocRowItem row, bool isDark) {
|
||||
final item = row.item;
|
||||
// 用 activeTocItems 匹配,或者直接用 spineIndex 匹配当前章节
|
||||
@@ -266,6 +313,129 @@ class _TocDrawerState extends State<TocDrawer> {
|
||||
);
|
||||
}
|
||||
|
||||
// ─── 书签 Tab ────────────────────────────────────────────────
|
||||
|
||||
Widget _buildBookmarkTab(BuildContext context, bool isDark) {
|
||||
final colors = widget.themeData.colorScheme;
|
||||
|
||||
if (widget.bookmarks.isEmpty) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.bookmark_outline, size: 48, color: colors.onSurfaceVariant.withValues(alpha: 0.3)),
|
||||
const SizedBox(height: 12),
|
||||
Text('暂无书签', style: TextStyle(fontSize: 14, color: colors.onSurfaceVariant.withValues(alpha: 0.5))),
|
||||
const SizedBox(height: 4),
|
||||
Text('阅读时点击顶部书签图标添加', style: TextStyle(fontSize: 12, color: colors.onSurfaceVariant.withValues(alpha: 0.3))),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return ListView.builder(
|
||||
padding: const EdgeInsets.only(bottom: 56),
|
||||
itemCount: widget.bookmarks.length,
|
||||
itemBuilder: (context, index) {
|
||||
final bm = widget.bookmarks[index];
|
||||
return _buildBookmarkItem(context, bm);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBookmarkItem(BuildContext context, Map<String, dynamic> bookmark) {
|
||||
final colors = widget.themeData.colorScheme;
|
||||
final content = bookmark['content'] as String? ?? '';
|
||||
final cfi = bookmark['cfi'] as String? ?? '';
|
||||
final createdAt = bookmark['created_at'] as String? ?? '';
|
||||
final bookmarkId = bookmark['id'] as int;
|
||||
|
||||
// 解析页码信息
|
||||
String pageInfo = '';
|
||||
if (cfi.isNotEmpty) {
|
||||
final parts = cfi.split(':');
|
||||
if (parts.length >= 2) {
|
||||
final chapterIdx = int.tryParse(parts[0]) ?? 0;
|
||||
pageInfo = '第 ${chapterIdx + 1} 章';
|
||||
}
|
||||
}
|
||||
|
||||
// 解析时间
|
||||
String timeStr = '';
|
||||
if (createdAt.isNotEmpty) {
|
||||
try {
|
||||
final dt = DateTime.parse(createdAt).toLocal();
|
||||
timeStr = '${dt.month}/${dt.day} ${dt.hour}:${dt.minute.toString().padLeft(2, '0')}';
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
return Dismissible(
|
||||
key: ValueKey(bookmarkId),
|
||||
direction: DismissDirection.endToStart,
|
||||
background: Container(
|
||||
alignment: Alignment.centerRight,
|
||||
padding: const EdgeInsets.only(right: 24),
|
||||
color: colors.error,
|
||||
child: Icon(Icons.delete_outline, color: colors.onError, size: 20),
|
||||
),
|
||||
onDismissed: (_) {
|
||||
widget.onBookmarkDelete?.call(bookmarkId);
|
||||
},
|
||||
child: Material(
|
||||
color: Colors.transparent,
|
||||
child: InkWell(
|
||||
onTap: () {
|
||||
widget.onBookmarkTap?.call(bookmark);
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
decoration: BoxDecoration(
|
||||
border: Border(
|
||||
bottom: BorderSide(color: colors.outlineVariant, width: 0.5),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.bookmark, size: 18, color: colors.primary),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
content.isNotEmpty ? content : pageInfo,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: widget.themeData.textTheme.bodyMedium?.copyWith(
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
if (pageInfo.isNotEmpty && content.isNotEmpty) ...[
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
pageInfo,
|
||||
style: TextStyle(fontSize: 12, color: colors.onSurfaceVariant.withValues(alpha: 0.5)),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
if (timeStr.isNotEmpty)
|
||||
Text(
|
||||
timeStr,
|
||||
style: TextStyle(fontSize: 11, color: colors.onSurfaceVariant.withValues(alpha: 0.4)),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// ─── Header ──────────────────────────────────────────────────
|
||||
|
||||
Widget _buildHeader(BuildContext context, bool isDark) {
|
||||
const authorText = '';
|
||||
|
||||
@@ -273,12 +443,6 @@ class _TocDrawerState extends State<TocDrawer> {
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: widget.themeData.colorScheme.surface,
|
||||
border: Border(
|
||||
bottom: BorderSide(
|
||||
color: widget.themeData.colorScheme.outline,
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
),
|
||||
child: GestureDetector(
|
||||
onTap: () {
|
||||
|
||||
@@ -36,7 +36,7 @@ class BookGridItem extends StatelessWidget {
|
||||
|
||||
// ─── mode helpers ─────────────────────────────────────────────────────────
|
||||
|
||||
/// Relaxed: cover + title + author + progress bar.
|
||||
/// Relaxed: cover + title + author, 右上角进度百分比。
|
||||
Widget _buildRelaxed(BuildContext context) {
|
||||
final title = book['title'] as String? ?? '';
|
||||
final author = book['author'] as String? ?? '';
|
||||
@@ -45,8 +45,10 @@ class BookGridItem extends StatelessWidget {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Expanded(child: _buildCoverStack(context, fit: StackFit.expand)),
|
||||
const SizedBox(height: 12),
|
||||
Expanded(child: _buildCoverStack(context, fit: StackFit.expand, extras: [
|
||||
if (progress > 0) _buildProgressBadge(context),
|
||||
])),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
title,
|
||||
maxLines: 2,
|
||||
@@ -55,8 +57,8 @@ class BookGridItem extends StatelessWidget {
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
if (author.isNotEmpty)
|
||||
if (author.isNotEmpty) ...[
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
author,
|
||||
maxLines: 1,
|
||||
@@ -66,17 +68,7 @@ class BookGridItem extends StatelessWidget {
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
if (progress > 0)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(2),
|
||||
child: LinearProgressIndicator(
|
||||
value: progress,
|
||||
minHeight: 3,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user