编辑界面优化

This commit is contained in:
DelLevin-Home
2026-03-22 15:54:28 +08:00
parent f9ea3d8284
commit 0c18df3943
9 changed files with 2284 additions and 595 deletions

View File

@@ -8,6 +8,7 @@ import '../../models/data_models.dart';
import '../../utils/toast_util.dart';
import 'book_reviews_page.dart';
import 'book_excerpts_page.dart';
import 'book_share_page.dart';
/// 书籍详情页 - 极简主义设计
class BookDetailPage extends StatefulWidget {
@@ -42,90 +43,128 @@ class _BookDetailPageState extends State<BookDetailPage> {
return Scaffold(
backgroundColor: Colors.white,
body: CustomScrollView(
slivers: [
// 顶部封面区域
_buildSliverAppBar(book),
body: Stack(
children: [
CustomScrollView(
slivers: [
// 顶部封面区域
_buildSliverAppBar(book),
// 内容区域
SliverToBoxAdapter(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 基本信息
_buildBasicInfo(book),
const Divider(height: 0.5, thickness: 0.5, color: Color(0xFFE5E5E5)),
// 作者信息
_buildAuthorsSection(book),
// 内容区域
SliverToBoxAdapter(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 基本信息
_buildBasicInfo(book),
const Divider(height: 0.5, thickness: 0.5, color: Color(0xFFE5E5E5)),
// 作者信息
_buildAuthorsSection(book),
// 类型
if (book.genres.isNotEmpty)
_buildGenresSection(book),
// 类型
if (book.genres.isNotEmpty)
_buildGenresSection(book),
// ISBN
if (book.isbn != null && book.isbn!.isNotEmpty)
_buildIsbnSection(book),
// ISBN
if (book.isbn != null && book.isbn!.isNotEmpty)
_buildIsbnSection(book),
// 出版社
if (book.publisher != null && book.publisher!.isNotEmpty)
_buildPublisherSection(book),
// 出版社
if (book.publisher != null && book.publisher!.isNotEmpty)
_buildPublisherSection(book),
// 出版时间
if (book.publishDate != null)
_buildPublishDateSection(book),
const Divider(height: 0.5, thickness: 0.5, color: Color(0xFFE5E5E5)),
// 简介
if (book.summary != null && book.summary!.isNotEmpty)
_buildSummarySection(book),
// 出版时间
if (book.publishDate != null)
_buildPublishDateSection(book),
const Divider(height: 0.5, thickness: 0.5, color: Color(0xFFE5E5E5)),
// 简介
if (book.summary != null && book.summary!.isNotEmpty)
_buildSummarySection(book),
const Divider(height: 0.5, thickness: 0.5, color: Color(0xFFE5E5E5)),
const Divider(height: 0.5, thickness: 0.5, color: Color(0xFFE5E5E5)),
// 书评和摘抄入口
_buildExtraSections(book),
// 书评和摘抄入口
_buildExtraSections(book),
const SizedBox(height: 48),
],
),
const SizedBox(height: 120),
],
),
),
],
),
// 右下角悬浮按钮组
Positioned(
right: 16,
bottom: 24,
child: _buildFloatingActionButtons(book),
),
],
),
// 底部无操作栏,编辑和删除在右上角
);
}
/// 构建右上角操作按钮
Widget _buildActionButton({
/// 构建右下角悬浮按钮
Widget _buildFloatingActionButtons(Book book) {
final hasCover = book.coverPath != null && book.coverPath!.isNotEmpty;
return Column(
mainAxisSize: MainAxisSize.min,
children: [
// 删除按钮
_buildFloatingButton(
icon: Icons.delete_outline,
onPressed: () => _showDeleteDialog(context),
tooltip: '删除',
backgroundColor: Colors.red,
),
const SizedBox(height: 12),
// 清空封面按钮(仅当有封面时显示)
if (hasCover) ...[
_buildFloatingButton(
icon: Icons.hide_image_outlined,
onPressed: () => _showClearCoverDialog(book),
tooltip: '清空封面',
),
const SizedBox(height: 12),
],
// 编辑按钮
_buildFloatingButton(
icon: Icons.edit_outlined,
onPressed: () => _navigateToEdit(context),
tooltip: '编辑',
),
const SizedBox(height: 12),
// 分享按钮
_buildFloatingButton(
icon: Icons.share_outlined,
onPressed: () => _showSharePoster(book),
tooltip: '分享海报',
backgroundColor: const Color(0xFF4CAF50),
),
],
);
}
/// 构建单个悬浮按钮
Widget _buildFloatingButton({
required IconData icon,
required VoidCallback onPressed,
required String tooltip,
Color color = const Color(0xFF666666),
Color backgroundColor = const Color(0xFF1A1A1A),
}) {
return Container(
margin: const EdgeInsets.symmetric(horizontal: 4, vertical: 8),
decoration: BoxDecoration(
color: Colors.white.withOpacity(0.9),
borderRadius: BorderRadius.circular(8),
),
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: onPressed,
borderRadius: BorderRadius.circular(8),
child: Container(
padding: const EdgeInsets.all(8),
child: Icon(
icon,
color: color,
size: 22,
),
),
),
),
return FloatingActionButton(
onPressed: onPressed,
tooltip: tooltip,
backgroundColor: backgroundColor,
foregroundColor: Colors.white,
mini: true,
elevation: 4,
child: Icon(icon, size: 20),
);
}
@@ -157,8 +196,6 @@ class _BookDetailPageState extends State<BookDetailPage> {
/// 构建顶部 AppBar
Widget _buildSliverAppBar(Book book) {
final hasCover = book.coverPath != null && book.coverPath!.isNotEmpty;
return SliverAppBar(
expandedHeight: 320,
pinned: true,
@@ -167,29 +204,7 @@ class _BookDetailPageState extends State<BookDetailPage> {
flexibleSpace: FlexibleSpaceBar(
background: _buildCoverSection(book),
),
actions: [
// 清空封面按钮(仅当有封面时显示)
if (hasCover)
_buildActionButton(
icon: Icons.hide_image_outlined,
onPressed: () => _showClearCoverDialog(book),
tooltip: '清空封面',
),
// 编辑按钮
_buildActionButton(
icon: Icons.edit_outlined,
onPressed: () => _navigateToEdit(context),
tooltip: '编辑',
),
// 删除按钮
_buildActionButton(
icon: Icons.delete_outline,
color: Colors.red,
onPressed: () => _showDeleteDialog(context),
tooltip: '删除',
),
const SizedBox(width: 8),
],
// 右上角按钮已移到右下角悬浮按钮
);
}
@@ -875,4 +890,14 @@ class _BookDetailPageState extends State<BookDetailPage> {
ToastUtil.show(context, '下载失败: $e');
}
}
/// 显示分享海报页面
void _showSharePoster(Book book) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BookSharePage(book: book),
),
);
}
}

View File

@@ -148,160 +148,166 @@ class _BookFormPageState extends State<BookFormPage> {
body: Form(
key: _formKey,
child: ListView(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 16),
children: [
// 封面选择 - 居中显示
Center(child: _buildCoverPicker()),
const SizedBox(height: 32),
const SizedBox(height: 24),
// 状态选择(靠左显示
// 状态选择(简洁风格
_buildStatusSelector(),
const SizedBox(height: 20),
const SizedBox(height: 16),
// 评分 - 星星选择(靠左显示
// 评分 - 星星选择(简洁风格
_buildStarRating(),
const SizedBox(height: 32),
// 书名
_buildHorizontalFormItem(
label: '书名',
required: true,
child: TextFormField(
controller: _titleController,
style: const TextStyle(fontSize: 15, color: Color(0xFF1A1A1A)),
decoration: const InputDecoration(
hintText: '请输入书名',
hintStyle: TextStyle(fontSize: 14, color: Color(0xFFCCCCCC)),
border: InputBorder.none,
contentPadding: EdgeInsets.zero,
// 信息卡片网格
Wrap(
spacing: 12,
runSpacing: 12,
children: [
// 第一行:书名 + 别名
SizedBox(
width: (MediaQuery.of(context).size.width - 52) / 2,
height: 90,
child: _buildInfoCard(
label: '书名',
value: _titleController.text,
required: true,
icon: Icons.book_outlined,
onTap: () => _showTextInputDialog(
title: '书名',
initialValue: _titleController.text,
hint: '请输入书名',
onConfirm: (value) => setState(() => _titleController.text = value),
),
),
),
validator: (value) {
if (value == null || value.trim().isEmpty) {
return '请输入书名';
}
return null;
},
),
),
const SizedBox(height: 16),
// 别名
_buildHorizontalMultiValueItem(
label: '别名',
values: _alternateTitles,
hint: '输入别名',
controllerKey: 'alternateTitles',
onAdd: (v) => setState(() => _alternateTitles.add(v)),
onRemove: (i) => setState(() => _alternateTitles.removeAt(i)),
),
const SizedBox(height: 16),
// 作者
_buildHorizontalMultiValueItem(
label: '作者',
values: _authors,
hint: '输入作者姓名',
controllerKey: 'authors',
onAdd: (v) => setState(() => _authors.add(v)),
onRemove: (i) => setState(() => _authors.removeAt(i)),
),
const SizedBox(height: 16),
// 出版社
_buildHorizontalFormItem(
label: '出版社',
child: TextFormField(
controller: _publisherController,
style: const TextStyle(fontSize: 15, color: Color(0xFF1A1A1A)),
decoration: const InputDecoration(
hintText: '请输入出版社',
hintStyle: TextStyle(fontSize: 14, color: Color(0xFFCCCCCC)),
border: InputBorder.none,
contentPadding: EdgeInsets.zero,
SizedBox(
width: (MediaQuery.of(context).size.width - 52) / 2,
height: 90,
child: _buildInfoCard(
label: '别名',
value: _alternateTitles.isEmpty
? ''
: '${_alternateTitles.length}个:${_alternateTitles.join('')}',
icon: Icons.alternate_email_outlined,
onTap: () => _showMultiValueDialog(
title: '添加别名',
initialValues: _alternateTitles,
hint: '输入别名',
onConfirm: (values) => setState(() => _alternateTitles = values),
),
),
),
),
),
const SizedBox(height: 16),
// 类型
_buildHorizontalMultiValueItem(
label: '类型',
values: _genres,
hint: '如:小说、历史、传记',
controllerKey: 'genres',
onAdd: (v) => setState(() => _genres.add(v)),
onRemove: (i) => setState(() => _genres.removeAt(i)),
),
const SizedBox(height: 16),
// ISBN
_buildHorizontalFormItem(
label: 'ISBN',
child: TextFormField(
controller: _isbnController,
style: const TextStyle(fontSize: 15, color: Color(0xFF1A1A1A)),
decoration: const InputDecoration(
hintText: '请输入ISBN编号',
hintStyle: TextStyle(fontSize: 14, color: Color(0xFFCCCCCC)),
border: InputBorder.none,
contentPadding: EdgeInsets.zero,
// 第二行:作者 + 出版社
SizedBox(
width: (MediaQuery.of(context).size.width - 52) / 2,
height: 90,
child: _buildInfoCard(
label: '作者',
value: _authors.isEmpty
? ''
: '${_authors.length}人:${_authors.join('')}',
icon: Icons.person_outline,
onTap: () => _showMultiValueDialog(
title: '添加作者',
initialValues: _authors,
hint: '输入作者姓名',
onConfirm: (values) => setState(() => _authors = values),
),
),
),
),
),
const SizedBox(height: 16),
// 出版社
_buildHorizontalFormItem(
label: '出版社',
child: TextFormField(
controller: _publisherController,
style: const TextStyle(fontSize: 15, color: Color(0xFF1A1A1A)),
decoration: const InputDecoration(
hintText: '请输入出版社名称',
hintStyle: TextStyle(fontSize: 14, color: Color(0xFFCCCCCC)),
border: InputBorder.none,
contentPadding: EdgeInsets.zero,
SizedBox(
width: (MediaQuery.of(context).size.width - 52) / 2,
height: 90,
child: _buildInfoCard(
label: '出版社',
value: _publisherController.text,
icon: Icons.business_outlined,
onTap: () => _showTextInputDialog(
title: '出版社',
initialValue: _publisherController.text,
hint: '请输入出版社',
onConfirm: (value) => setState(() => _publisherController.text = value),
),
),
),
),
),
const SizedBox(height: 16),
// 出版时间
_buildVerticalDateItem(
label: '出版时间',
date: _publishDate,
onTap: _selectPublishDate,
onClear: () => setState(() => _publishDate = null),
),
const SizedBox(height: 16),
// 书籍简介
_buildFormItem(
label: '书籍简介',
child: TextFormField(
controller: _summaryController,
maxLines: 4,
style: const TextStyle(fontSize: 15, color: Color(0xFF1A1A1A), height: 1.5),
decoration: const InputDecoration(
hintText: '写下书籍简介...',
hintStyle: TextStyle(fontSize: 14, color: Color(0xFFCCCCCC)),
border: InputBorder.none,
contentPadding: EdgeInsets.zero,
// 第三行:类型 + ISBN
SizedBox(
width: (MediaQuery.of(context).size.width - 52) / 2,
height: 90,
child: _buildInfoCard(
label: '类型',
value: _genres.isEmpty
? ''
: '${_genres.length}个:${_genres.join('')}',
icon: Icons.category_outlined,
onTap: () => _showMultiValueDialog(
title: '添加类型',
initialValues: _genres,
hint: '如:小说、历史、传记',
onConfirm: (values) => setState(() => _genres = values),
),
),
),
),
SizedBox(
width: (MediaQuery.of(context).size.width - 52) / 2,
height: 90,
child: _buildInfoCard(
label: 'ISBN',
value: _isbnController.text,
icon: Icons.qr_code_outlined,
onTap: () => _showTextInputDialog(
title: 'ISBN',
initialValue: _isbnController.text,
hint: '请输入ISBN编号',
onConfirm: (value) => setState(() => _isbnController.text = value),
),
),
),
// 第四行:出版时间(独占一行)
SizedBox(
width: double.infinity,
height: 90,
child: _buildInfoCard(
label: '出版时间',
value: _publishDate != null
? '${_publishDate!.year}.${_publishDate!.month.toString().padLeft(2, '0')}.${_publishDate!.day.toString().padLeft(2, '0')}'
: '',
icon: Icons.date_range_outlined,
onTap: () => _selectPublishDate(),
),
),
// 第五行:书籍简介(独占一行)
SizedBox(
width: double.infinity,
child: _buildInfoCard(
label: '书籍简介',
value: _summaryController.text,
icon: Icons.description_outlined,
height: 120,
scrollable: true,
onTap: () => _showTextInputDialog(
title: '书籍简介',
initialValue: _summaryController.text,
hint: '写下书籍简介...',
maxLines: 8,
onConfirm: (value) => setState(() => _summaryController.text = value),
),
),
),
],
),
const SizedBox(height: 48),
],
),
@@ -560,20 +566,111 @@ class _BookFormPageState extends State<BookFormPage> {
}
/// 构建状态选择器(靠左显示,带标签
/// 构建信息卡片(用于网格布局
Widget _buildInfoCard({
required String label,
required String value,
required VoidCallback onTap,
bool required = false,
IconData? icon,
double? height,
bool scrollable = false,
}) {
final hasValue = value.isNotEmpty;
// 构建值显示部分
Widget buildContent() {
if (scrollable && height != null) {
// 可滚动模式(仅书籍简介使用)
return Flexible(
child: SingleChildScrollView(
physics: const BouncingScrollPhysics(),
child: Text(
hasValue ? value : '点击填写',
style: TextStyle(
fontSize: 15,
color: hasValue ? const Color(0xFF1A1A1A) : const Color(0xFFCCCCCC),
fontWeight: hasValue ? FontWeight.w500 : FontWeight.normal,
),
),
),
);
} else {
// 普通模式:只显示一行
return Text(
hasValue ? value : '点击填写',
style: TextStyle(
fontSize: 15,
color: hasValue ? const Color(0xFF1A1A1A) : const Color(0xFFCCCCCC),
fontWeight: hasValue ? FontWeight.w500 : FontWeight.normal,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
);
}
}
return GestureDetector(
onTap: onTap,
child: Container(
height: height,
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
border: Border.all(color: const Color(0xFFE8E8E8)),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.03),
blurRadius: 8,
offset: const Offset(0, 2),
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: height != null ? MainAxisSize.max : MainAxisSize.min,
children: [
// 标签行
Row(
children: [
if (icon != null) ...[
Icon(icon, size: 14, color: const Color(0xFF999999)),
const SizedBox(width: 6),
],
Text(
required ? '$label *' : label,
style: TextStyle(
fontSize: 12,
color: required ? const Color(0xFF1A1A1A) : const Color(0xFF999999),
fontWeight: required ? FontWeight.w500 : FontWeight.normal,
),
),
],
),
const SizedBox(height: 8),
// 值显示
buildContent(),
],
),
),
);
}
/// 构建状态选择器(简洁风格)
Widget _buildStatusSelector() {
return Row(
children: [
const Text(
'状态',
style: TextStyle(fontSize: 14, color: Color(0xFF666666)),
style: TextStyle(fontSize: 13, color: Color(0xFF999999)),
),
const SizedBox(width: 16),
Container(
padding: const EdgeInsets.all(4),
padding: const EdgeInsets.all(3),
decoration: BoxDecoration(
color: const Color(0xFFF5F5F5),
borderRadius: BorderRadius.circular(8),
color: const Color(0xFFF0F0F0),
borderRadius: BorderRadius.circular(6),
),
child: Row(
mainAxisSize: MainAxisSize.min,
@@ -588,13 +685,13 @@ class _BookFormPageState extends State<BookFormPage> {
);
}
/// 构建星星评分(5星制每星2分支持手动输入)
/// 构建星星评分(支持手动输入)
Widget _buildStarRating() {
return Row(
children: [
const Text(
'评分',
style: TextStyle(fontSize: 14, color: Color(0xFF666666)),
style: TextStyle(fontSize: 13, color: Color(0xFF999999)),
),
const SizedBox(width: 16),
// 星星选择
@@ -648,52 +745,42 @@ class _BookFormPageState extends State<BookFormPage> {
);
}
/// 构建评分输入框
/// 构建评分输入框支持0-10保留1位小数
Widget _buildRatingInputField() {
return Container(
width: 56,
height: 36,
height: 32,
decoration: BoxDecoration(
color: const Color(0xFFF5F5F5),
borderRadius: BorderRadius.circular(8),
color: const Color(0xFFF0F0F0),
borderRadius: BorderRadius.circular(6),
),
child: TextFormField(
controller: _ratingController,
keyboardType: const TextInputType.numberWithOptions(decimal: true),
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 15,
fontSize: 14,
fontWeight: FontWeight.w500,
color: Color(0xFF1A1A1A),
),
decoration: const InputDecoration(
hintText: '-',
hintStyle: TextStyle(fontSize: 15, color: Color(0xFFCCCCCC)),
hintStyle: TextStyle(fontSize: 14, color: Color(0xFFCCCCCC)),
border: InputBorder.none,
contentPadding: EdgeInsets.symmetric(horizontal: 8, vertical: 8),
contentPadding: EdgeInsets.symmetric(horizontal: 4, vertical: 6),
),
validator: (value) {
if (value != null && value.isNotEmpty) {
final rating = double.tryParse(value);
if (rating == null || rating < 0 || rating > 10) {
return '0-10';
}
}
return null;
},
onChanged: (value) {
// 限制输入范围
if (value.isNotEmpty) {
final rating = double.tryParse(value);
if (rating != null) {
if (rating > 10) {
_ratingController.text = '10';
_ratingController.text = '10.0';
} else if (rating < 0) {
_ratingController.text = '0';
_ratingController.text = '0.0';
}
}
}
setState(() {}); // 更新星星显示
setState(() {});
},
),
);
@@ -806,16 +893,18 @@ class _BookFormPageState extends State<BookFormPage> {
final hasCover = _coverPath != null && _coverPath!.isNotEmpty;
return Column(
mainAxisSize: MainAxisSize.min,
children: [
GestureDetector(
onTap: _pickCover,
child: Container(
width: 140,
height: 200,
width: 120,
height: 170,
decoration: BoxDecoration(
color: const Color(0xFFF5F5F5),
border: Border.all(color: const Color(0xFFE5E5E5), width: 0.5),
borderRadius: BorderRadius.circular(8),
),
clipBehavior: Clip.antiAlias,
child: hasCover
? Image.file(
File(_coverPath!),
@@ -828,28 +917,29 @@ class _BookFormPageState extends State<BookFormPage> {
// 清空封面按钮(仅当有封面时显示)
if (hasCover)
Padding(
padding: const EdgeInsets.only(top: 12),
padding: const EdgeInsets.only(top: 10),
child: GestureDetector(
onTap: () => setState(() => _coverPath = null),
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
decoration: BoxDecoration(
border: Border.all(color: const Color(0xFFE5E5E5)),
color: const Color(0xFFF5F5F5),
borderRadius: BorderRadius.circular(16),
),
child: const Row(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Icons.hide_image_outlined,
size: 16,
color: Color(0xFF666666),
Icons.delete_outline,
size: 14,
color: Colors.grey[600],
),
SizedBox(width: 4),
const SizedBox(width: 4),
Text(
'清空封面',
'移除封面',
style: TextStyle(
fontSize: 13,
color: Color(0xFF666666),
fontSize: 12,
color: Colors.grey[600],
),
),
],
@@ -866,16 +956,16 @@ class _BookFormPageState extends State<BookFormPage> {
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.add_photo_alternate_outlined,
size: 40,
color: Color(0xFF999999),
Icons.image_outlined,
size: 32,
color: Color(0xFFCCCCCC),
),
SizedBox(height: 12),
SizedBox(height: 8),
Text(
'点击添加封面',
'封面',
style: TextStyle(
fontSize: 14,
color: Color(0xFF999999),
fontSize: 12,
color: Color(0xFFAAAAAA),
),
),
],
@@ -1256,4 +1346,293 @@ class _BookFormPageState extends State<BookFormPage> {
return null;
}
/// 显示文本输入对话框
Future<void> _showTextInputDialog({
required String title,
required String initialValue,
required Function(String) onConfirm,
int maxLines = 1,
String hint = '',
}) async {
final result = await showDialog<String>(
context: context,
builder: (context) => _TextInputDialog(
title: title,
initialValue: initialValue,
hint: hint,
maxLines: maxLines,
),
);
if (result != null) {
onConfirm(result);
}
}
/// 显示多值输入对话框
Future<void> _showMultiValueDialog({
required String title,
required List<String> initialValues,
required String hint,
required Function(List<String>) onConfirm,
}) async {
final result = await showDialog<List<String>>(
context: context,
builder: (context) => _MultiValueDialog(
title: title,
initialValues: initialValues,
hint: hint,
),
);
if (result != null) {
onConfirm(result);
}
}
}
/// 文本输入对话框组件
class _TextInputDialog extends StatefulWidget {
final String title;
final String initialValue;
final String hint;
final int maxLines;
const _TextInputDialog({
required this.title,
required this.initialValue,
required this.hint,
required this.maxLines,
});
@override
State<_TextInputDialog> createState() => _TextInputDialogState();
}
class _TextInputDialogState extends State<_TextInputDialog> {
late final TextEditingController controller;
@override
void initState() {
super.initState();
controller = TextEditingController(text: widget.initialValue);
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return AlertDialog(
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
title: Text(
widget.title,
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
),
content: TextField(
controller: controller,
maxLines: widget.maxLines,
autofocus: true,
style: const TextStyle(fontSize: 15, color: Color(0xFF1A1A1A)),
decoration: InputDecoration(
hintText: widget.hint,
hintStyle: const TextStyle(fontSize: 14, color: Color(0xFFAAAAAA)),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: const BorderSide(color: Color(0xFFE0E0E0)),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: const BorderSide(color: Color(0xFFE0E0E0)),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: const BorderSide(color: Color(0xFF1A1A1A)),
),
contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 12),
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('取消', style: TextStyle(color: Color(0xFF999999))),
),
ElevatedButton(
onPressed: () => Navigator.pop(context, controller.text.trim()),
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF1A1A1A),
foregroundColor: Colors.white,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
),
child: const Text('确定'),
),
],
);
}
}
/// 多值输入对话框组件
class _MultiValueDialog extends StatefulWidget {
final String title;
final List<String> initialValues;
final String hint;
const _MultiValueDialog({
required this.title,
required this.initialValues,
required this.hint,
});
@override
State<_MultiValueDialog> createState() => _MultiValueDialogState();
}
class _MultiValueDialogState extends State<_MultiValueDialog> {
late List<String> values;
final controller = TextEditingController();
@override
void initState() {
super.initState();
values = List<String>.from(widget.initialValues);
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
void _addValue(String value) {
final trimmed = value.trim();
if (trimmed.isNotEmpty && !values.contains(trimmed)) {
setState(() {
values.add(trimmed);
});
controller.clear();
}
}
void _removeValue(int index) {
setState(() {
values.removeAt(index);
});
}
@override
Widget build(BuildContext context) {
return AlertDialog(
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
title: Text(
widget.title,
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
),
content: SizedBox(
width: double.maxFinite,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 已添加的值列表
if (values.isNotEmpty)
Wrap(
spacing: 8,
runSpacing: 8,
children: values.asMap().entries.map((entry) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
decoration: BoxDecoration(
color: const Color(0xFFF5F5F5),
borderRadius: BorderRadius.circular(6),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(
entry.value,
style: const TextStyle(fontSize: 14, color: Color(0xFF1A1A1A)),
),
const SizedBox(width: 4),
GestureDetector(
onTap: () => _removeValue(entry.key),
child: const Icon(Icons.close, size: 14, color: Color(0xFF999999)),
),
],
),
);
}).toList(),
),
if (values.isNotEmpty) const SizedBox(height: 16),
// 输入框
Row(
children: [
Expanded(
child: TextField(
controller: controller,
autofocus: true,
style: const TextStyle(fontSize: 15, color: Color(0xFF1A1A1A)),
decoration: InputDecoration(
hintText: widget.hint,
hintStyle: const TextStyle(fontSize: 14, color: Color(0xFFAAAAAA)),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: const BorderSide(color: Color(0xFFE0E0E0)),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: const BorderSide(color: Color(0xFFE0E0E0)),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: const BorderSide(color: Color(0xFF1A1A1A)),
),
contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 12),
),
onSubmitted: _addValue,
),
),
const SizedBox(width: 8),
GestureDetector(
onTap: () => _addValue(controller.text),
child: Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: const Color(0xFF1A1A1A),
borderRadius: BorderRadius.circular(8),
),
child: const Icon(Icons.add, size: 20, color: Colors.white),
),
),
],
),
],
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('取消', style: TextStyle(color: Color(0xFF999999))),
),
ElevatedButton(
onPressed: () {
// 如果输入框还有内容,先添加
_addValue(controller.text);
Navigator.pop(context, values);
},
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF1A1A1A),
foregroundColor: Colors.white,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
),
child: const Text('确定'),
),
],
);
}
}

View File

@@ -0,0 +1,319 @@
import 'dart:io';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:path_provider/path_provider.dart';
import 'package:share_plus/share_plus.dart';
import '../../models/data_models.dart';
import '../../utils/toast_util.dart';
/// 书籍分享海报页面
class BookSharePage extends StatefulWidget {
final Book book;
const BookSharePage({super.key, required this.book});
@override
State<BookSharePage> createState() => _BookSharePageState();
}
class _BookSharePageState extends State<BookSharePage> {
final GlobalKey _posterKey = GlobalKey();
bool _isGenerating = false;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFF5F5F5),
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 0,
leading: IconButton(
icon: const Icon(Icons.close, color: Color(0xFF1A1A1A)),
onPressed: () => Navigator.pop(context),
),
title: const Text(
'分享海报',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w600,
color: Color(0xFF1A1A1A),
),
),
centerTitle: true,
actions: [
TextButton(
onPressed: _isGenerating ? null : _generateAndShare,
child: _isGenerating
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Text(
'分享',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
color: Color(0xFF1A1A1A),
),
),
),
const SizedBox(width: 8),
],
),
body: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: RepaintBoundary(
key: _posterKey,
child: _buildPosterWidget(),
),
),
),
);
}
/// 构建海报 Widget
Widget _buildPosterWidget() {
final book = widget.book;
final hasCover = book.coverPath != null && book.coverPath!.isNotEmpty;
return Container(
width: 320,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.1),
blurRadius: 20,
offset: const Offset(0, 10),
),
],
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 封面图片
if (hasCover)
ClipRRect(
borderRadius: const BorderRadius.vertical(top: Radius.circular(16)),
child: Image.file(
File(book.coverPath!),
width: 320,
height: 200,
fit: BoxFit.cover,
),
),
// 内容区域
Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 书名
Text(
book.title,
style: const TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold,
color: Color(0xFF1A1A1A),
),
),
// 别名
if (book.alternateTitles.isNotEmpty) ...[
const SizedBox(height: 8),
Text(
book.alternateTitles.join(' / '),
style: const TextStyle(
fontSize: 14,
color: Color(0xFF666666),
),
),
],
const SizedBox(height: 16),
// 评分
if (book.rating != null && book.rating! > 0) ...[
Row(
children: [
const Icon(
Icons.star,
size: 18,
color: Color(0xFFFFB800),
),
const SizedBox(width: 4),
Text(
book.rating!.toStringAsFixed(1),
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
color: Color(0xFFFFB800),
),
),
const SizedBox(width: 4),
const Text(
'/ 10',
style: TextStyle(
fontSize: 12,
color: Color(0xFF999999),
),
),
],
),
const SizedBox(height: 12),
],
// 作者
if (book.authors.isNotEmpty)
_buildInfoRow('作者', book.authors.join(' / ')),
// 出版社
if (book.publisher != null && book.publisher!.isNotEmpty)
_buildInfoRow('出版社', book.publisher!),
// 出版时间
if (book.publishDate != null)
_buildInfoRow(
'出版',
'${book.publishDate!.year}.${book.publishDate!.month.toString().padLeft(2, '0')}.${book.publishDate!.day.toString().padLeft(2, '0')}',
),
// 类型
if (book.genres.isNotEmpty)
_buildInfoRow('类型', book.genres.join(' / ')),
// ISBN
if (book.isbn != null && book.isbn!.isNotEmpty)
_buildInfoRow('ISBN', book.isbn!),
const SizedBox(height: 16),
// 简介
if (book.summary != null && book.summary!.isNotEmpty) ...[
const Text(
'简介',
style: TextStyle(
fontSize: 12,
color: Color(0xFF999999),
),
),
const SizedBox(height: 8),
Text(
book.summary!,
style: const TextStyle(
fontSize: 13,
color: Color(0xFF666666),
height: 1.6,
),
maxLines: 5,
overflow: TextOverflow.ellipsis,
),
],
const SizedBox(height: 20),
// 底部标识
const Divider(height: 1, color: Color(0xFFE8E8E8)),
const SizedBox(height: 12),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.book_outlined,
size: 14,
color: const Color(0xFF1A1A1A).withOpacity(0.5),
),
const SizedBox(width: 6),
Text(
'来自 MookNote',
style: TextStyle(
fontSize: 12,
color: const Color(0xFF1A1A1A).withOpacity(0.5),
),
),
],
),
],
),
),
],
),
);
}
/// 构建信息行
Widget _buildInfoRow(String label, String value) {
return Padding(
padding: const EdgeInsets.only(bottom: 8),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'$label',
style: const TextStyle(
fontSize: 13,
color: Color(0xFF999999),
),
),
Expanded(
child: Text(
value,
style: const TextStyle(
fontSize: 13,
color: Color(0xFF333333),
),
),
),
],
),
);
}
/// 生成并分享海报
Future<void> _generateAndShare() async {
setState(() => _isGenerating = true);
try {
// 捕获海报图片
final boundary = _posterKey.currentContext?.findRenderObject()
as RenderRepaintBoundary?;
if (boundary == null) {
throw Exception('无法获取海报边界');
}
final image = await boundary.toImage(pixelRatio: 3.0);
final byteData = await image.toByteData(format: ui.ImageByteFormat.png);
if (byteData == null) {
throw Exception('无法生成图片数据');
}
final bytes = byteData.buffer.asUint8List();
// 保存到临时文件
final tempDir = await getTemporaryDirectory();
final fileName = 'book_poster_${DateTime.now().millisecondsSinceEpoch}.png';
final file = File('${tempDir.path}/$fileName');
await file.writeAsBytes(bytes);
// 分享
await Share.shareXFiles(
[XFile(file.path)],
text: '分享书籍:${widget.book.title}',
);
} catch (e) {
if (mounted) {
ToastUtil.show(context, '生成海报失败:$e');
}
} finally {
if (mounted) {
setState(() => _isGenerating = false);
}
}
}
}