generated from dellevin/template
v0.2.7
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
import 'dart:io';
|
||||
import 'dart:ui';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import '../../data/epub/reader_dao.dart';
|
||||
import '../../services/epub/epub_service.dart';
|
||||
import '../../utils/user_prefs.dart';
|
||||
import '../../utils/responsive.dart';
|
||||
import '../../utils/toast_util.dart';
|
||||
import 'epub_detail_page.dart';
|
||||
import 'widgets/book_grid_item.dart';
|
||||
|
||||
/// EPUB 书架页面
|
||||
class EpubLibraryPage extends StatefulWidget {
|
||||
@@ -25,9 +24,6 @@ class _EpubLibraryPageState extends State<EpubLibraryPage> {
|
||||
bool _isLoading = true;
|
||||
bool _isSearching = false;
|
||||
final TextEditingController _searchCtrl = TextEditingController();
|
||||
ViewMode _viewMode = UserPrefs().epubViewMode == 1
|
||||
? ViewMode.compact
|
||||
: ViewMode.relaxed;
|
||||
int _sortMode = UserPrefs().epubSortMode;
|
||||
|
||||
@override
|
||||
@@ -156,15 +152,9 @@ class _EpubLibraryPageState extends State<EpubLibraryPage> {
|
||||
MaterialPageRoute(
|
||||
builder: (_) => EpubDetailPage(bookId: book['id'], book: book),
|
||||
),
|
||||
).then((_) => _loadBooks());
|
||||
}
|
||||
|
||||
void _toggleViewMode() {
|
||||
setState(() {
|
||||
_viewMode =
|
||||
_viewMode == ViewMode.relaxed ? ViewMode.compact : ViewMode.relaxed;
|
||||
).then((_) {
|
||||
if (mounted) _loadBooks();
|
||||
});
|
||||
UserPrefs().setEpubViewMode(_viewMode == ViewMode.compact ? 1 : 0);
|
||||
}
|
||||
|
||||
void _showSortMenu() {
|
||||
@@ -215,6 +205,16 @@ class _EpubLibraryPageState extends State<EpubLibraryPage> {
|
||||
);
|
||||
}
|
||||
|
||||
/// 找到最近在读的书(进度 > 0 且 < 1,按更新时间排序取第一本)
|
||||
Map<String, dynamic>? get _lastReadingBook {
|
||||
final reading = _books.where((b) {
|
||||
final p = (b['reading_percentage'] as num?)?.toDouble() ?? 0.0;
|
||||
return p > 0.0 && p < 1.0;
|
||||
}).toList();
|
||||
if (reading.isEmpty) return null;
|
||||
return reading.first;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_searchCtrl.dispose();
|
||||
@@ -280,11 +280,203 @@ class _EpubLibraryPageState extends State<EpubLibraryPage> {
|
||||
? _buildEmpty(colors)
|
||||
: _filteredBooks.isEmpty
|
||||
? Center(child: Text('无搜索结果', style: TextStyle(fontSize: 14, color: colors.onSurface.withValues(alpha: 0.35))))
|
||||
: _buildGrid(colors)),
|
||||
: RefreshIndicator(
|
||||
color: colors.primary,
|
||||
onRefresh: _loadBooks,
|
||||
child: _buildContent(colors),
|
||||
)),
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
/// 主体内容:继续阅读横幅 + 书架列表
|
||||
Widget _buildContent(ColorScheme colors) {
|
||||
final lastBook = _lastReadingBook;
|
||||
return CustomScrollView(
|
||||
slivers: [
|
||||
// 继续阅读横幅
|
||||
if (lastBook != null && !_isSearching)
|
||||
SliverToBoxAdapter(child: _buildContinueReading(colors, lastBook)),
|
||||
// 书架列表
|
||||
_buildSliverListView(colors),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// 继续阅读横幅卡片 — 封面背景 + 毛玻璃
|
||||
Widget _buildContinueReading(ColorScheme colors, Map<String, dynamic> book) {
|
||||
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;
|
||||
final percentStr = '${(progress * 100).toInt()}%';
|
||||
final hasCover = coverPath != null && coverPath.isNotEmpty && File(coverPath).existsSync();
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 16, 16, 0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
GestureDetector(
|
||||
onTap: () => _openBook(book),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
child: Stack(
|
||||
fit: StackFit.passthrough,
|
||||
children: [
|
||||
// 底层:封面图做背景
|
||||
if (hasCover)
|
||||
SizedBox(
|
||||
height: 140,
|
||||
width: double.infinity,
|
||||
child: Image.file(
|
||||
File(coverPath!),
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, __, ___) => Container(color: colors.primaryContainer),
|
||||
),
|
||||
),
|
||||
// 毛玻璃遮罩层
|
||||
ClipRRect(
|
||||
child: BackdropFilter(
|
||||
filter: ImageFilter.blur(sigmaX: 16, sigmaY: 16),
|
||||
child: Container(
|
||||
height: 140,
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surface.withValues(alpha: 0.35),
|
||||
borderRadius: hasCover ? BorderRadius.zero : BorderRadius.circular(16),
|
||||
),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 封面
|
||||
Container(
|
||||
width: 56,
|
||||
height: 78,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
color: colors.outlineVariant,
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: colors.shadow.withValues(alpha: 0.2),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(2, 3),
|
||||
),
|
||||
],
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: _buildCover(coverPath, colors),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(title,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w700,
|
||||
color: colors.onSurface,
|
||||
)),
|
||||
if (author.isNotEmpty) ...[
|
||||
const SizedBox(height: 3),
|
||||
Text(author,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: colors.onSurface.withValues(alpha: 0.55),
|
||||
)),
|
||||
],
|
||||
const Spacer(),
|
||||
// 进度条 + 百分比
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
child: LinearProgressIndicator(
|
||||
value: progress,
|
||||
minHeight: 6,
|
||||
backgroundColor: colors.primary.withValues(alpha: 0.15),
|
||||
valueColor: AlwaysStoppedAnimation<Color>(colors.primary),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
Text(percentStr,
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w700,
|
||||
color: colors.primary,
|
||||
)),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
// 继续阅读按钮
|
||||
Align(
|
||||
alignment: Alignment.centerRight,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 7),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.primary,
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.play_arrow_rounded, size: 16, color: colors.onPrimary),
|
||||
const SizedBox(width: 4),
|
||||
Text('继续阅读',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: colors.onPrimary,
|
||||
)),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
// 书架分隔
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 20, left: 4, bottom: 4),
|
||||
child: Row(
|
||||
children: [
|
||||
Text('书架',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: colors.onSurface.withValues(alpha: 0.45),
|
||||
)),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Container(
|
||||
height: 0.5,
|
||||
color: colors.outlineVariant,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
List<Widget> _buildActions(ColorScheme colors) {
|
||||
return [
|
||||
if (!_isSearching)
|
||||
@@ -292,16 +484,6 @@ class _EpubLibraryPageState extends State<EpubLibraryPage> {
|
||||
icon: Icon(Icons.search, size: 20, color: colors.onSurface.withValues(alpha: 0.6)),
|
||||
onPressed: _toggleSearch,
|
||||
),
|
||||
IconButton(
|
||||
icon: Icon(
|
||||
_viewMode == ViewMode.relaxed
|
||||
? Icons.view_compact_outlined
|
||||
: Icons.view_agenda_outlined,
|
||||
size: 20,
|
||||
color: colors.onSurface.withValues(alpha: 0.6),
|
||||
),
|
||||
onPressed: _toggleViewMode,
|
||||
),
|
||||
IconButton(
|
||||
icon: Icon(Icons.sort, size: 20, color: colors.onSurface.withValues(alpha: 0.6)),
|
||||
onPressed: _showSortMenu,
|
||||
@@ -355,104 +537,90 @@ class _EpubLibraryPageState extends State<EpubLibraryPage> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildGrid(ColorScheme colors) {
|
||||
final bool showList = _viewMode == ViewMode.compact;
|
||||
// ─── Sliver 书架 ────────────────────────────────────────────────────
|
||||
|
||||
if (showList) {
|
||||
return _buildListView(colors);
|
||||
}
|
||||
return _buildGridView(colors);
|
||||
}
|
||||
|
||||
Widget _buildGridView(ColorScheme colors) {
|
||||
return LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final count = responsiveCrossAxisCount(constraints.maxWidth, minItemWidth: 120);
|
||||
return GridView.builder(
|
||||
padding: const EdgeInsets.fromLTRB(16, 16, 16, 100),
|
||||
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: count,
|
||||
crossAxisSpacing: 12,
|
||||
mainAxisSpacing: 16,
|
||||
childAspectRatio: 0.55,
|
||||
),
|
||||
itemCount: _filteredBooks.length,
|
||||
itemBuilder: (context, index) {
|
||||
final book = _filteredBooks[index];
|
||||
return BookGridItem(
|
||||
book: book,
|
||||
viewMode: ViewMode.relaxed,
|
||||
onTap: () => _openBook(book),
|
||||
onLongPress: () => _deleteBook(book),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
Widget _buildSliverListView(ColorScheme colors) {
|
||||
return SliverPadding(
|
||||
padding: const EdgeInsets.fromLTRB(12, 8, 12, 100),
|
||||
sliver: SliverList(
|
||||
delegate: SliverChildBuilderDelegate(
|
||||
(context, index) => _buildListItem(colors, _filteredBooks[index]),
|
||||
childCount: _filteredBooks.length,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildListView(ColorScheme colors) {
|
||||
return ListView.builder(
|
||||
padding: const EdgeInsets.fromLTRB(12, 8, 12, 100),
|
||||
itemCount: _filteredBooks.length,
|
||||
itemBuilder: (context, index) {
|
||||
final book = _filteredBooks[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;
|
||||
Widget _buildListItem(ColorScheme colors, Map<String, dynamic> book) {
|
||||
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;
|
||||
final updatedAt = book['updated_at'] as String?;
|
||||
|
||||
// 阅读状态推断
|
||||
final String statusLabel;
|
||||
final Color statusColor;
|
||||
if (progress >= 1.0) {
|
||||
statusLabel = '已读';
|
||||
statusColor = const Color(0xFF16A34A);
|
||||
} else if (progress > 0.0) {
|
||||
statusLabel = '在读';
|
||||
statusColor = colors.primary;
|
||||
} else {
|
||||
statusLabel = '未读';
|
||||
statusColor = const Color(0xFFDC2626);
|
||||
}
|
||||
// 阅读状态推断
|
||||
final String statusLabel;
|
||||
final Color statusColor;
|
||||
if (progress >= 1.0) {
|
||||
statusLabel = '已读';
|
||||
statusColor = const Color(0xFF16A34A);
|
||||
} else if (progress > 0.0) {
|
||||
statusLabel = '在读';
|
||||
statusColor = colors.primary;
|
||||
} else {
|
||||
statusLabel = '未读';
|
||||
statusColor = const Color(0xFFDC2626);
|
||||
}
|
||||
|
||||
return GestureDetector(
|
||||
onTap: () => _openBook(book),
|
||||
onLongPress: () => _deleteBook(book),
|
||||
child: Container(
|
||||
margin: const EdgeInsets.only(bottom: 8),
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHigh,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
// 最后阅读时间
|
||||
String? lastReadText;
|
||||
if (updatedAt != null && updatedAt.isNotEmpty) {
|
||||
try {
|
||||
final dt = DateTime.parse(updatedAt);
|
||||
lastReadText = _formatRelativeDate(dt);
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
return RepaintBoundary(
|
||||
child: GestureDetector(
|
||||
onTap: () => _openBook(book),
|
||||
onLongPress: () => _deleteBook(book),
|
||||
child: Container(
|
||||
margin: const EdgeInsets.only(bottom: 8),
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHigh,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
// 封面
|
||||
Container(
|
||||
width: 48, height: 64,
|
||||
decoration: BoxDecoration(
|
||||
color: colors.outlineVariant,
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: _buildCover(coverPath, colors),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
// 封面
|
||||
Container(
|
||||
width: 48, height: 64,
|
||||
decoration: BoxDecoration(
|
||||
color: colors.outlineVariant,
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: _buildCover(coverPath, colors),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
// 信息
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
const SizedBox(width: 12),
|
||||
// 信息
|
||||
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: 3),
|
||||
Text(author, maxLines: 1, overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(fontSize: 12, color: colors.onSurface.withValues(alpha: 0.35))),
|
||||
],
|
||||
const SizedBox(height: 6),
|
||||
// 状态标签 + 最后阅读时间
|
||||
Row(
|
||||
children: [
|
||||
Text(title, maxLines: 1, overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(fontSize: 15, fontWeight: FontWeight.w600, color: colors.onSurface)),
|
||||
if (author.isNotEmpty) ...[
|
||||
const SizedBox(height: 3),
|
||||
Text(author, maxLines: 1, overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(fontSize: 12, color: colors.onSurface.withValues(alpha: 0.35))),
|
||||
],
|
||||
const SizedBox(height: 6),
|
||||
// 状态标签
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
|
||||
decoration: BoxDecoration(
|
||||
@@ -462,16 +630,51 @@ class _EpubLibraryPageState extends State<EpubLibraryPage> {
|
||||
child: Text(statusLabel,
|
||||
style: TextStyle(fontSize: 11, fontWeight: FontWeight.w500, color: statusColor)),
|
||||
),
|
||||
if (lastReadText != null) ...[
|
||||
const SizedBox(width: 8),
|
||||
Text(lastReadText,
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: colors.onSurface.withValues(alpha: 0.3),
|
||||
)),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Icon(Icons.chevron_right, color: colors.onSurface.withValues(alpha: 0.2), size: 20),
|
||||
],
|
||||
// 进度条
|
||||
if (progress > 0 && progress < 1.0) ...[
|
||||
const SizedBox(height: 6),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(2),
|
||||
child: LinearProgressIndicator(
|
||||
value: progress,
|
||||
minHeight: 3,
|
||||
backgroundColor: colors.outlineVariant,
|
||||
valueColor: AlwaysStoppedAnimation<Color>(colors.primary),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text('${(progress * 100).toInt()}%',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: colors.primary,
|
||||
)),
|
||||
],
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
const SizedBox(width: 8),
|
||||
Icon(Icons.chevron_right, color: colors.onSurface.withValues(alpha: 0.2), size: 20),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -479,8 +682,17 @@ class _EpubLibraryPageState extends State<EpubLibraryPage> {
|
||||
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),
|
||||
child: Image.file(
|
||||
File(path),
|
||||
fit: BoxFit.cover,
|
||||
width: double.infinity,
|
||||
height: double.infinity,
|
||||
errorBuilder: (_, __, ___) => Container(
|
||||
color: colors.outlineVariant,
|
||||
child: Icon(Icons.auto_stories_outlined, size: 22,
|
||||
color: colors.onSurface.withValues(alpha: 0.25)),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
return Container(
|
||||
@@ -493,4 +705,23 @@ class _EpubLibraryPageState extends State<EpubLibraryPage> {
|
||||
);
|
||||
}
|
||||
|
||||
/// 相对时间格式化
|
||||
String _formatRelativeDate(DateTime date) {
|
||||
final now = DateTime.now();
|
||||
final diff = now.difference(date);
|
||||
if (diff.isNegative) return '${date.month}月${date.day}日';
|
||||
if (diff.inDays == 0) {
|
||||
if (diff.inHours == 0) {
|
||||
if (diff.inMinutes == 0) return '刚刚';
|
||||
return '${diff.inMinutes}分钟前';
|
||||
}
|
||||
return '${diff.inHours}小时前';
|
||||
} else if (diff.inDays < 7) {
|
||||
return '${diff.inDays}天前';
|
||||
} else if (diff.inDays < 30) {
|
||||
return '${(diff.inDays / 7).floor()}周前';
|
||||
} else {
|
||||
return '${date.month}月${date.day}日';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,13 +24,15 @@ class BookGridItem extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
return RepaintBoundary(
|
||||
child: GestureDetector(
|
||||
onTap: onTap,
|
||||
onLongPress: onLongPress,
|
||||
child: switch (viewMode) {
|
||||
ViewMode.relaxed => _buildRelaxed(context),
|
||||
ViewMode.compact => _buildCompact(context),
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -41,6 +43,7 @@ class BookGridItem extends StatelessWidget {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
final title = book['title'] as String? ?? '';
|
||||
final author = book['author'] as String? ?? '';
|
||||
final progress = _readingProgress;
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
@@ -59,8 +62,16 @@ class BookGridItem extends StatelessWidget {
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: _buildCoverStack(context, fit: StackFit.expand, extras: [
|
||||
// 阅读状态角标
|
||||
_buildStatusBadge(context),
|
||||
// 阅读状态圆点
|
||||
_buildStatusDot(context),
|
||||
// 底部进度条
|
||||
if (progress > 0 && progress < 1.0)
|
||||
Positioned(
|
||||
bottom: 0,
|
||||
left: 0,
|
||||
right: 0,
|
||||
child: _buildProgressBar(context, progress, height: 3),
|
||||
),
|
||||
]),
|
||||
),
|
||||
),
|
||||
@@ -95,13 +106,22 @@ class BookGridItem extends StatelessWidget {
|
||||
Widget _buildCompact(BuildContext context) {
|
||||
final title = book['title'] as String? ?? '';
|
||||
final author = book['author'] as String? ?? '';
|
||||
final progress = _readingProgress;
|
||||
|
||||
return _buildCoverStack(
|
||||
context,
|
||||
fit: StackFit.expand,
|
||||
extras: [
|
||||
// 阅读状态角标
|
||||
_buildStatusBadge(context),
|
||||
// 阅读状态圆点
|
||||
_buildStatusDot(context),
|
||||
// 底部进度条
|
||||
if (progress > 0 && progress < 1.0)
|
||||
Positioned(
|
||||
bottom: 0,
|
||||
left: 0,
|
||||
right: 0,
|
||||
child: _buildProgressBar(context, progress, height: 3),
|
||||
),
|
||||
Positioned(
|
||||
bottom: 0,
|
||||
left: 0,
|
||||
@@ -193,23 +213,30 @@ class BookGridItem extends StatelessWidget {
|
||||
}
|
||||
|
||||
Widget _buildPlaceholder(BuildContext context) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
final title = book['title'] as String? ?? '';
|
||||
final initial = title.isNotEmpty ? title.substring(0, 1) : '';
|
||||
// 根据书名首字生成渐变色
|
||||
final gradientColors = _generateGradientColors(initial);
|
||||
return Container(
|
||||
color: colors.surfaceContainerHighest,
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: gradientColors,
|
||||
),
|
||||
),
|
||||
child: Center(
|
||||
child: initial.isNotEmpty
|
||||
? Text(initial,
|
||||
style: TextStyle(
|
||||
fontSize: 32,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: colors.onSurface.withValues(alpha: 0.2),
|
||||
color: Colors.white.withValues(alpha: 0.85),
|
||||
))
|
||||
: Icon(
|
||||
Icons.auto_stories_outlined,
|
||||
size: 36,
|
||||
color: colors.onSurface.withValues(alpha: 0.2),
|
||||
color: Colors.white.withValues(alpha: 0.5),
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -217,44 +244,76 @@ class BookGridItem extends StatelessWidget {
|
||||
|
||||
// ─── badge helpers ────────────────────────────────────────────────────────
|
||||
|
||||
/// 阅读状态角标(左上角,含百分比)
|
||||
Widget _buildStatusBadge(BuildContext context) {
|
||||
/// 阅读状态圆点(左上角,三色)
|
||||
Widget _buildStatusDot(BuildContext context) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
final progress = _readingProgress;
|
||||
final String label;
|
||||
final Color color;
|
||||
final Color dotColor;
|
||||
if (progress >= 1.0) {
|
||||
label = '已读';
|
||||
color = const Color(0xFF16A34A);
|
||||
dotColor = const Color(0xFF16A34A);
|
||||
} else if (progress > 0.0) {
|
||||
label = '在读 ${(progress * 100).toInt()}%';
|
||||
color = colors.primary;
|
||||
dotColor = colors.primary;
|
||||
} else {
|
||||
label = '未读';
|
||||
color = const Color(0xFFDC2626);
|
||||
dotColor = const Color(0xFFDC2626);
|
||||
}
|
||||
|
||||
return Positioned(
|
||||
top: 6,
|
||||
left: 6,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 2),
|
||||
width: 8,
|
||||
height: 8,
|
||||
decoration: BoxDecoration(
|
||||
color: color.withValues(alpha: 0.85),
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
color: dotColor,
|
||||
shape: BoxShape.circle,
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: dotColor.withValues(alpha: 0.4),
|
||||
blurRadius: 3,
|
||||
offset: const Offset(0, 1),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Text(label,
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 9,
|
||||
fontWeight: FontWeight.w600,
|
||||
)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 底部进度条
|
||||
Widget _buildProgressBar(BuildContext context, double progress, {double height = 3}) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
LinearProgressIndicator(
|
||||
value: progress,
|
||||
minHeight: height,
|
||||
backgroundColor: Colors.black26,
|
||||
valueColor: AlwaysStoppedAnimation<Color>(colors.primary),
|
||||
borderRadius: BorderRadius.zero,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
// ─── helpers ──────────────────────────────────────────────────────────────
|
||||
|
||||
double get _readingProgress =>
|
||||
(book['reading_percentage'] as num?)?.toDouble() ?? 0.0;
|
||||
|
||||
/// 根据首字符生成渐变色
|
||||
List<Color> _generateGradientColors(String initial) {
|
||||
if (initial.isEmpty) return [const Color(0xFF6B7280), const Color(0xFF9CA3AF)];
|
||||
final code = initial.codeUnitAt(0);
|
||||
final palettes = [
|
||||
[const Color(0xFF6366F1), const Color(0xFF8B5CF6)], // 靛蓝-紫
|
||||
[const Color(0xFF3B82F6), const Color(0xFF06B6D4)], // 蓝-青
|
||||
[const Color(0xFF10B981), const Color(0xFF34D399)], // 绿
|
||||
[const Color(0xFFF59E0B), const Color(0xFFF97316)], // 琥珀-橙
|
||||
[const Color(0xFFEF4444), const Color(0xFFF472B6)], // 红-粉
|
||||
[const Color(0xFF8B5CF6), const Color(0xFFEC4899)], // 紫-粉
|
||||
[const Color(0xFF14B8A6), const Color(0xFF3B82F6)], // 青-蓝
|
||||
[const Color(0xFFF97316), const Color(0xFFEF4444)], // 橙-红
|
||||
];
|
||||
return palettes[code % palettes.length];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import 'dart:io';
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_markdown_plus/flutter_markdown_plus.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
@@ -12,7 +10,6 @@ import '../../models/data_models.dart';
|
||||
import '../../utils/toast_util.dart';
|
||||
import '../../utils/image_path_helper.dart';
|
||||
import '../../widgets/fade_in_local_image.dart';
|
||||
import '../../widgets/tag_side_panel.dart';
|
||||
import '../../widgets/vditor_editor.dart';
|
||||
|
||||
class NoteAddPage extends StatefulWidget {
|
||||
@@ -125,20 +122,19 @@ class _NoteAddPageState extends State<NoteAddPage> {
|
||||
}
|
||||
|
||||
Widget _buildEditArea(ColorScheme colors) {
|
||||
final isWin = Platform.isWindows;
|
||||
return Column(children: [
|
||||
// 标题输入(Windows: 更大更醒目)
|
||||
// 标题输入
|
||||
Container(
|
||||
padding: EdgeInsets.symmetric(horizontal: isWin ? 48 : 16, vertical: isWin ? 16 : 8),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
decoration: BoxDecoration(border: Border(bottom: BorderSide(color: colors.outlineVariant, width: 0.5))),
|
||||
child: Center(child: ConstrainedBox(constraints: const BoxConstraints(maxWidth: 720),
|
||||
child: TextField(
|
||||
controller: _titleCtrl,
|
||||
maxLines: 1,
|
||||
style: TextStyle(fontSize: isWin ? 22 : 16, fontWeight: FontWeight.w700, color: colors.onSurface, height: 1.4),
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w700, color: colors.onSurface, height: 1.4),
|
||||
decoration: InputDecoration(
|
||||
hintText: '添加标题',
|
||||
hintStyle: TextStyle(fontSize: isWin ? 22 : 16, fontWeight: FontWeight.w700, color: colors.onSurface.withValues(alpha: 0.2), height: 1.4),
|
||||
hintStyle: TextStyle(fontSize: 16, fontWeight: FontWeight.w700, color: colors.onSurface.withValues(alpha: 0.2), height: 1.4),
|
||||
border: InputBorder.none, enabledBorder: InputBorder.none, focusedBorder: InputBorder.none,
|
||||
isDense: true,
|
||||
contentPadding: EdgeInsets.zero,
|
||||
@@ -147,163 +143,29 @@ class _NoteAddPageState extends State<NoteAddPage> {
|
||||
),
|
||||
)),
|
||||
),
|
||||
// Windows: 标签栏(彩色药丸样式)
|
||||
if (isWin)
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 48, vertical: 8),
|
||||
child: Center(child: ConstrainedBox(constraints: const BoxConstraints(maxWidth: 720),
|
||||
child: Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: SingleChildScrollView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
child: Row(mainAxisSize: MainAxisSize.min, children: [
|
||||
for (int i = 0; i < _tags.length; i++)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 6),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.primaryContainer,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Row(mainAxisSize: MainAxisSize.min, children: [
|
||||
Text(_tags[i], style: TextStyle(fontSize: 12, color: colors.onPrimaryContainer, fontWeight: FontWeight.w500)),
|
||||
const SizedBox(width: 4),
|
||||
GestureDetector(
|
||||
onTap: () => setState(() => _tags.removeAt(i)),
|
||||
child: Icon(Icons.close, size: 12, color: colors.onPrimaryContainer.withValues(alpha: 0.6)),
|
||||
),
|
||||
]),
|
||||
),
|
||||
),
|
||||
GestureDetector(
|
||||
onTap: _showTagPanel,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 5),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: colors.outline.withValues(alpha: 0.3), width: 1),
|
||||
),
|
||||
child: Row(mainAxisSize: MainAxisSize.min, children: [
|
||||
Icon(Icons.add, size: 14, color: colors.onSurface.withValues(alpha: 0.4)),
|
||||
const SizedBox(width: 3),
|
||||
Text('标签', style: TextStyle(fontSize: 12, color: colors.onSurface.withValues(alpha: 0.4))),
|
||||
]),
|
||||
),
|
||||
),
|
||||
]),
|
||||
),
|
||||
),
|
||||
)),
|
||||
),
|
||||
// 内容编辑(Windows: 限宽居中)
|
||||
// 内容编辑
|
||||
Expanded(
|
||||
child: isWin
|
||||
? Center(child: ConstrainedBox(constraints: const BoxConstraints(maxWidth: 720),
|
||||
child: VditorEditor(
|
||||
key: _vditorKey,
|
||||
initialContent: _contentCtrl.text,
|
||||
noteId: _tempId,
|
||||
isDark: Theme.of(context).brightness == Brightness.dark,
|
||||
surfaceColor: colors.surface,
|
||||
onContentChanged: (value) {
|
||||
_contentCtrl.text = value;
|
||||
setState(() {});
|
||||
},
|
||||
),
|
||||
))
|
||||
: TextField(
|
||||
controller: _contentCtrl,
|
||||
maxLines: null,
|
||||
expands: true,
|
||||
textAlignVertical: TextAlignVertical.top,
|
||||
strutStyle: const StrutStyle(forceStrutHeight: true, height: 1.6, fontSize: 14),
|
||||
style: TextStyle(fontSize: 14, color: colors.onSurface, height: 1.6),
|
||||
decoration: InputDecoration(
|
||||
hintText: '使用 Markdown 格式书写...',
|
||||
hintStyle: TextStyle(fontSize: 14, color: colors.onSurface.withValues(alpha: 0.25), height: 1.6),
|
||||
border: InputBorder.none, enabledBorder: InputBorder.none, focusedBorder: InputBorder.none,
|
||||
contentPadding: const EdgeInsets.all(16),
|
||||
),
|
||||
onChanged: (_) => setState(() {}),
|
||||
),
|
||||
child: VditorEditor(
|
||||
key: _vditorKey,
|
||||
initialContent: _contentCtrl.text,
|
||||
noteId: _tempId,
|
||||
isDark: Theme.of(context).brightness == Brightness.dark,
|
||||
surfaceColor: colors.surface,
|
||||
onContentChanged: (value) {
|
||||
_contentCtrl.text = value;
|
||||
setState(() {});
|
||||
},
|
||||
),
|
||||
),
|
||||
// 图片行
|
||||
if (_images.isNotEmpty) _buildImageRow(colors),
|
||||
// 底部标签 + 工具栏(仅非 Windows)
|
||||
if (!isWin)
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
|
||||
decoration: BoxDecoration(border: Border(top: BorderSide(color: colors.outlineVariant, width: 0.5))),
|
||||
child: Column(children: [
|
||||
// 标签行
|
||||
Wrap(spacing: 6, runSpacing: 4, children: [
|
||||
for (int i = 0; i < _tags.length; i++) Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(color: colors.surfaceContainerHighest, borderRadius: BorderRadius.circular(6)),
|
||||
child: Row(mainAxisSize: MainAxisSize.min, children: [
|
||||
Text(_tags[i], style: TextStyle(fontSize: 11, color: colors.onSurface.withValues(alpha: 0.6))),
|
||||
const SizedBox(width: 3),
|
||||
GestureDetector(
|
||||
onTap: () => setState(() => _tags.removeAt(i)),
|
||||
child: Icon(Icons.close, size: 10, color: colors.onSurface.withValues(alpha: 0.3)),
|
||||
),
|
||||
]),
|
||||
),
|
||||
GestureDetector(
|
||||
onTap: _showTagPanel,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
border: Border.all(color: colors.onSurface.withValues(alpha: 0.25), width: 1),
|
||||
),
|
||||
child: Row(mainAxisSize: MainAxisSize.min, children: [
|
||||
Icon(Icons.add, size: 12, color: colors.onSurface.withValues(alpha: 0.35)),
|
||||
const SizedBox(width: 2),
|
||||
Text('标签', style: TextStyle(fontSize: 11, color: colors.onSurface.withValues(alpha: 0.35))),
|
||||
]),
|
||||
),
|
||||
),
|
||||
]),
|
||||
const SizedBox(height: 6),
|
||||
// 工具栏
|
||||
Row(children: [
|
||||
Expanded(
|
||||
child: SingleChildScrollView(scrollDirection: Axis.horizontal, child: Row(children: [
|
||||
_toolBtn(Icons.title, '标题', _insertHeading),
|
||||
_toolBtn(Icons.format_bold, '粗体', () => _insertMarkdown('**', '**')),
|
||||
_toolBtn(Icons.format_italic, '斜体', () => _insertMarkdown('*', '*')),
|
||||
_toolBtn(Icons.format_strikethrough, '删除线', () => _insertMarkdown('~~', '~~')),
|
||||
_toolGap(colors),
|
||||
_toolBtn(Icons.format_list_bulleted, '无序列表', () => _insertMarkdown('- ', '')),
|
||||
_toolBtn(Icons.format_list_numbered, '有序列表', () => _insertMarkdown('1. ', '')),
|
||||
_toolBtn(Icons.format_quote, '引用', () => _insertMarkdown('> ', '')),
|
||||
_toolBtn(Icons.insert_link, '链接', () => _insertMarkdown('[', '](url)')),
|
||||
_toolGap(colors),
|
||||
_toolBtn(Icons.code, '行内代码', () => _insertMarkdown('`', '`')),
|
||||
_toolBtn(Icons.data_object, '代码块', () => _insertMarkdown('```\n', '\n```')),
|
||||
_toolBtn(Icons.horizontal_rule, '分割线', () => _insertMarkdown('---\n', '')),
|
||||
_toolGap(colors),
|
||||
_toolBtn(Icons.add_photo_alternate_outlined, '图片', _pickImage),
|
||||
])),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text('${_contentCtrl.text.length} 字',
|
||||
style: TextStyle(fontSize: 11, color: colors.onSurface.withValues(alpha: 0.3))),
|
||||
]),
|
||||
]),
|
||||
),
|
||||
// Windows: 底部字数(简洁)
|
||||
if (isWin)
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 48, vertical: 8),
|
||||
child: Center(child: ConstrainedBox(constraints: const BoxConstraints(maxWidth: 720),
|
||||
child: Row(mainAxisAlignment: MainAxisAlignment.end, children: [
|
||||
Text('${_contentCtrl.text.length} 字', style: TextStyle(fontSize: 11, color: colors.onSurface.withValues(alpha: 0.3))),
|
||||
]),
|
||||
)),
|
||||
),
|
||||
// 底部字数
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
child: Row(mainAxisAlignment: MainAxisAlignment.end, children: [
|
||||
Text('${_contentCtrl.text.length} 字', style: TextStyle(fontSize: 11, color: colors.onSurface.withValues(alpha: 0.3))),
|
||||
]),
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -400,78 +262,6 @@ class _NoteAddPageState extends State<NoteAddPage> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _toolBtn(IconData icon, String tooltip, VoidCallback onTap) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
return Material(
|
||||
color: Colors.transparent,
|
||||
child: InkWell(
|
||||
onTap: onTap,
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
child: Tooltip(
|
||||
message: tooltip,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
|
||||
child: Icon(icon, size: 16, color: colors.onSurface.withValues(alpha: 0.6)),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _toolGap(ColorScheme colors) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 3),
|
||||
child: SizedBox(height: 12, child: VerticalDivider(width: 0, thickness: 0.5, color: colors.outline)),
|
||||
);
|
||||
}
|
||||
|
||||
void _insertMarkdown(String left, String right) {
|
||||
final text = _contentCtrl.text;
|
||||
final selection = _contentCtrl.selection;
|
||||
final start = selection.start;
|
||||
final end = selection.end;
|
||||
String selectedText = end > start ? text.substring(start, end) : '';
|
||||
final insertion = '$left$selectedText$right';
|
||||
_contentCtrl.value = TextEditingValue(
|
||||
text: text.substring(0, start) + insertion + text.substring(end),
|
||||
selection: TextSelection.collapsed(
|
||||
offset: selectedText.isEmpty ? start + left.length : start + left.length + selectedText.length + right.length,
|
||||
),
|
||||
);
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
void _insertHeading() {
|
||||
final text = _contentCtrl.text;
|
||||
final selection = _contentCtrl.selection;
|
||||
final start = selection.start;
|
||||
int lineStart = start;
|
||||
while (lineStart > 0 && text[lineStart - 1] != '\n') lineStart--;
|
||||
int hashCount = 0;
|
||||
int pos = lineStart;
|
||||
while (pos < text.length && text[pos] == '#') { hashCount++; pos++; }
|
||||
if (pos < text.length && text[pos] == ' ') pos++;
|
||||
if (hashCount > 0 && hashCount < 6) {
|
||||
hashCount++;
|
||||
final newPrefix = '${'#' * hashCount} ';
|
||||
_contentCtrl.value = TextEditingValue(
|
||||
text: text.substring(0, lineStart) + newPrefix + text.substring(pos),
|
||||
selection: TextSelection.collapsed(offset: lineStart + newPrefix.length),
|
||||
);
|
||||
} else if (hashCount >= 6) {
|
||||
_contentCtrl.value = TextEditingValue(
|
||||
text: text.substring(0, lineStart) + text.substring(pos),
|
||||
selection: TextSelection.collapsed(offset: lineStart),
|
||||
);
|
||||
} else {
|
||||
_contentCtrl.value = TextEditingValue(
|
||||
text: text.substring(0, lineStart) + '# ' + text.substring(lineStart),
|
||||
selection: TextSelection.collapsed(offset: lineStart + 2),
|
||||
);
|
||||
}
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
Future<void> _pickImage() async {
|
||||
try {
|
||||
final XFile? image = await _picker.pickImage(source: ImageSource.gallery, maxWidth: 1920, maxHeight: 1920, imageQuality: 85);
|
||||
@@ -487,20 +277,6 @@ class _NoteAddPageState extends State<NoteAddPage> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _showTagPanel() async {
|
||||
final provider = context.read<AppProvider>();
|
||||
final tagRows = await provider.getTags('note_tag');
|
||||
final allTags = tagRows.map((t) => t['name'] as String).toSet();
|
||||
for (final note in provider.notes) { allTags.addAll(note.tags); }
|
||||
if (!mounted) return;
|
||||
TagSidePanel.show(
|
||||
context: context,
|
||||
selectedTags: List.from(_tags),
|
||||
allAvailableTags: allTags.toList()..sort(),
|
||||
onTagsChanged: (newTags) => setState(() => _tags = newTags),
|
||||
);
|
||||
}
|
||||
|
||||
MarkdownStyleSheet _buildMarkdownStyleSheet(ColorScheme colors) {
|
||||
return MarkdownStyleSheet(
|
||||
h1: TextStyle(fontSize: 24, fontWeight: FontWeight.w600, color: colors.onSurface, height: 1.4),
|
||||
@@ -538,7 +314,7 @@ class _NoteAddPageState extends State<NoteAddPage> {
|
||||
Future<void> _save() async {
|
||||
final title = _titleCtrl.text.trim();
|
||||
String content;
|
||||
if (Platform.isWindows && _vditorKey.currentState != null && _vditorKey.currentState!.isReady) {
|
||||
if (_vditorKey.currentState != null && _vditorKey.currentState!.isReady) {
|
||||
content = (await _vditorKey.currentState!.getValue()).trim();
|
||||
} else {
|
||||
content = _contentCtrl.text.trim();
|
||||
@@ -587,9 +363,4 @@ class _NoteAddPageState extends State<NoteAddPage> {
|
||||
}
|
||||
}
|
||||
|
||||
static List<String> _collectUnique(List<List<String>> lists) {
|
||||
final s = <String>{};
|
||||
for (final l in lists) { s.addAll(l); }
|
||||
return s.toList()..sort();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,14 +5,12 @@ import 'package:flutter_markdown_plus/flutter_markdown_plus.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:uuid/uuid.dart';
|
||||
import '../../providers/app_provider.dart';
|
||||
import '../../widgets/fade_in_local_image.dart';
|
||||
import '../../models/data_models.dart';
|
||||
import '../../utils/toast_util.dart';
|
||||
import '../../utils/image_path_helper.dart';
|
||||
import '../../utils/responsive.dart';
|
||||
import '../../widgets/tag_side_panel.dart';
|
||||
import '../../widgets/vditor_editor.dart';
|
||||
import 'note_share_page.dart';
|
||||
|
||||
@@ -79,7 +77,7 @@ class _NoteDetailPageState extends State<NoteDetailPage> {
|
||||
|
||||
Future<void> _autoSave() async {
|
||||
String content;
|
||||
if (Platform.isWindows && _vditorKey.currentState != null && _vditorKey.currentState!.isReady) {
|
||||
if (_vditorKey.currentState != null && _vditorKey.currentState!.isReady) {
|
||||
content = (await _vditorKey.currentState!.getValue()).trim();
|
||||
} else {
|
||||
content = _contentCtrl.text.trim();
|
||||
@@ -107,7 +105,7 @@ class _NoteDetailPageState extends State<NoteDetailPage> {
|
||||
_autoSaveTimer?.cancel();
|
||||
final title = _titleCtrl.text.trim();
|
||||
String content;
|
||||
if (Platform.isWindows && _vditorKey.currentState != null && _vditorKey.currentState!.isReady) {
|
||||
if (_vditorKey.currentState != null && _vditorKey.currentState!.isReady) {
|
||||
content = (await _vditorKey.currentState!.getValue()).trim();
|
||||
} else {
|
||||
content = _contentCtrl.text.trim();
|
||||
@@ -153,13 +151,9 @@ class _NoteDetailPageState extends State<NoteDetailPage> {
|
||||
)
|
||||
: null,
|
||||
titleSpacing: 0,
|
||||
title: Text(
|
||||
note.title.isNotEmpty
|
||||
? note.title
|
||||
: _truncateContent(note.content),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
maxLines: 1,
|
||||
),
|
||||
title: note.title.isNotEmpty
|
||||
? Text(note.title, style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: colors.onSurface))
|
||||
: null,
|
||||
),
|
||||
body: Stack(
|
||||
children: [
|
||||
@@ -215,16 +209,27 @@ class _NoteDetailPageState extends State<NoteDetailPage> {
|
||||
),
|
||||
|
||||
Expanded(
|
||||
child: Markdown(
|
||||
data: note.content,
|
||||
styleSheet: _buildMarkdownStyleSheet(colors),
|
||||
child: ListView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
// ignore: deprecated_member_use
|
||||
imageBuilder: (uri, title, alt) => _buildMarkdownImage(uri, note),
|
||||
children: [
|
||||
// Markdown 正文
|
||||
Markdown(
|
||||
data: note.content,
|
||||
styleSheet: _buildMarkdownStyleSheet(colors),
|
||||
padding: EdgeInsets.zero,
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
// ignore: deprecated_member_use
|
||||
imageBuilder: (uri, title, alt) => _buildMarkdownImage(uri, note),
|
||||
),
|
||||
// 附加图片(不在 markdown 中的独立图片)
|
||||
if (note.images.isNotEmpty) ...[
|
||||
const SizedBox(height: 16),
|
||||
_buildInlineImageGrid(note.images),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
if (note.images.isNotEmpty) _buildImageRow(note.images),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -435,173 +440,55 @@ class _NoteDetailPageState extends State<NoteDetailPage> {
|
||||
}
|
||||
|
||||
Widget _buildEditArea(ColorScheme colors, Note note) {
|
||||
final isWin = Platform.isWindows;
|
||||
return Column(children: [
|
||||
// 标题输入(Windows: 更大更醒目)
|
||||
// 标题输入
|
||||
Container(
|
||||
padding: EdgeInsets.symmetric(horizontal: isWin ? 48 : 16, vertical: isWin ? 16 : 8),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
decoration: BoxDecoration(border: Border(bottom: BorderSide(color: colors.outlineVariant, width: 0.5))),
|
||||
child: Center(child: ConstrainedBox(constraints: const BoxConstraints(maxWidth: 720),
|
||||
child: TextField(controller: _titleCtrl, maxLines: 1,
|
||||
style: TextStyle(fontSize: isWin ? 22 : 16, fontWeight: FontWeight.w700, color: colors.onSurface, height: 1.4),
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w700, color: colors.onSurface, height: 1.4),
|
||||
decoration: InputDecoration(hintText: '添加标题',
|
||||
hintStyle: TextStyle(fontSize: isWin ? 22 : 16, fontWeight: FontWeight.w700, color: colors.onSurface.withValues(alpha: 0.2), height: 1.4),
|
||||
hintStyle: TextStyle(fontSize: 16, fontWeight: FontWeight.w700, color: colors.onSurface.withValues(alpha: 0.2), height: 1.4),
|
||||
border: InputBorder.none, enabledBorder: InputBorder.none, focusedBorder: InputBorder.none, isDense: true, contentPadding: EdgeInsets.zero),
|
||||
onChanged: (_) => setState(() {})),
|
||||
)),
|
||||
),
|
||||
// Windows: 标签栏(彩色药丸样式)
|
||||
if (isWin)
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 48, vertical: 8),
|
||||
child: Center(child: ConstrainedBox(constraints: const BoxConstraints(maxWidth: 720),
|
||||
child: Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: SingleChildScrollView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
child: Row(mainAxisSize: MainAxisSize.min, children: [
|
||||
for (int i = 0; i < _editTags.length; i++)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 6),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.primaryContainer,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Row(mainAxisSize: MainAxisSize.min, children: [
|
||||
Text(_editTags[i], style: TextStyle(fontSize: 12, color: colors.onPrimaryContainer, fontWeight: FontWeight.w500)),
|
||||
const SizedBox(width: 4),
|
||||
GestureDetector(
|
||||
onTap: () => setState(() => _editTags.removeAt(i)),
|
||||
child: Icon(Icons.close, size: 12, color: colors.onPrimaryContainer.withValues(alpha: 0.6)),
|
||||
),
|
||||
]),
|
||||
),
|
||||
),
|
||||
GestureDetector(
|
||||
onTap: _showEditTagPanel,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 5),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: colors.outline.withValues(alpha: 0.3), width: 1),
|
||||
),
|
||||
child: Row(mainAxisSize: MainAxisSize.min, children: [
|
||||
Icon(Icons.add, size: 14, color: colors.onSurface.withValues(alpha: 0.4)),
|
||||
const SizedBox(width: 3),
|
||||
Text('标签', style: TextStyle(fontSize: 12, color: colors.onSurface.withValues(alpha: 0.4))),
|
||||
]),
|
||||
),
|
||||
),
|
||||
]),
|
||||
),
|
||||
),
|
||||
)),
|
||||
),
|
||||
// 内容编辑(Windows: 限宽居中)
|
||||
// 内容编辑
|
||||
Expanded(
|
||||
child: isWin
|
||||
? Center(child: ConstrainedBox(constraints: const BoxConstraints(maxWidth: 720),
|
||||
child: VditorEditor(
|
||||
key: _vditorKey,
|
||||
initialContent: _contentCtrl.text,
|
||||
noteId: widget.note.id,
|
||||
isDark: Theme.of(context).brightness == Brightness.dark,
|
||||
surfaceColor: colors.surface,
|
||||
onContentChanged: (value) {
|
||||
_contentCtrl.text = value;
|
||||
_onContentChanged();
|
||||
},
|
||||
),
|
||||
))
|
||||
: TextField(controller: _contentCtrl, maxLines: null, expands: true,
|
||||
textAlignVertical: TextAlignVertical.top,
|
||||
strutStyle: const StrutStyle(forceStrutHeight: true, height: 1.6, fontSize: 14),
|
||||
style: TextStyle(fontSize: 14, color: colors.onSurface, height: 1.6),
|
||||
decoration: InputDecoration(hintText: '使用 Markdown 格式书写...',
|
||||
hintStyle: TextStyle(fontSize: 14, color: colors.onSurface.withValues(alpha: 0.25), height: 1.6),
|
||||
border: InputBorder.none, enabledBorder: InputBorder.none, focusedBorder: InputBorder.none,
|
||||
contentPadding: const EdgeInsets.all(16)),
|
||||
onChanged: (_) => _onContentChanged()),
|
||||
child: VditorEditor(
|
||||
key: _vditorKey,
|
||||
initialContent: _contentCtrl.text,
|
||||
noteId: widget.note.id,
|
||||
isDark: Theme.of(context).brightness == Brightness.dark,
|
||||
surfaceColor: colors.surface,
|
||||
onContentChanged: (value) {
|
||||
_contentCtrl.text = value;
|
||||
_onContentChanged();
|
||||
},
|
||||
),
|
||||
),
|
||||
// 图片网格
|
||||
if (_editImages.isNotEmpty) _buildEditImageGrid(colors),
|
||||
// 底部标签 + 字数 + 工具栏(仅非 Windows)
|
||||
if (!isWin)
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
|
||||
decoration: BoxDecoration(border: Border(top: BorderSide(color: colors.outlineVariant, width: 0.5))),
|
||||
child: Column(children: [
|
||||
// 标签行
|
||||
Wrap(spacing: 6, runSpacing: 4, children: [
|
||||
for (int i = 0; i < _editTags.length; i++) Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(color: colors.surfaceContainerHighest, borderRadius: BorderRadius.circular(6)),
|
||||
child: Row(mainAxisSize: MainAxisSize.min, children: [
|
||||
Text(_editTags[i], style: TextStyle(fontSize: 11, color: colors.onSurface.withValues(alpha: 0.6))),
|
||||
const SizedBox(width: 3),
|
||||
GestureDetector(onTap: () => setState(() => _editTags.removeAt(i)),
|
||||
child: Icon(Icons.close, size: 10, color: colors.onSurface.withValues(alpha: 0.3))),
|
||||
])),
|
||||
GestureDetector(onTap: _showEditTagPanel,
|
||||
child: Container(padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
|
||||
decoration: BoxDecoration(borderRadius: BorderRadius.circular(6),
|
||||
border: Border.all(color: colors.onSurface.withValues(alpha: 0.25), width: 1)),
|
||||
child: Row(mainAxisSize: MainAxisSize.min, children: [
|
||||
Icon(Icons.add, size: 12, color: colors.onSurface.withValues(alpha: 0.35)),
|
||||
const SizedBox(width: 2),
|
||||
Text('标签', style: TextStyle(fontSize: 11, color: colors.onSurface.withValues(alpha: 0.35))),
|
||||
]))),
|
||||
]),
|
||||
const SizedBox(height: 6),
|
||||
// 工具栏
|
||||
Row(children: [
|
||||
Expanded(child: SingleChildScrollView(scrollDirection: Axis.horizontal, child: Row(children: [
|
||||
_editToolBtn(Icons.title, '标题', _insertHeading),
|
||||
_editToolBtn(Icons.format_bold, '粗体', () => _insertMarkdown('**', '**')),
|
||||
_editToolBtn(Icons.format_italic, '斜体', () => _insertMarkdown('*', '*')),
|
||||
_editToolBtn(Icons.format_strikethrough, '删除线', () => _insertMarkdown('~~', '~~')),
|
||||
_editToolGap(colors),
|
||||
_editToolBtn(Icons.format_list_bulleted, '无序列表', () => _insertMarkdown('- ', '')),
|
||||
_editToolBtn(Icons.format_list_numbered, '有序列表', () => _insertMarkdown('1. ', '')),
|
||||
_editToolBtn(Icons.format_quote, '引用', () => _insertMarkdown('> ', '')),
|
||||
_editToolBtn(Icons.insert_link, '链接', () => _insertMarkdown('[', '](url)')),
|
||||
_editToolGap(colors),
|
||||
_editToolBtn(Icons.code, '行内代码', () => _insertMarkdown('`', '`')),
|
||||
_editToolBtn(Icons.data_object, '代码块', () => _insertMarkdown('```\n', '\n```')),
|
||||
_editToolBtn(Icons.horizontal_rule, '分割线', () => _insertMarkdown('---\n', '')),
|
||||
_editToolGap(colors),
|
||||
_editToolBtn(Icons.add_photo_alternate_outlined, '图片', _pickEditImage),
|
||||
]))),
|
||||
const SizedBox(width: 8),
|
||||
Text('${_contentCtrl.text.length} 字', style: TextStyle(fontSize: 11, color: colors.onSurface.withValues(alpha: 0.3))),
|
||||
]),
|
||||
]),
|
||||
),
|
||||
// Windows: 底部字数(简洁)
|
||||
if (isWin)
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 48, vertical: 8),
|
||||
child: Center(child: ConstrainedBox(constraints: const BoxConstraints(maxWidth: 720),
|
||||
child: Row(mainAxisAlignment: MainAxisAlignment.end, children: [
|
||||
Text('${_contentCtrl.text.length} 字', style: TextStyle(fontSize: 11, color: colors.onSurface.withValues(alpha: 0.3))),
|
||||
]),
|
||||
)),
|
||||
),
|
||||
// 底部字数
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
child: Row(mainAxisAlignment: MainAxisAlignment.end, children: [
|
||||
Text('${_contentCtrl.text.length} 字', style: TextStyle(fontSize: 11, color: colors.onSurface.withValues(alpha: 0.3))),
|
||||
]),
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
Widget _buildPreviewArea(ColorScheme colors, Note note) {
|
||||
final isWin = Platform.isWindows;
|
||||
return ListView(
|
||||
padding: EdgeInsets.symmetric(vertical: isWin ? 32 : 24),
|
||||
padding: const EdgeInsets.symmetric(vertical: 24),
|
||||
children: [
|
||||
Center(child: ConstrainedBox(constraints: const BoxConstraints(maxWidth: 720),
|
||||
child: Padding(padding: EdgeInsets.symmetric(horizontal: isWin ? 48 : 24),
|
||||
child: Padding(padding: const EdgeInsets.symmetric(horizontal: 24),
|
||||
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
|
||||
if (_titleCtrl.text.isNotEmpty) ...[
|
||||
Text(_titleCtrl.text, style: TextStyle(fontSize: isWin ? 28 : 24, fontWeight: FontWeight.w700, color: colors.onSurface, height: 1.3)),
|
||||
Text(_titleCtrl.text, style: TextStyle(fontSize: 24, fontWeight: FontWeight.w700, color: colors.onSurface, height: 1.3)),
|
||||
const SizedBox(height: 12),
|
||||
],
|
||||
// 标签
|
||||
@@ -640,64 +527,6 @@ class _NoteDetailPageState extends State<NoteDetailPage> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _editToolBtn(IconData icon, String tooltip, VoidCallback onTap) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
return Material(color: Colors.transparent,
|
||||
child: InkWell(onTap: onTap, borderRadius: BorderRadius.circular(6),
|
||||
child: Tooltip(message: tooltip,
|
||||
child: Padding(padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
|
||||
child: Icon(icon, size: 16, color: colors.onSurface.withValues(alpha: 0.6))))));
|
||||
}
|
||||
|
||||
Widget _editToolGap(ColorScheme colors) {
|
||||
return Padding(padding: const EdgeInsets.symmetric(horizontal: 3),
|
||||
child: SizedBox(height: 12, child: VerticalDivider(width: 0, thickness: 0.5, color: colors.outline)));
|
||||
}
|
||||
|
||||
void _insertMarkdown(String left, String right) {
|
||||
final text = _contentCtrl.text;
|
||||
final selection = _contentCtrl.selection;
|
||||
final start = selection.start;
|
||||
final end = selection.end;
|
||||
String selectedText = end > start ? text.substring(start, end) : '';
|
||||
final insertion = '$left$selectedText$right';
|
||||
_contentCtrl.value = TextEditingValue(
|
||||
text: text.substring(0, start) + insertion + text.substring(end),
|
||||
selection: TextSelection.collapsed(
|
||||
offset: selectedText.isEmpty ? start + left.length : start + left.length + selectedText.length + right.length,
|
||||
),
|
||||
);
|
||||
_onContentChanged();
|
||||
}
|
||||
|
||||
void _insertHeading() {
|
||||
final text = _contentCtrl.text;
|
||||
final selection = _contentCtrl.selection;
|
||||
final start = selection.start;
|
||||
int lineStart = start;
|
||||
while (lineStart > 0 && text[lineStart - 1] != '\n') lineStart--;
|
||||
int hashCount = 0;
|
||||
int pos = lineStart;
|
||||
while (pos < text.length && text[pos] == '#') { hashCount++; pos++; }
|
||||
if (pos < text.length && text[pos] == ' ') pos++;
|
||||
if (hashCount > 0 && hashCount < 6) {
|
||||
hashCount++;
|
||||
final newPrefix = '${'#' * hashCount} ';
|
||||
_contentCtrl.value = TextEditingValue(
|
||||
text: text.substring(0, lineStart) + newPrefix + text.substring(pos),
|
||||
selection: TextSelection.collapsed(offset: lineStart + newPrefix.length));
|
||||
} else if (hashCount >= 6) {
|
||||
_contentCtrl.value = TextEditingValue(
|
||||
text: text.substring(0, lineStart) + text.substring(pos),
|
||||
selection: TextSelection.collapsed(offset: lineStart));
|
||||
} else {
|
||||
_contentCtrl.value = TextEditingValue(
|
||||
text: text.substring(0, lineStart) + '# ' + text.substring(lineStart),
|
||||
selection: TextSelection.collapsed(offset: lineStart + 2));
|
||||
}
|
||||
_onContentChanged();
|
||||
}
|
||||
|
||||
Future<void> _pickEditImage() async {
|
||||
try {
|
||||
final XFile? image = await _picker.pickImage(source: ImageSource.gallery, maxWidth: 1920, maxHeight: 1920, imageQuality: 85);
|
||||
@@ -714,17 +543,6 @@ class _NoteDetailPageState extends State<NoteDetailPage> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _showEditTagPanel() async {
|
||||
final provider = context.read<AppProvider>();
|
||||
final tagRows = await provider.getTags('note_tag');
|
||||
final allTags = tagRows.map((t) => t['name'] as String).toSet();
|
||||
for (final note in provider.notes) { allTags.addAll(note.tags); }
|
||||
if (!mounted) return;
|
||||
TagSidePanel.show(context: context, selectedTags: List.from(_editTags),
|
||||
allAvailableTags: allTags.toList()..sort(),
|
||||
onTagsChanged: (newTags) => setState(() => _editTags = newTags));
|
||||
}
|
||||
|
||||
Widget _buildEditImageGrid(ColorScheme colors) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.fromLTRB(16, 6, 16, 0),
|
||||
@@ -783,6 +601,37 @@ class _NoteDetailPageState extends State<NoteDetailPage> {
|
||||
);
|
||||
}
|
||||
|
||||
/// 内嵌图片网格(移动端正文后展示)
|
||||
Widget _buildInlineImageGrid(List<String> images) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
return Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: images.map((path) {
|
||||
return GestureDetector(
|
||||
onTap: () => _showImagePreview(images, images.indexOf(path)),
|
||||
child: Container(
|
||||
width: (MediaQuery.of(context).size.width - 48) / 3,
|
||||
height: (MediaQuery.of(context).size.width - 48) / 3,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: colors.outlineVariant, width: 0.5),
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: FadeInLocalImage(
|
||||
path: path,
|
||||
fit: BoxFit.cover,
|
||||
errorWidget: Container(
|
||||
color: colors.surfaceContainerHighest,
|
||||
child: Icon(Icons.broken_image_outlined, size: 20, color: colors.onSurface.withValues(alpha: 0.25)),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildImageRow(List<String> images) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
return Container(
|
||||
|
||||
@@ -4,7 +4,6 @@ import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:flutter_markdown_plus/flutter_markdown_plus.dart';
|
||||
import '../../providers/app_provider.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
import '../../models/data_models.dart';
|
||||
@@ -12,6 +11,7 @@ import '../../utils/toast_util.dart';
|
||||
import '../../utils/image_path_helper.dart';
|
||||
import '../../widgets/fade_in_local_image.dart';
|
||||
import '../../widgets/tag_side_panel.dart';
|
||||
import '../../widgets/vditor_editor.dart';
|
||||
|
||||
/// 添加/编辑笔记页面 - 极简书写界面
|
||||
class NoteFormPage extends StatefulWidget {
|
||||
@@ -32,11 +32,13 @@ class _NoteFormPageState extends State<NoteFormPage> {
|
||||
bool _isEditing = false;
|
||||
final ImagePicker _picker = ImagePicker();
|
||||
String? _tempNoteId; // 新建模式时使用的临时笔记ID
|
||||
String _editorMode = 'edit'; // 'edit' | 'preview'
|
||||
Timer? _autoSaveTimer;
|
||||
Timer? _saveStatusTimer;
|
||||
String _saveStatus = ''; // '', 'saved'
|
||||
Note? _savedNote; // 新建模式首次自动保存后的笔记引用
|
||||
final _vditorKey = GlobalKey<VditorEditorState>();
|
||||
final _scrollController = ScrollController();
|
||||
bool _editorTouched = false;
|
||||
|
||||
static const _weekdays = ['一', '二', '三', '四', '五', '六', '日'];
|
||||
|
||||
@@ -51,6 +53,9 @@ class _NoteFormPageState extends State<NoteFormPage> {
|
||||
_tags = note != null ? List.from(note.tags) : [];
|
||||
_images = note != null ? List.from(note.images) : [];
|
||||
_isEditing = note != null;
|
||||
if (!_isEditing) {
|
||||
_tempNoteId = const Uuid().v4();
|
||||
}
|
||||
_titleController.addListener(_onTextChanged);
|
||||
_contentController.addListener(_onTextChanged);
|
||||
}
|
||||
@@ -72,7 +77,12 @@ class _NoteFormPageState extends State<NoteFormPage> {
|
||||
}
|
||||
|
||||
Future<void> _autoSave() async {
|
||||
final content = _contentController.text.trim();
|
||||
String content;
|
||||
if (_vditorKey.currentState != null && _vditorKey.currentState!.isReady) {
|
||||
content = (await _vditorKey.currentState!.getValue()).trim();
|
||||
} else {
|
||||
content = _contentController.text.trim();
|
||||
}
|
||||
final title = _titleController.text.trim();
|
||||
if (title.isEmpty && content.isEmpty) return;
|
||||
|
||||
@@ -157,13 +167,22 @@ class _NoteFormPageState extends State<NoteFormPage> {
|
||||
// 可滚动内容
|
||||
Expanded(
|
||||
child: CustomScrollView(
|
||||
controller: _scrollController,
|
||||
physics: _editorTouched ? const NeverScrollableScrollPhysics() : null,
|
||||
slivers: [
|
||||
// 标题行(点击编辑)
|
||||
SliverToBoxAdapter(child: _buildTitleInput(colors)),
|
||||
|
||||
// 编辑区域 — 固定高度
|
||||
// 编辑区域 — 高度随内容撑开,触摸时禁用外层滚动
|
||||
SliverToBoxAdapter(
|
||||
child: SizedBox(height: contentH, child: _buildContentArea()),
|
||||
child: ConstrainedBox(
|
||||
constraints: BoxConstraints(minHeight: contentH),
|
||||
child: GestureDetector(
|
||||
onTapDown: (_) => setState(() => _editorTouched = true),
|
||||
onTapUp: (_) => setState(() => _editorTouched = false),
|
||||
onTapCancel: () => setState(() => _editorTouched = false),
|
||||
child: _buildEditor(),
|
||||
)),
|
||||
),
|
||||
|
||||
// 图片 + 标签 + 字数 — 随内容撑开
|
||||
@@ -256,31 +275,7 @@ class _NoteFormPageState extends State<NoteFormPage> {
|
||||
);
|
||||
}
|
||||
|
||||
/// 在光标处插入 Markdown 语法,光标自动放到正确位置
|
||||
void _insertMarkdown(String left, String right) {
|
||||
final text = _contentController.text;
|
||||
final selection = _contentController.selection;
|
||||
final start = selection.start;
|
||||
final end = selection.end;
|
||||
|
||||
String selectedText = '';
|
||||
if (end > start) {
|
||||
selectedText = text.substring(start, end);
|
||||
}
|
||||
|
||||
final insertion = '$left$selectedText$right';
|
||||
// 原子更新:一次性设置 text 和 selection,避免分步操作导致光标跳动
|
||||
_contentController.value = TextEditingValue(
|
||||
text: text.substring(0, start) + insertion + text.substring(end),
|
||||
selection: TextSelection.collapsed(
|
||||
offset: selectedText.isEmpty
|
||||
? start + left.length
|
||||
: start + left.length + selectedText.length + right.length,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 现代极简底部工具栏
|
||||
/// 底部浮动工具栏
|
||||
Widget _buildFloatingToolbar() {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
return Container(
|
||||
@@ -313,12 +308,15 @@ class _NoteFormPageState extends State<NoteFormPage> {
|
||||
_toolGap(),
|
||||
_toolBtn(Icons.format_list_bulleted, '无序列表', () => _insertMarkdown('- ', '')),
|
||||
_toolBtn(Icons.format_list_numbered, '有序列表', () => _insertMarkdown('1. ', '')),
|
||||
_toolBtn(Icons.check_box_outlined, '待办', () => _insertMarkdown('- [ ] ', '')),
|
||||
_toolBtn(Icons.format_quote, '引用', () => _insertMarkdown('> ', '')),
|
||||
_toolBtn(Icons.insert_link, '链接', () => _insertMarkdown('[', '](url)')),
|
||||
_toolGap(),
|
||||
_toolBtn(Icons.code, '行内代码', () => _insertMarkdown('`', '`')),
|
||||
_toolBtn(Icons.data_object, '代码块', () => _insertMarkdown('```\n', '\n```')),
|
||||
_toolBtn(Icons.horizontal_rule, '分割线', () => _insertMarkdown('---\n', '')),
|
||||
_toolGap(),
|
||||
_toolBtn(Icons.add_photo_alternate_outlined, '图片', _pickImage),
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -357,194 +355,38 @@ class _NoteFormPageState extends State<NoteFormPage> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _modeSwitch(IconData icon, String mode) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
final active = _editorMode == mode;
|
||||
return Material(
|
||||
color: Colors.transparent,
|
||||
child: InkWell(
|
||||
onTap: () => setState(() => _editorMode = mode),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 7, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: active ? colors.primary : Colors.transparent,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Icon(
|
||||
icon,
|
||||
size: 18,
|
||||
color: active ? colors.onPrimary : colors.onSurface.withValues(alpha: 0.35),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
/// 在光标处插入 Markdown 语法(通过 VditorEditor)
|
||||
void _insertMarkdown(String left, String right) {
|
||||
final vditor = _vditorKey.currentState;
|
||||
if (vditor != null && vditor.isReady) {
|
||||
vditor.insertValue(left + right);
|
||||
}
|
||||
}
|
||||
|
||||
/// 插入标题(在当前行首插入 # ,如果已有则升级 ## → ### → ####)
|
||||
/// 插入标题(通过 VditorEditor)
|
||||
void _insertHeading() {
|
||||
final text = _contentController.text;
|
||||
final selection = _contentController.selection;
|
||||
final start = selection.start;
|
||||
|
||||
// 找到当前行起始位置
|
||||
int lineStart = start;
|
||||
while (lineStart > 0 && text[lineStart - 1] != '\n') {
|
||||
lineStart--;
|
||||
}
|
||||
|
||||
// 计算当前行的 # 前缀
|
||||
int hashCount = 0;
|
||||
int pos = lineStart;
|
||||
while (pos < text.length && text[pos] == '#') {
|
||||
hashCount++;
|
||||
pos++;
|
||||
}
|
||||
// 跳过 # 后的空格
|
||||
if (pos < text.length && text[pos] == ' ') pos++;
|
||||
|
||||
String newPrefix;
|
||||
int cursorOffset;
|
||||
if (hashCount > 0 && hashCount < 6) {
|
||||
// 升级标题级别
|
||||
hashCount++;
|
||||
newPrefix = '${'#' * hashCount} ';
|
||||
cursorOffset = newPrefix.length;
|
||||
// 替换旧前缀
|
||||
_contentController.value = TextEditingValue(
|
||||
text: text.substring(0, lineStart) + newPrefix + text.substring(pos),
|
||||
selection: TextSelection.collapsed(offset: lineStart + cursorOffset),
|
||||
);
|
||||
} else if (hashCount >= 6) {
|
||||
// 已经是 H6,重置为普通文本
|
||||
_contentController.value = TextEditingValue(
|
||||
text: text.substring(0, lineStart) + text.substring(pos),
|
||||
selection: TextSelection.collapsed(offset: lineStart),
|
||||
);
|
||||
} else {
|
||||
// 没有 # 前缀,添加 H1
|
||||
newPrefix = '# ';
|
||||
_contentController.value = TextEditingValue(
|
||||
text: text.substring(0, lineStart) + newPrefix + text.substring(lineStart),
|
||||
selection: TextSelection.collapsed(offset: lineStart + 2),
|
||||
);
|
||||
final vditor = _vditorKey.currentState;
|
||||
if (vditor != null && vditor.isReady) {
|
||||
vditor.insertValue('# ');
|
||||
}
|
||||
}
|
||||
|
||||
/// 根据 _editorMode 构建内容区域
|
||||
Widget _buildContentArea() {
|
||||
switch (_editorMode) {
|
||||
case 'preview':
|
||||
return _buildPreview();
|
||||
case 'edit':
|
||||
default:
|
||||
return _buildEditor();
|
||||
}
|
||||
}
|
||||
|
||||
/// 编辑器
|
||||
/// 编辑器 — 使用 VditorEditor 替代原来的 TextField
|
||||
Widget _buildEditor() {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
return TextField(
|
||||
controller: _contentController,
|
||||
maxLines: null,
|
||||
expands: true,
|
||||
textAlignVertical: TextAlignVertical.top,
|
||||
strutStyle: const StrutStyle(
|
||||
forceStrutHeight: true,
|
||||
height: 1.6,
|
||||
fontSize: 14,
|
||||
),
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: colors.onSurface,
|
||||
height: 1.6,
|
||||
),
|
||||
decoration: InputDecoration(
|
||||
hintText: '使用 Markdown 格式书写...',
|
||||
hintStyle: TextStyle(
|
||||
fontSize: 14,
|
||||
color: colors.onSurface.withValues(alpha: 0.25),
|
||||
height: 1.6,
|
||||
),
|
||||
border: InputBorder.none,
|
||||
enabledBorder: InputBorder.none,
|
||||
focusedBorder: InputBorder.none,
|
||||
contentPadding: const EdgeInsets.all(16),
|
||||
),
|
||||
return VditorEditor(
|
||||
key: _vditorKey,
|
||||
initialContent: _contentController.text,
|
||||
noteId: _isEditing && widget.note != null ? widget.note!.id : (_tempNoteId ?? ''),
|
||||
isDark: Theme.of(context).brightness == Brightness.dark,
|
||||
surfaceColor: colors.surface,
|
||||
onContentChanged: (value) {
|
||||
_contentController.text = value;
|
||||
_onTextChanged();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// 实时 Markdown 预览(点击回到编辑)
|
||||
Widget _buildPreview() {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
final text = _contentController.text;
|
||||
return GestureDetector(
|
||||
onTap: () => setState(() => _editorMode = 'edit'),
|
||||
behavior: HitTestBehavior.opaque,
|
||||
child: Container(
|
||||
color: colors.surfaceContainerHigh,
|
||||
child: text.isEmpty
|
||||
? Center(
|
||||
child: Text(
|
||||
'预览区域',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: colors.onSurface.withValues(alpha: 0.25),
|
||||
),
|
||||
),
|
||||
)
|
||||
: Markdown(
|
||||
data: text,
|
||||
selectable: true,
|
||||
padding: const EdgeInsets.all(16),
|
||||
styleSheet: MarkdownStyleSheet(
|
||||
h1: TextStyle(
|
||||
fontSize: 22,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: colors.onSurface,
|
||||
height: 1.4,
|
||||
),
|
||||
h2: TextStyle(
|
||||
fontSize: 19,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: colors.onSurface,
|
||||
height: 1.4,
|
||||
),
|
||||
h3: TextStyle(
|
||||
fontSize: 17,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: colors.onSurface,
|
||||
height: 1.4,
|
||||
),
|
||||
p: TextStyle(
|
||||
fontSize: 15,
|
||||
color: colors.onSurface.withValues(alpha: 0.75),
|
||||
height: 1.7,
|
||||
),
|
||||
code: TextStyle(
|
||||
fontSize: 14,
|
||||
color: colors.onSurface,
|
||||
backgroundColor: colors.outlineVariant,
|
||||
),
|
||||
codeblockDecoration: BoxDecoration(
|
||||
color: colors.outlineVariant,
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
),
|
||||
blockquote: TextStyle(
|
||||
fontSize: 15,
|
||||
color: colors.onSurface.withValues(alpha: 0.6),
|
||||
),
|
||||
blockquoteDecoration: BoxDecoration(
|
||||
border: Border(
|
||||
left: BorderSide(color: colors.onSurface.withValues(alpha: 0.25), width: 3),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
/// 标签 chips 行(右侧展示)
|
||||
Widget _buildTagChips() {
|
||||
return Wrap(
|
||||
@@ -631,10 +473,10 @@ class _NoteFormPageState extends State<NoteFormPage> {
|
||||
child: TextField(
|
||||
controller: _titleController,
|
||||
maxLines: 1,
|
||||
style: TextStyle(fontSize: 15, fontWeight: FontWeight.w700, color: colors.onSurface),
|
||||
style: TextStyle(fontSize: 20, fontWeight: FontWeight.w700, color: colors.onSurface),
|
||||
decoration: InputDecoration(
|
||||
hintText: '添加标题',
|
||||
hintStyle: TextStyle(fontSize: 15, fontWeight: FontWeight.w700, color: colors.onSurface.withValues(alpha: 0.2)),
|
||||
hintStyle: TextStyle(fontSize: 20, fontWeight: FontWeight.w700, color: colors.onSurface.withValues(alpha: 0.2)),
|
||||
border: InputBorder.none,
|
||||
enabledBorder: InputBorder.none,
|
||||
focusedBorder: InputBorder.none,
|
||||
@@ -665,7 +507,12 @@ class _NoteFormPageState extends State<NoteFormPage> {
|
||||
|
||||
Future<void> _saveNote() async {
|
||||
_autoSaveTimer?.cancel();
|
||||
final content = _contentController.text.trim();
|
||||
String content;
|
||||
if (_vditorKey.currentState != null && _vditorKey.currentState!.isReady) {
|
||||
content = (await _vditorKey.currentState!.getValue()).trim();
|
||||
} else {
|
||||
content = _contentController.text.trim();
|
||||
}
|
||||
final title = _titleController.text.trim();
|
||||
|
||||
if (title.isEmpty && content.isEmpty) {
|
||||
@@ -679,7 +526,7 @@ class _NoteFormPageState extends State<NoteFormPage> {
|
||||
if (_isEditing && widget.note != null) {
|
||||
// 更新现有笔记(编辑已有笔记)
|
||||
final updatedNote = widget.note!.copyWith(
|
||||
title: _titleController.text.trim(),
|
||||
title: title,
|
||||
content: content,
|
||||
tags: _tags,
|
||||
images: _images,
|
||||
@@ -689,7 +536,7 @@ class _NoteFormPageState extends State<NoteFormPage> {
|
||||
} else if (_savedNote != null) {
|
||||
// 自动保存过的新笔记,更新它
|
||||
final updatedNote = _savedNote!.copyWith(
|
||||
title: _titleController.text.trim(),
|
||||
title: title,
|
||||
content: content,
|
||||
tags: _tags,
|
||||
images: _images,
|
||||
@@ -709,8 +556,6 @@ class _NoteFormPageState extends State<NoteFormPage> {
|
||||
finalImages = await _moveImagesToNewId(oldNoteId, newNoteId);
|
||||
}
|
||||
|
||||
final title = _titleController.text.trim();
|
||||
|
||||
final newNote = Note(
|
||||
id: noteId,
|
||||
title: title,
|
||||
|
||||
@@ -335,110 +335,105 @@ class _BookDetailPageState extends State<BookDetailPage> {
|
||||
final pages = m['pagination'];
|
||||
final coverUrl = cover.toString().isNotEmpty ? _resolveCoverUrl(cover.toString()) : '';
|
||||
|
||||
return NestedScrollView(
|
||||
headerSliverBuilder: (context, _) => [
|
||||
SliverToBoxAdapter(
|
||||
child: Container(
|
||||
color: colors.surface,
|
||||
child: SafeArea(
|
||||
bottom: false,
|
||||
child: Column(children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
child: Row(children: [
|
||||
GestureDetector(
|
||||
onTap: () => Navigator.pop(context),
|
||||
child: Container(
|
||||
width: 36,
|
||||
height: 36,
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHigh,
|
||||
shape: BoxShape.circle),
|
||||
child: Icon(Icons.arrow_back, size: 20, color: colors.onSurface)),
|
||||
return Scaffold(
|
||||
backgroundColor: colors.surface,
|
||||
body: Column(children: [
|
||||
// 头部:返回按钮 + 封面信息
|
||||
SafeArea(
|
||||
bottom: false,
|
||||
child: Column(children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
child: Row(children: [
|
||||
GestureDetector(
|
||||
onTap: () => Navigator.pop(context),
|
||||
child: Container(
|
||||
width: 36,
|
||||
height: 36,
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHigh,
|
||||
shape: BoxShape.circle),
|
||||
child: Icon(Icons.arrow_back, size: 20, color: colors.onSurface)),
|
||||
),
|
||||
]),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 4, 16, 16),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
child: SizedBox(
|
||||
width: 110,
|
||||
height: 160,
|
||||
child: coverUrl.isNotEmpty
|
||||
? Image.network(coverUrl,
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, __, ___) => _coverPlaceholder(colors))
|
||||
: _coverPlaceholder(colors),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(title, style: TextStyle(fontSize: 18, fontWeight: FontWeight.w700, color: colors.onSurface)),
|
||||
if (author.toString().isNotEmpty) ...[
|
||||
const SizedBox(height: 8),
|
||||
Text(author, style: TextStyle(fontSize: 13, color: colors.onSurface.withValues(alpha: 0.6))),
|
||||
],
|
||||
if (press.toString().isNotEmpty) ...[
|
||||
const SizedBox(height: 6),
|
||||
Text(press, style: TextStyle(fontSize: 12, color: colors.onSurface.withValues(alpha: 0.45))),
|
||||
],
|
||||
if (year.toString().isNotEmpty) ...[
|
||||
const SizedBox(height: 6),
|
||||
Text('出版年份:$year', style: TextStyle(fontSize: 12, color: colors.onSurface.withValues(alpha: 0.4))),
|
||||
],
|
||||
if (isbn.toString().isNotEmpty) ...[
|
||||
const SizedBox(height: 4),
|
||||
Text('ISBN:$isbn', style: TextStyle(fontSize: 12, color: colors.onSurface.withValues(alpha: 0.4))),
|
||||
],
|
||||
if (pages != null && pages != 0) ...[
|
||||
const SizedBox(height: 4),
|
||||
Text('页数:$pages', style: TextStyle(fontSize: 12, color: colors.onSurface.withValues(alpha: 0.4))),
|
||||
],
|
||||
if (_localBook != null) ...[
|
||||
const SizedBox(height: 10),
|
||||
_buildLocalStatus(colors),
|
||||
],
|
||||
]),
|
||||
),
|
||||
]),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 4, 16, 16),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
child: SizedBox(
|
||||
width: 110,
|
||||
height: 160,
|
||||
child: coverUrl.isNotEmpty
|
||||
? Image.network(coverUrl,
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, __, ___) => _coverPlaceholder(colors))
|
||||
: _coverPlaceholder(colors),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(title, style: TextStyle(fontSize: 18, fontWeight: FontWeight.w700, color: colors.onSurface)),
|
||||
if (author.toString().isNotEmpty) ...[
|
||||
const SizedBox(height: 8),
|
||||
Text(author, style: TextStyle(fontSize: 13, color: colors.onSurface.withValues(alpha: 0.6))),
|
||||
],
|
||||
if (press.toString().isNotEmpty) ...[
|
||||
const SizedBox(height: 6),
|
||||
Text(press, style: TextStyle(fontSize: 12, color: colors.onSurface.withValues(alpha: 0.45))),
|
||||
],
|
||||
if (year.toString().isNotEmpty) ...[
|
||||
const SizedBox(height: 6),
|
||||
Text('出版年份:$year', style: TextStyle(fontSize: 12, color: colors.onSurface.withValues(alpha: 0.4))),
|
||||
],
|
||||
if (isbn.toString().isNotEmpty) ...[
|
||||
const SizedBox(height: 4),
|
||||
Text('ISBN:$isbn', style: TextStyle(fontSize: 12, color: colors.onSurface.withValues(alpha: 0.4))),
|
||||
],
|
||||
if (pages != null && pages != 0) ...[
|
||||
const SizedBox(height: 4),
|
||||
Text('页数:$pages', style: TextStyle(fontSize: 12, color: colors.onSurface.withValues(alpha: 0.4))),
|
||||
],
|
||||
if (_localBook != null) ...[
|
||||
const SizedBox(height: 10),
|
||||
_buildLocalStatus(colors),
|
||||
],
|
||||
]),
|
||||
),
|
||||
]),
|
||||
),
|
||||
]),
|
||||
),
|
||||
),
|
||||
]),
|
||||
),
|
||||
// Tab 栏:吸顶
|
||||
SliverPersistentHeader(
|
||||
pinned: true,
|
||||
delegate: _StickyTabBarDelegate(
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surface,
|
||||
border: Border(
|
||||
bottom: BorderSide(color: colors.outlineVariant, width: 0.5))),
|
||||
child: Row(children: [
|
||||
_buildTabButton('基础信息', 0),
|
||||
_buildTabButton('国图信息', 1),
|
||||
_buildTabButton('网购地址', 2),
|
||||
_buildTabButton('书籍目录', 3),
|
||||
]),
|
||||
),
|
||||
),
|
||||
// Tab 栏
|
||||
Container(
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surface,
|
||||
border: Border(
|
||||
bottom: BorderSide(color: colors.outlineVariant, width: 0.5))),
|
||||
child: Row(children: [
|
||||
_buildTabButton('基础信息', 0),
|
||||
_buildTabButton('国图信息', 1),
|
||||
_buildTabButton('网购地址', 2),
|
||||
_buildTabButton('书籍目录', 3),
|
||||
]),
|
||||
),
|
||||
],
|
||||
body: _currentTab == 0
|
||||
? _buildBasicInfo(colors)
|
||||
: _currentTab == 1
|
||||
? _buildOpacTab(colors)
|
||||
: _currentTab == 2
|
||||
? _buildOnlineTab(colors)
|
||||
: _buildCatalogTab(colors),
|
||||
// Tab 内容
|
||||
Expanded(
|
||||
child: _currentTab == 0
|
||||
? _buildBasicInfo(colors)
|
||||
: _currentTab == 1
|
||||
? _buildOpacTab(colors)
|
||||
: _currentTab == 2
|
||||
? _buildOnlineTab(colors)
|
||||
: _buildCatalogTab(colors),
|
||||
),
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -758,20 +753,3 @@ class _BookDetailPageState extends State<BookDetailPage> {
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
class _StickyTabBarDelegate extends SliverPersistentHeaderDelegate {
|
||||
final Widget child;
|
||||
_StickyTabBarDelegate({required this.child});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) => child;
|
||||
|
||||
@override
|
||||
double get minExtent => 44;
|
||||
|
||||
@override
|
||||
double get maxExtent => 44;
|
||||
|
||||
@override
|
||||
bool shouldRebuild(_StickyTabBarDelegate oldDelegate) => child != oldDelegate.child;
|
||||
}
|
||||
|
||||
@@ -329,137 +329,130 @@ class _MovieDetailPageState extends State<MovieDetailPage> {
|
||||
final typeParts =
|
||||
[typeName, classStr].where((s) => s.toString().isNotEmpty).join(' / ');
|
||||
|
||||
return NestedScrollView(
|
||||
headerSliverBuilder: (context, _) => [
|
||||
SliverToBoxAdapter(
|
||||
child: Container(
|
||||
color: colors.surface,
|
||||
child: SafeArea(
|
||||
bottom: false,
|
||||
child: Column(children: [
|
||||
// AppBar
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
child: Row(children: [
|
||||
GestureDetector(
|
||||
onTap: () => Navigator.pop(context),
|
||||
child: Container(
|
||||
width: 36,
|
||||
height: 36,
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHigh,
|
||||
shape: BoxShape.circle),
|
||||
child: Icon(Icons.arrow_back,
|
||||
size: 20, color: colors.onSurface)),
|
||||
return Scaffold(
|
||||
backgroundColor: colors.surface,
|
||||
body: Column(children: [
|
||||
// 头部:返回按钮 + 海报信息
|
||||
SafeArea(
|
||||
bottom: false,
|
||||
child: Column(children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
child: Row(children: [
|
||||
GestureDetector(
|
||||
onTap: () => Navigator.pop(context),
|
||||
child: Container(
|
||||
width: 36,
|
||||
height: 36,
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHigh,
|
||||
shape: BoxShape.circle),
|
||||
child: Icon(Icons.arrow_back,
|
||||
size: 20, color: colors.onSurface)),
|
||||
),
|
||||
const Spacer(),
|
||||
GestureDetector(
|
||||
onTap: () => setState(() => _detailStyle = _detailStyle == 0 ? 1 : 0),
|
||||
child: Container(
|
||||
width: 36,
|
||||
height: 36,
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHigh,
|
||||
shape: BoxShape.circle),
|
||||
child: Icon(
|
||||
_detailStyle == 0
|
||||
? Icons.crop_landscape_rounded
|
||||
: Icons.grid_view_rounded,
|
||||
size: 18,
|
||||
color: colors.onSurface)),
|
||||
),
|
||||
]),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 4, 16, 16),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
child: SizedBox(
|
||||
width: 120,
|
||||
height: 170,
|
||||
child: pic.toString().isNotEmpty
|
||||
? Image.network(pic,
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, __, ___) =>
|
||||
_posterPlaceholder(colors))
|
||||
: _posterPlaceholder(colors),
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
GestureDetector(
|
||||
onTap: () => setState(() => _detailStyle = _detailStyle == 0 ? 1 : 0),
|
||||
child: Container(
|
||||
width: 36,
|
||||
height: 36,
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHigh,
|
||||
shape: BoxShape.circle),
|
||||
child: Icon(
|
||||
_detailStyle == 0
|
||||
? Icons.crop_landscape_rounded
|
||||
: Icons.grid_view_rounded,
|
||||
size: 18,
|
||||
color: colors.onSurface)),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(name,
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.w700,
|
||||
color: colors.onSurface)),
|
||||
const SizedBox(height: 8),
|
||||
if (score.toString().isNotEmpty && score != '0.0') ...[
|
||||
Row(children: [
|
||||
Icon(Icons.star_rounded, size: 16, color: const Color(0xFFF59E0B)),
|
||||
const SizedBox(width: 3),
|
||||
Text('$score', style: TextStyle(fontSize: 15, fontWeight: FontWeight.w600, color: colors.onSurface)),
|
||||
Text(' /10', style: TextStyle(fontSize: 11, color: colors.onSurface.withValues(alpha: 0.3))),
|
||||
]),
|
||||
const SizedBox(height: 2),
|
||||
Text('评分来源于网络资源收集,并非官方评分', style: TextStyle(fontSize: 10, color: colors.onSurface.withValues(alpha: 0.25))),
|
||||
const SizedBox(height: 8),
|
||||
],
|
||||
_endTag(isEnd),
|
||||
if (metaParts.isNotEmpty) ...[
|
||||
const SizedBox(height: 8),
|
||||
Text(metaParts,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: colors.onSurface
|
||||
.withValues(alpha: 0.5))),
|
||||
],
|
||||
if (typeParts.isNotEmpty) ...[
|
||||
const SizedBox(height: 3),
|
||||
Text(typeParts,
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: colors.onSurface
|
||||
.withValues(alpha: 0.4)),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis),
|
||||
],
|
||||
if (_localMovie != null) ...[
|
||||
const SizedBox(height: 10),
|
||||
_buildLocalStatus(colors),
|
||||
],
|
||||
]),
|
||||
),
|
||||
]),
|
||||
),
|
||||
// 海报 + 信息
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 4, 16, 16),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
child: SizedBox(
|
||||
width: 120,
|
||||
height: 170,
|
||||
child: pic.toString().isNotEmpty
|
||||
? Image.network(pic,
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, __, ___) =>
|
||||
_posterPlaceholder(colors))
|
||||
: _posterPlaceholder(colors),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(name,
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.w700,
|
||||
color: colors.onSurface)),
|
||||
const SizedBox(height: 8),
|
||||
if (score.toString().isNotEmpty && score != '0.0') ...[
|
||||
Row(children: [
|
||||
Icon(Icons.star_rounded, size: 16, color: const Color(0xFFF59E0B)),
|
||||
const SizedBox(width: 3),
|
||||
Text('$score', style: TextStyle(fontSize: 15, fontWeight: FontWeight.w600, color: colors.onSurface)),
|
||||
Text(' /10', style: TextStyle(fontSize: 11, color: colors.onSurface.withValues(alpha: 0.3))),
|
||||
]),
|
||||
const SizedBox(height: 2),
|
||||
Text('评分来源于网络资源收集,并非官方评分', style: TextStyle(fontSize: 10, color: colors.onSurface.withValues(alpha: 0.25))),
|
||||
const SizedBox(height: 8),
|
||||
],
|
||||
_endTag(isEnd),
|
||||
if (metaParts.isNotEmpty) ...[
|
||||
const SizedBox(height: 8),
|
||||
Text(metaParts,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: colors.onSurface
|
||||
.withValues(alpha: 0.5))),
|
||||
],
|
||||
if (typeParts.isNotEmpty) ...[
|
||||
const SizedBox(height: 3),
|
||||
Text(typeParts,
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: colors.onSurface
|
||||
.withValues(alpha: 0.4)),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis),
|
||||
],
|
||||
if (_localMovie != null) ...[
|
||||
const SizedBox(height: 10),
|
||||
_buildLocalStatus(colors),
|
||||
],
|
||||
]),
|
||||
),
|
||||
]),
|
||||
),
|
||||
]),
|
||||
),
|
||||
),
|
||||
]),
|
||||
),
|
||||
// Tab 栏:吸顶
|
||||
SliverPersistentHeader(
|
||||
pinned: true,
|
||||
delegate: _StickyTabBarDelegate(
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surface,
|
||||
border: Border(
|
||||
bottom: BorderSide(color: colors.outlineVariant, width: 0.5))),
|
||||
child: Row(children: [
|
||||
_buildTabButton('概要', 0),
|
||||
_buildTabButton('演职人员', 1),
|
||||
]),
|
||||
),
|
||||
),
|
||||
// Tab 栏
|
||||
Container(
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surface,
|
||||
border: Border(
|
||||
bottom: BorderSide(color: colors.outlineVariant, width: 0.5))),
|
||||
child: Row(children: [
|
||||
_buildTabButton('概要', 0),
|
||||
_buildTabButton('演职人员', 1),
|
||||
]),
|
||||
),
|
||||
],
|
||||
body: _currentTab == 0 ? _buildOverview(colors) : _buildStaffTab(colors),
|
||||
// Tab 内容
|
||||
Expanded(
|
||||
child: _currentTab == 0 ? _buildOverview(colors) : _buildStaffTab(colors),
|
||||
),
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -478,114 +471,113 @@ class _MovieDetailPageState extends State<MovieDetailPage> {
|
||||
final metaParts = [year, area].where((s) => s.toString().isNotEmpty).join(' · ');
|
||||
final typeParts = [typeName, classStr].where((s) => s.toString().isNotEmpty).join(' / ');
|
||||
|
||||
return NestedScrollView(
|
||||
headerSliverBuilder: (context, _) => [
|
||||
SliverToBoxAdapter(
|
||||
child: Stack(children: [
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
height: 320,
|
||||
child: pic.toString().isNotEmpty
|
||||
? Image.network(pic, fit: BoxFit.cover,
|
||||
errorBuilder: (_, __, ___) => Container(color: colors.surfaceContainerHighest))
|
||||
: Container(color: colors.surfaceContainerHighest,
|
||||
child: Icon(Icons.movie_outlined, size: 64, color: colors.onSurface.withValues(alpha: 0.1))),
|
||||
),
|
||||
Positioned.fill(
|
||||
child: DecoratedBox(
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: [Colors.transparent, Colors.black.withValues(alpha: 0.8)],
|
||||
stops: const [0.35, 1.0],
|
||||
),
|
||||
return Scaffold(
|
||||
backgroundColor: colors.surface,
|
||||
body: Column(children: [
|
||||
// 沉浸式头部
|
||||
Stack(children: [
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
height: 320,
|
||||
child: pic.toString().isNotEmpty
|
||||
? Image.network(pic, fit: BoxFit.cover,
|
||||
errorBuilder: (_, __, ___) => Container(color: colors.surfaceContainerHighest))
|
||||
: Container(color: colors.surfaceContainerHighest,
|
||||
child: Icon(Icons.movie_outlined, size: 64, color: colors.onSurface.withValues(alpha: 0.1))),
|
||||
),
|
||||
Positioned.fill(
|
||||
child: DecoratedBox(
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: [Colors.transparent, Colors.black.withValues(alpha: 0.8)],
|
||||
stops: const [0.35, 1.0],
|
||||
),
|
||||
),
|
||||
),
|
||||
SafeArea(
|
||||
bottom: false,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
child: Row(children: [
|
||||
GestureDetector(
|
||||
onTap: () => Navigator.pop(context),
|
||||
child: Container(
|
||||
width: 36, height: 36,
|
||||
decoration: BoxDecoration(color: Colors.black.withValues(alpha: 0.3), shape: BoxShape.circle),
|
||||
child: const Icon(Icons.arrow_back, size: 20, color: Colors.white)),
|
||||
),
|
||||
const Spacer(),
|
||||
GestureDetector(
|
||||
onTap: () => setState(() => _detailStyle = 0),
|
||||
child: Container(
|
||||
width: 36, height: 36,
|
||||
decoration: BoxDecoration(color: Colors.black.withValues(alpha: 0.3), shape: BoxShape.circle),
|
||||
child: const Icon(Icons.grid_view_rounded, size: 18, color: Colors.white)),
|
||||
),
|
||||
]),
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
left: 16, right: 16, bottom: 18,
|
||||
child: Column(crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [
|
||||
Text(name, maxLines: 2, overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(fontSize: 22, fontWeight: FontWeight.w700, color: Colors.white)),
|
||||
const SizedBox(height: 8),
|
||||
Row(children: [
|
||||
if (score.toString().isNotEmpty && score != '0.0') ...[
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.black.withValues(alpha: 0.3),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.star_rounded, size: 18, color: Colors.amber.shade400),
|
||||
const SizedBox(width: 3),
|
||||
Text('$score', style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w700, color: Colors.white)),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
],
|
||||
_endTag(isEnd),
|
||||
if (metaParts.isNotEmpty) ...[
|
||||
const SizedBox(width: 8),
|
||||
Text(metaParts, style: TextStyle(fontSize: 12, color: Colors.white.withValues(alpha: 0.7))),
|
||||
],
|
||||
]),
|
||||
if (typeParts.isNotEmpty) ...[
|
||||
const SizedBox(height: 4),
|
||||
Text(typeParts, maxLines: 1, overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(fontSize: 12, color: Colors.white.withValues(alpha: 0.5))),
|
||||
],
|
||||
if (_localMovie != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
_buildLocalStatus(colors),
|
||||
],
|
||||
]),
|
||||
),
|
||||
]),
|
||||
),
|
||||
SliverPersistentHeader(
|
||||
pinned: true,
|
||||
delegate: _StickyTabBarDelegate(
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surface,
|
||||
border: Border(bottom: BorderSide(color: colors.outlineVariant, width: 0.5))),
|
||||
),
|
||||
SafeArea(
|
||||
bottom: false,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
child: Row(children: [
|
||||
_buildTabButton('概要', 0),
|
||||
_buildTabButton('演职人员', 1),
|
||||
GestureDetector(
|
||||
onTap: () => Navigator.pop(context),
|
||||
child: Container(
|
||||
width: 36, height: 36,
|
||||
decoration: BoxDecoration(color: Colors.black.withValues(alpha: 0.3), shape: BoxShape.circle),
|
||||
child: const Icon(Icons.arrow_back, size: 20, color: Colors.white)),
|
||||
),
|
||||
const Spacer(),
|
||||
GestureDetector(
|
||||
onTap: () => setState(() => _detailStyle = 0),
|
||||
child: Container(
|
||||
width: 36, height: 36,
|
||||
decoration: BoxDecoration(color: Colors.black.withValues(alpha: 0.3), shape: BoxShape.circle),
|
||||
child: const Icon(Icons.grid_view_rounded, size: 18, color: Colors.white)),
|
||||
),
|
||||
]),
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
left: 16, right: 16, bottom: 18,
|
||||
child: Column(crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [
|
||||
Text(name, maxLines: 2, overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(fontSize: 22, fontWeight: FontWeight.w700, color: Colors.white)),
|
||||
const SizedBox(height: 8),
|
||||
Row(children: [
|
||||
if (score.toString().isNotEmpty && score != '0.0') ...[
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.black.withValues(alpha: 0.3),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.star_rounded, size: 18, color: Colors.amber.shade400),
|
||||
const SizedBox(width: 3),
|
||||
Text('$score', style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w700, color: Colors.white)),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
],
|
||||
_endTag(isEnd),
|
||||
if (metaParts.isNotEmpty) ...[
|
||||
const SizedBox(width: 8),
|
||||
Text(metaParts, style: TextStyle(fontSize: 12, color: Colors.white.withValues(alpha: 0.7))),
|
||||
],
|
||||
]),
|
||||
if (typeParts.isNotEmpty) ...[
|
||||
const SizedBox(height: 4),
|
||||
Text(typeParts, maxLines: 1, overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(fontSize: 12, color: Colors.white.withValues(alpha: 0.5))),
|
||||
],
|
||||
if (_localMovie != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
_buildLocalStatus(colors),
|
||||
],
|
||||
]),
|
||||
),
|
||||
]),
|
||||
// Tab 栏
|
||||
Container(
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surface,
|
||||
border: Border(bottom: BorderSide(color: colors.outlineVariant, width: 0.5))),
|
||||
child: Row(children: [
|
||||
_buildTabButton('概要', 0),
|
||||
_buildTabButton('演职人员', 1),
|
||||
]),
|
||||
),
|
||||
],
|
||||
body: _currentTab == 0 ? _buildOverview(colors) : _buildStaffTab(colors),
|
||||
// Tab 内容
|
||||
Expanded(
|
||||
child: _currentTab == 0 ? _buildOverview(colors) : _buildStaffTab(colors),
|
||||
),
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -907,20 +899,3 @@ class _MovieDetailPageState extends State<MovieDetailPage> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _StickyTabBarDelegate extends SliverPersistentHeaderDelegate {
|
||||
final Widget child;
|
||||
_StickyTabBarDelegate({required this.child});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) => child;
|
||||
|
||||
@override
|
||||
double get minExtent => 44;
|
||||
|
||||
@override
|
||||
double get maxExtent => 44;
|
||||
|
||||
@override
|
||||
bool shouldRebuild(_StickyTabBarDelegate oldDelegate) => child != oldDelegate.child;
|
||||
}
|
||||
|
||||
@@ -235,13 +235,13 @@ class _SearchPageState extends State<SearchPage> {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
|
||||
child: Row(children: [
|
||||
_filterChip('影视', Icons.movie_outlined, _showMovies, movieCount, () { setState(() { _showMovies = !_showMovies; _performSearch(); }); }),
|
||||
Expanded(child: _filterChip('影视', Icons.movie_outlined, _showMovies, movieCount, () { setState(() { _showMovies = !_showMovies; _performSearch(); }); })),
|
||||
const SizedBox(width: 8),
|
||||
_filterChip('书籍', Icons.menu_book_outlined, _showBooks, bookCount, () { setState(() { _showBooks = !_showBooks; _performSearch(); }); }),
|
||||
Expanded(child: _filterChip('书籍', Icons.menu_book_outlined, _showBooks, bookCount, () { setState(() { _showBooks = !_showBooks; _performSearch(); }); })),
|
||||
const SizedBox(width: 8),
|
||||
_filterChip('笔记', Icons.note_outlined, _showNotes, noteCount, () { setState(() { _showNotes = !_showNotes; _performSearch(); }); }),
|
||||
Expanded(child: _filterChip('笔记', Icons.note_outlined, _showNotes, noteCount, () { setState(() { _showNotes = !_showNotes; _performSearch(); }); })),
|
||||
const SizedBox(width: 8),
|
||||
_filterChip('游戏', Icons.sports_esports_outlined, _showGames, gameCount, () { setState(() { _showGames = !_showGames; _performSearch(); }); }),
|
||||
Expanded(child: _filterChip('游戏', Icons.sports_esports_outlined, _showGames, gameCount, () { setState(() { _showGames = !_showGames; _performSearch(); }); })),
|
||||
]),
|
||||
);
|
||||
}
|
||||
@@ -253,16 +253,16 @@ class _SearchPageState extends State<SearchPage> {
|
||||
onTap: onTap,
|
||||
child: AnimatedContainer(
|
||||
duration: const Duration(milliseconds: 200),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 7),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 7),
|
||||
decoration: BoxDecoration(
|
||||
color: selected ? colors.primary : colors.surfaceContainerHighest,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Row(mainAxisSize: MainAxisSize.min, children: [
|
||||
Icon(icon, size: 14, color: selected ? colors.onPrimary : colors.onSurface.withValues(alpha: 0.4)),
|
||||
const SizedBox(width: 5),
|
||||
Text(label, style: TextStyle(fontSize: 13, fontWeight: FontWeight.w500, color: selected ? colors.onPrimary : colors.onSurface.withValues(alpha: 0.4))),
|
||||
if (showCount) ...[const SizedBox(width: 4), Text('$count', style: TextStyle(fontSize: 11, fontWeight: FontWeight.w600, color: selected ? colors.onPrimary.withValues(alpha: 0.7) : colors.onSurface.withValues(alpha: 0.25)))],
|
||||
const SizedBox(width: 4),
|
||||
Flexible(child: Text(label, style: TextStyle(fontSize: 13, fontWeight: FontWeight.w500, color: selected ? colors.onPrimary : colors.onSurface.withValues(alpha: 0.4)), overflow: TextOverflow.ellipsis)),
|
||||
if (showCount) ...[const SizedBox(width: 3), Text('$count', style: TextStyle(fontSize: 11, fontWeight: FontWeight.w600, color: selected ? colors.onPrimary.withValues(alpha: 0.7) : colors.onSurface.withValues(alpha: 0.25)))],
|
||||
]),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../providers/app_provider.dart';
|
||||
import '../models/data_models.dart';
|
||||
import 'fade_in_local_image.dart';
|
||||
|
||||
/// 笔记列表项组件 - 卡片式设计,内容展示在卡片内
|
||||
class NoteListItem extends StatelessWidget {
|
||||
@@ -98,6 +99,54 @@ class _NoteListItemContent extends StatelessWidget {
|
||||
),
|
||||
],
|
||||
|
||||
// 图片预览
|
||||
if (note.images.isNotEmpty) ...[
|
||||
const SizedBox(height: 8),
|
||||
SizedBox(
|
||||
height: 48,
|
||||
child: ListView.separated(
|
||||
scrollDirection: Axis.horizontal,
|
||||
itemCount: note.images.length > 4 ? 4 : note.images.length,
|
||||
separatorBuilder: (_, __) => const SizedBox(width: 6),
|
||||
itemBuilder: (context, index) {
|
||||
if (index == 3 && note.images.length > 4) {
|
||||
return Container(
|
||||
width: 48,
|
||||
height: 48,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
color: colors.surfaceContainerHighest,
|
||||
),
|
||||
child: Center(
|
||||
child: Text(
|
||||
'+${note.images.length - 3}',
|
||||
style: TextStyle(fontSize: 12, color: colors.onSurface.withValues(alpha: 0.5)),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
return Container(
|
||||
width: 48,
|
||||
height: 48,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
border: Border.all(color: colors.outlineVariant, width: 0.5),
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: FadeInLocalImage(
|
||||
path: note.images[index],
|
||||
fit: BoxFit.cover,
|
||||
errorWidget: Container(
|
||||
color: colors.surfaceContainerHighest,
|
||||
child: Icon(Icons.broken_image_outlined, size: 16, color: colors.onSurface.withValues(alpha: 0.25)),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
// 标签
|
||||
if (note.tags.isNotEmpty) ...[
|
||||
const SizedBox(height: 8),
|
||||
|
||||
@@ -14,6 +14,7 @@ class VditorEditor extends StatefulWidget {
|
||||
final bool isDark;
|
||||
final Color surfaceColor;
|
||||
final ValueChanged<String>? onContentChanged;
|
||||
final ValueChanged<double>? onHeightChanged;
|
||||
final String placeholder;
|
||||
|
||||
const VditorEditor({
|
||||
@@ -23,6 +24,7 @@ class VditorEditor extends StatefulWidget {
|
||||
this.isDark = false,
|
||||
this.surfaceColor = Colors.white,
|
||||
this.onContentChanged,
|
||||
this.onHeightChanged,
|
||||
this.placeholder = '使用 Markdown 格式书写...',
|
||||
});
|
||||
|
||||
@@ -34,8 +36,9 @@ class VditorEditorState extends State<VditorEditor> {
|
||||
InAppWebViewController? _controller;
|
||||
bool _isReady = false;
|
||||
bool _loadFailed = false;
|
||||
bool _assetsReady = false;
|
||||
String? _distDir;
|
||||
String? _distDir; // Windows: 文件系统路径
|
||||
double _contentHeight = 200; // WebView 内容高度,随内容撑开
|
||||
double _lastKeyboardH = 0; // 上次键盘高度,用于检测键盘弹出
|
||||
final Completer<void> _readyCompleter = Completer<void>();
|
||||
Timer? _fallbackTimer;
|
||||
|
||||
@@ -46,7 +49,12 @@ class VditorEditorState extends State<VditorEditor> {
|
||||
void initState() {
|
||||
super.initState();
|
||||
_startFallbackTimer();
|
||||
_locateDistDir();
|
||||
if (Platform.isWindows) {
|
||||
_locateDistDir();
|
||||
} else {
|
||||
// Android/iOS: 直接从 asset 加载,无需定位文件系统路径
|
||||
if (mounted) setState(() {});
|
||||
}
|
||||
}
|
||||
|
||||
void _startFallbackTimer() {
|
||||
@@ -60,13 +68,12 @@ class VditorEditorState extends State<VditorEditor> {
|
||||
/// 定位 vditor_dist 目录(Windows 构建时由 CMakeLists 复制到 data/ 下)
|
||||
Future<void> _locateDistDir() async {
|
||||
try {
|
||||
// Windows: exe 同级 data/vditor_dist/
|
||||
final exePath = Platform.resolvedExecutable;
|
||||
final exeDir = p.dirname(exePath);
|
||||
final candidate = p.join(exeDir, 'data', 'vditor_dist');
|
||||
if (await File(p.join(candidate, 'vditor_editor.html')).exists()) {
|
||||
_distDir = candidate;
|
||||
if (mounted) setState(() => _assetsReady = true);
|
||||
if (mounted) setState(() {});
|
||||
return;
|
||||
}
|
||||
debugPrint('[VditorEditor] vditor_dist not found at $candidate');
|
||||
@@ -112,7 +119,7 @@ class VditorEditorState extends State<VditorEditor> {
|
||||
|
||||
Future<void> setTheme(bool isDark) async {
|
||||
if (_controller == null || !_isReady) return;
|
||||
final theme = isDark ? 'dark' : 'classic';
|
||||
final theme = isDark ? 'dark' : 'light';
|
||||
try {
|
||||
await _controller!.evaluateJavascript(source: 'setTheme("$theme")');
|
||||
} catch (_) {}
|
||||
@@ -134,16 +141,23 @@ class VditorEditorState extends State<VditorEditor> {
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
Future<void> _scrollToCursor() async {
|
||||
if (_controller == null || !_isReady) return;
|
||||
try {
|
||||
// 延迟一帧让键盘动画完成
|
||||
await Future.delayed(const Duration(milliseconds: 300));
|
||||
await _controller!.evaluateJavascript(source: 'scrollToCursor()');
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
void _onVditorReady() {
|
||||
if (_isReady) return;
|
||||
_fallbackTimer?.cancel();
|
||||
_isReady = true;
|
||||
if (!_readyCompleter.isCompleted) _readyCompleter.complete();
|
||||
// 设置初始内容
|
||||
if (widget.initialContent != null && widget.initialContent!.isNotEmpty) {
|
||||
setValue(widget.initialContent!);
|
||||
}
|
||||
// 设置背景色
|
||||
setBgColor(_colorToHex(widget.surfaceColor));
|
||||
}
|
||||
|
||||
@@ -202,83 +216,139 @@ class VditorEditorState extends State<VditorEditor> {
|
||||
Widget build(BuildContext context) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
|
||||
if (_loadFailed || !_assetsReady) {
|
||||
if (_loadFailed) {
|
||||
// 离线降级:使用普通 TextField
|
||||
return TextField(
|
||||
controller: TextEditingController(text: widget.initialContent ?? ''),
|
||||
maxLines: null,
|
||||
expands: true,
|
||||
textAlignVertical: TextAlignVertical.top,
|
||||
strutStyle: const StrutStyle(forceStrutHeight: true, height: 1.6, fontSize: 14),
|
||||
style: TextStyle(fontSize: 14, color: colors.onSurface, height: 1.6),
|
||||
decoration: InputDecoration(
|
||||
hintText: widget.placeholder,
|
||||
hintStyle: TextStyle(fontSize: 14, color: colors.onSurface.withValues(alpha: 0.25), height: 1.6),
|
||||
border: InputBorder.none,
|
||||
enabledBorder: InputBorder.none,
|
||||
focusedBorder: InputBorder.none,
|
||||
contentPadding: const EdgeInsets.all(16),
|
||||
),
|
||||
onChanged: widget.onContentChanged,
|
||||
);
|
||||
}
|
||||
// 等待 assets 解压
|
||||
if (_loadFailed) {
|
||||
return TextField(
|
||||
controller: TextEditingController(text: widget.initialContent ?? ''),
|
||||
maxLines: null,
|
||||
expands: true,
|
||||
textAlignVertical: TextAlignVertical.top,
|
||||
strutStyle: const StrutStyle(forceStrutHeight: true, height: 1.6, fontSize: 14),
|
||||
style: TextStyle(fontSize: 14, color: colors.onSurface, height: 1.6),
|
||||
decoration: InputDecoration(
|
||||
hintText: widget.placeholder,
|
||||
hintStyle: TextStyle(fontSize: 14, color: colors.onSurface.withValues(alpha: 0.25), height: 1.6),
|
||||
border: InputBorder.none,
|
||||
enabledBorder: InputBorder.none,
|
||||
focusedBorder: InputBorder.none,
|
||||
contentPadding: const EdgeInsets.all(16),
|
||||
),
|
||||
onChanged: widget.onContentChanged,
|
||||
);
|
||||
}
|
||||
|
||||
// Windows: 等待 dist 目录定位完成
|
||||
if (Platform.isWindows && _distDir == null) {
|
||||
return Center(child: CircularProgressIndicator(strokeWidth: 2, color: colors.primary));
|
||||
}
|
||||
|
||||
final htmlPath = p.join(_distDir!, 'vditor_editor.html');
|
||||
final fileUrl = 'file:///${htmlPath.replaceAll('\\', '/')}';
|
||||
debugPrint('[VditorEditor] loading: $fileUrl');
|
||||
final String initialUrl;
|
||||
if (Platform.isWindows) {
|
||||
final htmlPath = p.join(_distDir!, 'vditor_editor.html');
|
||||
initialUrl = 'file:///${htmlPath.replaceAll('\\', '/')}';
|
||||
} else {
|
||||
// Android: 直接访问 APK 内 asset,相对路径自动解析
|
||||
initialUrl = 'file:///android_asset/flutter_assets/assets/vditor/dist/vditor_editor.html';
|
||||
}
|
||||
debugPrint('[VditorEditor] loading: $initialUrl');
|
||||
|
||||
return Container(
|
||||
color: colors.surface,
|
||||
child: InAppWebView(
|
||||
webViewEnvironment: windowsWebViewEnvironment,
|
||||
initialUrlRequest: URLRequest(url: WebUri(fileUrl)),
|
||||
initialSettings: InAppWebViewSettings(
|
||||
javaScriptEnabled: true,
|
||||
transparentBackground: true,
|
||||
disableContextMenu: false,
|
||||
useHybridComposition: true,
|
||||
),
|
||||
onWebViewCreated: (controller) {
|
||||
_controller = controller;
|
||||
controller.addJavaScriptHandler(
|
||||
handlerName: 'onVditorReady',
|
||||
callback: (_) => _onVditorReady(),
|
||||
);
|
||||
controller.addJavaScriptHandler(
|
||||
handlerName: 'onContentChanged',
|
||||
callback: (args) {
|
||||
if (args.isNotEmpty) {
|
||||
widget.onContentChanged?.call(args[0].toString());
|
||||
}
|
||||
},
|
||||
);
|
||||
controller.addJavaScriptHandler(
|
||||
handlerName: 'onPickImage',
|
||||
callback: (_) => _pickImage(),
|
||||
);
|
||||
controller.addJavaScriptHandler(
|
||||
handlerName: 'onImageUpload',
|
||||
callback: (args) {
|
||||
if (args.length >= 3) {
|
||||
_handleImageUpload(args[0].toString(), args[1].toString(), args[2].toString());
|
||||
}
|
||||
},
|
||||
);
|
||||
},
|
||||
onLoadStop: (controller, url) async {
|
||||
final theme = widget.isDark ? 'dark' : 'classic';
|
||||
final escapedPlaceholder = jsonEncode(widget.placeholder);
|
||||
await controller.evaluateJavascript(
|
||||
source: 'initVditor("$theme", $escapedPlaceholder)',
|
||||
);
|
||||
},
|
||||
onReceivedError: (controller, request, error) {
|
||||
debugPrint('[VditorEditor] load error: ${error.description}');
|
||||
},
|
||||
// 键盘弹出时,滚动到光标位置
|
||||
final keyboardH = MediaQuery.of(context).viewInsets.bottom;
|
||||
if (keyboardH > 0 && _lastKeyboardH == 0 && _isReady) {
|
||||
Future.microtask(() => _scrollToCursor());
|
||||
}
|
||||
_lastKeyboardH = keyboardH;
|
||||
|
||||
return SizedBox(
|
||||
height: _contentHeight,
|
||||
child: Stack(
|
||||
children: [
|
||||
Container(
|
||||
color: colors.surface,
|
||||
child: InAppWebView(
|
||||
webViewEnvironment: Platform.isWindows ? windowsWebViewEnvironment : null,
|
||||
initialUrlRequest: URLRequest(url: WebUri(initialUrl)),
|
||||
initialSettings: InAppWebViewSettings(
|
||||
javaScriptEnabled: true,
|
||||
transparentBackground: true,
|
||||
disableContextMenu: false,
|
||||
useHybridComposition: true,
|
||||
allowFileAccessFromFileURLs: true,
|
||||
allowUniversalAccessFromFileURLs: true,
|
||||
),
|
||||
onWebViewCreated: (controller) {
|
||||
_controller = controller;
|
||||
controller.addJavaScriptHandler(
|
||||
handlerName: 'onVditorReady',
|
||||
callback: (_) => _onVditorReady(),
|
||||
);
|
||||
controller.addJavaScriptHandler(
|
||||
handlerName: 'onContentChanged',
|
||||
callback: (args) {
|
||||
if (args.isNotEmpty) {
|
||||
widget.onContentChanged?.call(args[0].toString());
|
||||
}
|
||||
},
|
||||
);
|
||||
controller.addJavaScriptHandler(
|
||||
handlerName: 'onPickImage',
|
||||
callback: (_) => _pickImage(),
|
||||
);
|
||||
controller.addJavaScriptHandler(
|
||||
handlerName: 'onImageUpload',
|
||||
callback: (args) {
|
||||
if (args.length >= 3) {
|
||||
_handleImageUpload(args[0].toString(), args[1].toString(), args[2].toString());
|
||||
}
|
||||
},
|
||||
);
|
||||
controller.addJavaScriptHandler(
|
||||
handlerName: 'onHeightChanged',
|
||||
callback: (args) {
|
||||
if (args.isNotEmpty) {
|
||||
final h = double.tryParse(args[0].toString()) ?? _contentHeight;
|
||||
if ((h - _contentHeight).abs() > 2 && h > 0) {
|
||||
setState(() => _contentHeight = h);
|
||||
widget.onHeightChanged?.call(h);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
},
|
||||
onLoadStop: (controller, url) async {
|
||||
final theme = widget.isDark ? 'dark' : 'light';
|
||||
final escapedPlaceholder = jsonEncode(widget.placeholder);
|
||||
await controller.evaluateJavascript(
|
||||
source: 'initVditor("$theme", $escapedPlaceholder)',
|
||||
);
|
||||
},
|
||||
onReceivedError: (controller, request, error) {
|
||||
debugPrint('[VditorEditor] load error: ${error.description}');
|
||||
},
|
||||
),
|
||||
),
|
||||
// 加载动画:Vditor 就绪前显示
|
||||
if (!_isReady)
|
||||
Container(
|
||||
color: colors.surface,
|
||||
child: Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 24,
|
||||
height: 24,
|
||||
child: CircularProgressIndicator(strokeWidth: 2, color: colors.primary),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
Text(
|
||||
'编辑器加载中...',
|
||||
style: TextStyle(fontSize: 13, color: colors.onSurface.withValues(alpha: 0.4)),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user