代码优化,epub阅读标签

This commit is contained in:
DelLevin-Home
2026-06-28 02:25:07 +08:00
parent c810a1a381
commit 5f434ae3fa
35 changed files with 2110 additions and 525 deletions

View File

@@ -296,24 +296,31 @@ class _BookExcerptsPageState extends State<BookExcerptsPage> {
return AlertDialog(
backgroundColor: colors.surface,
elevation: 0,
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
title: const Text('确认删除'),
content: const Text('确定要删除这条摘抄吗?'),
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)),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text('取消', style: TextStyle(color: colors.onSurface.withValues(alpha: 0.6))),
),
TextButton(
ElevatedButton(
onPressed: () async {
await context.read<AppProvider>().removeBookExcerpt(excerpt.id);
Navigator.pop(context);
_loadExcerpts();
ToastUtil.show(context, '已删除');
},
child: Text('删除', style: TextStyle(color: colors.error)),
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('删除'),
),
],
actionsPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
);
},
);

View File

@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../models/data_models.dart';
import '../../providers/app_provider.dart';
import '../../utils/toast_util.dart';
import 'book_review_form_page.dart';
/// 书评详情页
@@ -55,12 +56,15 @@ class _BookReviewDetailPageState extends State<BookReviewDetailPage> {
appBar: AppBar(
title: const Text('书评详情'),
actions: [
// 编辑按钮
IconButton(
icon: const Icon(Icons.edit_outlined),
onPressed: () => _navigateToEdit(context),
),
const SizedBox(width: 8),
IconButton(
icon: Icon(Icons.delete_outline, size: 20, color: colors.error.withValues(alpha: 0.7)),
onPressed: _deleteReview,
),
const SizedBox(width: 4),
],
),
body: SingleChildScrollView(
@@ -168,6 +172,41 @@ class _BookReviewDetailPageState extends State<BookReviewDetailPage> {
);
}
Future<void> _deleteReview() async {
final colors = Theme.of(context).colorScheme;
final confirmed = await showDialog<bool>(
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: Text('确定要删除这条书评吗?删除后可在回收站恢复。',
style: TextStyle(fontSize: 14, color: colors.onSurface.withValues(alpha: 0.6), height: 1.5)),
actions: [
TextButton(
onPressed: () => Navigator.pop(ctx, false),
child: Text('取消', style: TextStyle(color: colors.onSurface.withValues(alpha: 0.6))),
),
ElevatedButton(
onPressed: () => Navigator.pop(ctx, true),
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('删除'),
),
],
actionsPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
),
);
if (confirmed == true) {
await context.read<AppProvider>().removeBookReview(_review.id);
if (mounted) { ToastUtil.show(context, '已删除'); Navigator.pop(context); }
}
}
String _formatDate(DateTime date) {
return '${date.year}.${date.month.toString().padLeft(2, '0')}.${date.day.toString().padLeft(2, '0')}';
}

View File

@@ -1,20 +1,17 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../providers/app_provider.dart';
import '../../widgets/fade_in_local_image.dart';
import '../../models/data_models.dart';
import '../../utils/toast_util.dart';
import 'package:uuid/uuid.dart';
/// 添加/编辑书评页面 - 极简设计
/// 添加/编辑书评页面
class BookReviewFormPage extends StatefulWidget {
final String bookId;
final BookReview? review;
const BookReviewFormPage({
super.key,
required this.bookId,
this.review,
});
const BookReviewFormPage({super.key, required this.bookId, this.review});
@override
State<BookReviewFormPage> createState() => _BookReviewFormPageState();
@@ -30,11 +27,10 @@ class _BookReviewFormPageState extends State<BookReviewFormPage> {
@override
void initState() {
super.initState();
final review = widget.review;
_contentController = TextEditingController(text: review?.content ?? '');
_reviewerController = TextEditingController(text: review?.reviewer ?? '');
_sourceController = TextEditingController(text: review?.source ?? '');
_reviewType = review?.reviewType ?? 1;
_contentController = TextEditingController(text: widget.review?.content ?? '');
_reviewerController = TextEditingController(text: widget.review?.reviewer ?? '');
_sourceController = TextEditingController(text: widget.review?.source ?? '');
_reviewType = widget.review?.reviewType ?? 1;
}
@override
@@ -45,107 +41,90 @@ class _BookReviewFormPageState extends State<BookReviewFormPage> {
super.dispose();
}
Book? _getBook() {
return context.read<AppProvider>().books.where((b) => b.id == widget.bookId).firstOrNull;
}
@override
Widget build(BuildContext context) {
final colors = Theme.of(context).colorScheme;
final isEdit = widget.review != null;
final book = _getBook();
return Scaffold(
backgroundColor: colors.surface,
appBar: AppBar(
title: Text(isEdit ? '编辑书评' : '写书评'),
actions: [
TextButton(
onPressed: _saveReview,
child: const Text(
'保存',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
),
),
),
const SizedBox(width: 8),
],
),
body: Form(
key: _formKey,
child: Column(
children: [
// 顶部信息栏
Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(color: colors.outline, width: 0.5),
),
),
child: Row(
children: [
// 类型选择
_buildTypeSelector(colors),
const SizedBox(width: 16),
// 评论人
Expanded(
child: TextField(
controller: _reviewerController,
style: TextStyle(fontSize: 14, color: colors.onSurface),
decoration: InputDecoration(
hintText: '评论人',
hintStyle: TextStyle(fontSize: 14, color: colors.onSurface.withValues(alpha: 0.25)),
border: InputBorder.none,
isDense: true,
contentPadding: EdgeInsets.zero,
),
),
),
const SizedBox(width: 16),
// 来源
SizedBox(
width: 100,
child: TextField(
controller: _sourceController,
style: TextStyle(fontSize: 14, color: colors.onSurface),
decoration: InputDecoration(
hintText: '来源',
hintStyle: TextStyle(fontSize: 14, color: colors.onSurface.withValues(alpha: 0.25)),
border: InputBorder.none,
isDense: true,
contentPadding: EdgeInsets.zero,
),
),
),
],
),
),
// 评论内容区域
Expanded(
child: TextFormField(
controller: _contentController,
maxLines: null,
expands: true,
textAlignVertical: TextAlignVertical.top,
style: TextStyle(
fontSize: 16,
color: colors.onSurface,
height: 1.7,
child: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// ── 书籍信息卡片 ──────────────────
if (book != null) _buildBookCard(book, colors),
const SizedBox(height: 20),
// ── 类型选择 ──────────────────────
Text('书评类型', style: TextStyle(fontSize: 13, fontWeight: FontWeight.w500, color: colors.onSurface.withValues(alpha: 0.5))),
const SizedBox(height: 8),
_buildTypeSelector(colors),
const SizedBox(height: 20),
// ── 元信息 ────────────────────────
_buildMetaField(
icon: Icons.person_outline,
hint: '评论人(选填)',
controller: _reviewerController,
colors: colors,
),
const SizedBox(height: 12),
_buildMetaField(
icon: Icons.link,
hint: '来源(选填)',
controller: _sourceController,
colors: colors,
),
const SizedBox(height: 20),
// ── 评论内容 ──────────────────────
Text('评论内容', style: TextStyle(fontSize: 13, fontWeight: FontWeight.w500, color: colors.onSurface.withValues(alpha: 0.5))),
const SizedBox(height: 8),
_buildContentField(colors),
],
),
decoration: InputDecoration(
hintText: '写下你的书评...',
hintStyle: TextStyle(
fontSize: 16,
color: colors.onSurface.withValues(alpha: 0.25),
),
),
// ── 底部保存按钮 ──────────────────────
Container(
padding: EdgeInsets.only(
left: 16, right: 16, top: 12,
bottom: MediaQuery.of(context).padding.bottom + 12,
),
decoration: BoxDecoration(
color: colors.surface,
border: Border(top: BorderSide(color: colors.outlineVariant, width: 0.5)),
),
child: SizedBox(
width: double.infinity,
height: 48,
child: ElevatedButton(
onPressed: _saveReview,
style: ElevatedButton.styleFrom(
backgroundColor: colors.primary,
foregroundColor: colors.onPrimary,
elevation: 0,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
),
border: InputBorder.none,
contentPadding: const EdgeInsets.all(16),
child: Text(isEdit ? '更新书评' : '保存书评',
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600)),
),
validator: (value) {
if (value == null || value.trim().isEmpty) {
return '请输入评论内容';
}
return null;
},
),
),
],
@@ -154,117 +133,211 @@ class _BookReviewFormPageState extends State<BookReviewFormPage> {
);
}
/// 构建类型选择器
Widget _buildTypeSelector(ColorScheme colors) {
return GestureDetector(
onTap: () => _showTypeSelector(),
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
decoration: BoxDecoration(
border: Border.all(color: colors.outline),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(
_reviewType == 1 ? '短评' : '长评',
style: TextStyle(
fontSize: 13,
color: colors.onSurface.withValues(alpha: 0.6),
),
),
const SizedBox(width: 4),
Icon(
Icons.arrow_drop_down,
size: 16,
color: colors.onSurface.withValues(alpha: 0.4),
),
],
),
),
);
}
// ═══════════════════════════════════════════════════════════════════
// 书籍信息卡片
// ═══════════════════════════════════════════════════════════════════
/// 显示类型选择
void _showTypeSelector() {
showModalBottomSheet(
context: context,
backgroundColor: Colors.transparent,
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
builder: (context) {
final colors = Theme.of(context).colorScheme;
return Container(
color: colors.surface,
child: SafeArea(
Widget _buildBookCard(Book book, ColorScheme colors) {
return Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: colors.surfaceContainerHighest,
borderRadius: BorderRadius.circular(12),
),
child: Row(
children: [
Container(
width: 56, height: 72,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(6),
color: colors.outlineVariant,
),
clipBehavior: Clip.antiAlias,
child: book.coverPath != null && book.coverPath!.isNotEmpty && File(book.coverPath!).existsSync()
? FadeInLocalImage(path: book.coverPath, fit: BoxFit.cover,
errorWidget: Icon(Icons.book_outlined, size: 24, color: colors.onSurface.withValues(alpha: 0.25)))
: Icon(Icons.book_outlined, size: 24, color: colors.onSurface.withValues(alpha: 0.25)),
),
const SizedBox(width: 14),
Expanded(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ListTile(
title: const Text('短评'),
trailing: _reviewType == 1
? Icon(Icons.check, color: colors.onSurface)
: null,
onTap: () {
setState(() => _reviewType = 1);
Navigator.pop(context);
},
),
Divider(height: 0.5, color: colors.outline),
ListTile(
title: const Text('长评'),
trailing: _reviewType == 2
? Icon(Icons.check, color: colors.onSurface)
: null,
onTap: () {
setState(() => _reviewType = 2);
Navigator.pop(context);
},
),
Text(book.title, style: TextStyle(fontSize: 15, fontWeight: FontWeight.w600, color: colors.onSurface), maxLines: 2, overflow: TextOverflow.ellipsis),
if (book.authors.isNotEmpty) ...[
const SizedBox(height: 4),
Text(book.authors.take(2).join(' / '),
style: TextStyle(fontSize: 12, color: colors.onSurface.withValues(alpha: 0.5)),
maxLines: 1, overflow: TextOverflow.ellipsis),
],
if (book.rating != null) ...[
const SizedBox(height: 4),
Row(children: [
Icon(Icons.star, size: 14, color: const Color(0xFFFFB800)),
const SizedBox(width: 2),
Text('${book.rating}', style: TextStyle(fontSize: 13, fontWeight: FontWeight.w600, color: colors.onSurface.withValues(alpha: 0.6))),
]),
],
],
),
),
);
},
],
),
);
}
// ═══════════════════════════════════════════════════════════════════
// 类型选择器(分段按钮)
// ═══════════════════════════════════════════════════════════════════
Widget _buildTypeSelector(ColorScheme colors) {
return SegmentedButton<int>(
segments: const [
ButtonSegment(value: 1, label: Text('短评'), icon: Icon(Icons.short_text)),
ButtonSegment(value: 2, label: Text('长评'), icon: Icon(Icons.menu_book)),
],
selected: {_reviewType},
onSelectionChanged: (v) => setState(() => _reviewType = v.first),
style: ButtonStyle(
backgroundColor: WidgetStateProperty.resolveWith((states) {
if (states.contains(WidgetState.selected)) return colors.primary;
return colors.surfaceContainerHighest;
}),
foregroundColor: WidgetStateProperty.resolveWith((states) {
if (states.contains(WidgetState.selected)) return colors.onPrimary;
return colors.onSurface.withValues(alpha: 0.6);
}),
iconColor: WidgetStateProperty.resolveWith((states) {
if (states.contains(WidgetState.selected)) return colors.onPrimary;
return colors.onSurface.withValues(alpha: 0.4);
}),
),
);
}
// ═══════════════════════════════════════════════════════════════════
// 元信息输入
// ═══════════════════════════════════════════════════════════════════
Widget _buildMetaField({
required IconData icon,
required String hint,
required TextEditingController controller,
required ColorScheme colors,
}) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 14),
decoration: BoxDecoration(
color: colors.surfaceContainerHighest,
borderRadius: BorderRadius.circular(10),
),
child: Row(
children: [
Icon(icon, size: 18, color: colors.onSurface.withValues(alpha: 0.35)),
const SizedBox(width: 10),
Expanded(
child: TextField(
controller: controller,
style: TextStyle(fontSize: 14, color: colors.onSurface),
decoration: InputDecoration(
hintText: hint,
hintStyle: TextStyle(fontSize: 14, color: colors.onSurface.withValues(alpha: 0.3)),
border: InputBorder.none,
enabledBorder: InputBorder.none,
focusedBorder: InputBorder.none,
contentPadding: const EdgeInsets.symmetric(vertical: 12),
),
),
),
],
),
);
}
// ═══════════════════════════════════════════════════════════════════
// 内容输入区 + 字数统计
// ═══════════════════════════════════════════════════════════════════
Widget _buildContentField(ColorScheme colors) {
return Container(
decoration: BoxDecoration(
color: colors.surfaceContainerHighest,
borderRadius: BorderRadius.circular(10),
),
child: Column(
children: [
TextFormField(
controller: _contentController,
maxLines: 10,
minLines: 6,
textAlignVertical: TextAlignVertical.top,
style: TextStyle(fontSize: 15, color: colors.onSurface, height: 1.7),
decoration: InputDecoration(
hintText: '写下你的书评...',
hintStyle: TextStyle(fontSize: 15, color: colors.onSurface.withValues(alpha: 0.25)),
border: InputBorder.none,
enabledBorder: InputBorder.none,
focusedBorder: InputBorder.none,
contentPadding: const EdgeInsets.all(14),
),
validator: (value) {
if (value == null || value.trim().isEmpty) return '请输入评论内容';
return null;
},
onChanged: (_) => setState(() {}),
),
Padding(
padding: const EdgeInsets.only(right: 14, bottom: 10),
child: Align(
alignment: Alignment.centerRight,
child: Text(
'${_contentController.text.length}',
style: TextStyle(fontSize: 11, color: colors.onSurface.withValues(alpha: 0.3)),
),
),
),
],
),
);
}
// ═══════════════════════════════════════════════════════════════════
// 保存
// ═══════════════════════════════════════════════════════════════════
Future<void> _saveReview() async {
if (!_formKey.currentState!.validate()) {
return;
}
if (!_formKey.currentState!.validate()) return;
try {
final now = DateTime.now();
final now = DateTime.now();
if (widget.review == null) {
final newReview = BookReview(
id: const Uuid().v4(),
bookId: widget.bookId,
content: _contentController.text.trim(),
reviewer: _reviewerController.text.trim(),
source: _sourceController.text.trim(),
reviewType: _reviewType,
isDeleted: false,
createdAt: now,
updatedAt: now,
);
await context.read<AppProvider>().addBookReview(newReview);
} else {
final updatedReview = widget.review!.copyWith(
content: _contentController.text.trim(),
reviewer: _reviewerController.text.trim(),
source: _sourceController.text.trim(),
reviewType: _reviewType,
updatedAt: now,
);
await context.read<AppProvider>().updateBookReview(updatedReview);
}
if (widget.review == null) {
final newReview = BookReview(
id: now.millisecondsSinceEpoch.toString(),
bookId: widget.bookId,
content: _contentController.text.trim(),
reviewer: _reviewerController.text.trim(),
source: _sourceController.text.trim(),
reviewType: _reviewType,
isDeleted: false,
createdAt: now,
updatedAt: now,
);
await context.read<AppProvider>().addBookReview(newReview);
} else {
final updatedReview = widget.review!.copyWith(
content: _contentController.text.trim(),
reviewer: _reviewerController.text.trim(),
source: _sourceController.text.trim(),
reviewType: _reviewType,
updatedAt: now,
);
await context.read<AppProvider>().updateBookReview(updatedReview);
}
if (!mounted) return;
ToastUtil.show(context, widget.review == null ? '添加成功' : '更新成功');
Navigator.pop(context);
if (!mounted) return;
ToastUtil.show(context, widget.review == null ? '添加成功' : '更新成功');
Navigator.pop(context);
} catch (e) {
if (!mounted) return;
ToastUtil.show(context, '保存失败: $e');

View File

@@ -323,24 +323,31 @@ class _BookReviewsPageState extends State<BookReviewsPage> {
return AlertDialog(
backgroundColor: colors.surface,
elevation: 0,
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
title: const Text('确认删除'),
content: const Text('确定要删除这条书评吗?'),
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)),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text('取消', style: TextStyle(color: colors.onSurface.withValues(alpha: 0.6))),
),
TextButton(
ElevatedButton(
onPressed: () async {
await context.read<AppProvider>().removeBookReview(review.id);
Navigator.pop(context);
_loadReviews();
ToastUtil.show(context, '已删除');
},
child: Text('删除', style: TextStyle(color: colors.error)),
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('删除'),
),
],
actionsPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
);
},
);