From b72009718f026664282844ec39ac64ff03209741 Mon Sep 17 00:00:00 2001 From: iven Date: Thu, 4 Jun 2026 00:13:51 +0800 Subject: [PATCH] =?UTF-8?q?fix(app):=20=E6=97=A5=E8=AE=B0=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E9=80=BB=E8=BE=91=E4=BF=AE=E5=A4=8D=20=E2=80=94=20Edi?= =?UTF-8?q?torPage=20=E6=94=B9=E4=B8=BA=20StatefulWidget=20+=20=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=90=88=E5=B9=B6=E7=BC=96=E8=BE=91=E5=99=A8=E7=8A=B6?= =?UTF-8?q?=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因分析: 1. EditorPage 是 StatelessWidget,savedJournalId 作为 build() 局部变量 每次重建都重置为 null,导致每次自动保存都走新建而非更新分支 2. 更新分支直接 repo.updateJournal(existing),没有把编辑器当前 状态(标题/心情/标签)合并到已有日记中 修复: - EditorPage 改为 StatefulWidget,_savedJournalId 存储在 State 中 - 更新分支用 existing.copyWith() 合并编辑器当前状态后保存 --- .../features/editor/views/editor_page.dart | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/app/lib/features/editor/views/editor_page.dart b/app/lib/features/editor/views/editor_page.dart index ed0e205..7c0970c 100644 --- a/app/lib/features/editor/views/editor_page.dart +++ b/app/lib/features/editor/views/editor_page.dart @@ -37,12 +37,26 @@ import '../widgets/brush_panel.dart'; import '../widgets/dot_grid_painter.dart'; /// 手账编辑器页面 -class EditorPage extends StatelessWidget { +class EditorPage extends StatefulWidget { final String? journalId; final String? templateId; const EditorPage({super.key, this.journalId, this.templateId}); + @override + State createState() => _EditorPageState(); +} + +class _EditorPageState extends State { + /// 跟踪已保存的日记 ID — 新建日记首次保存后赋值 + String? _savedJournalId; + + @override + void initState() { + super.initState(); + _savedJournalId = widget.journalId; + } + @override Widget build(BuildContext context) { // 从 Provider 树获取 JournalRepository(IsarJournalRepository) @@ -50,10 +64,6 @@ class EditorPage extends StatelessWidget { // 从 Provider 树获取 SyncEngine(同步到后端) final syncEngine = context.read(); - // 可变闭包变量:跟踪已保存的日记 ID - // 新建日记首次保存后赋值,后续自动更新使用此 ID - String? savedJournalId = journalId; - return BlocProvider( create: (_) => EditorBloc( onSave: (state) async { @@ -66,7 +76,7 @@ class EditorPage extends StatelessWidget { } await _persistState( - repo, state, (id) => savedJournalId = id, savedJournalId, + repo, state, (id) => _savedJournalId = id, _savedJournalId, syncEngine: syncEngine, authorId: authorId, ); @@ -76,12 +86,12 @@ class EditorPage extends StatelessWidget { }, ), child: _EditorView( - journalId: journalId, - templateId: templateId, - savedJournalId: savedJournalId, + journalId: widget.journalId, + templateId: widget.templateId, + savedJournalId: _savedJournalId, repo: repo, onSaveComplete: () { - _showShareSheetAndNavigate(context, repo, savedJournalId); + _showShareSheetAndNavigate(context, repo, _savedJournalId); }, ), ); @@ -137,7 +147,14 @@ class EditorPage extends StatelessWidget { // --- 更新已有日记 --- final existing = await repo.getJournal(savedJournalId); if (existing != null) { - await repo.updateJournal(existing); + // 将编辑器当前状态合并到已有日记中 + final updated = existing.copyWith( + title: state.title.isNotEmpty ? state.title : existing.title, + mood: state.selectedMood, + tags: state.tags.isNotEmpty ? state.tags : existing.tags, + updatedAt: now, + ); + await repo.updateJournal(updated); // 入队 SyncEngine 等待同步到后端 syncEngine.enqueue(PendingOperation(