diff --git a/assets/vditor/dist/vditor_editor.html b/assets/vditor/dist/vditor_editor.html index 42c0e55..8407cd4 100644 --- a/assets/vditor/dist/vditor_editor.html +++ b/assets/vditor/dist/vditor_editor.html @@ -148,6 +148,10 @@ cursor.scrollIntoView({ block: 'center', behavior: 'smooth' }); } } + + function requestHeightUpdate() { + requestAnimationFrame(notifyHeight); + } diff --git a/lib/main.dart b/lib/main.dart index fb710bb..e6e3263 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -318,7 +318,17 @@ class _MyAppState extends State with WidgetsBindingObserver { theme: light, darkTheme: dark, themeMode: provider.themeMode, - builder: (ctx, nav) => AppShell(child: nav!), + builder: (ctx, nav) { + Widget shell = AppShell(child: nav!); + // Android: 点击空白区域收起键盘 + if (Platform.isAndroid) { + shell = GestureDetector( + onTap: () => FocusManager.instance.primaryFocus?.unfocus(), + child: shell, + ); + } + return shell; + }, localizationsDelegates: const [ GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, diff --git a/lib/pages/note/note_form_page.dart b/lib/pages/note/note_form_page.dart index 24276f5..c448bb8 100644 --- a/lib/pages/note/note_form_page.dart +++ b/lib/pages/note/note_form_page.dart @@ -64,6 +64,7 @@ class _NoteFormPageState extends State { void dispose() { _autoSaveTimer?.cancel(); _saveStatusTimer?.cancel(); + _scrollController.dispose(); _titleController.dispose(); _contentController.dispose(); super.dispose(); diff --git a/lib/widgets/vditor_editor.dart b/lib/widgets/vditor_editor.dart index 410c152..634e82b 100644 --- a/lib/widgets/vditor_editor.dart +++ b/lib/widgets/vditor_editor.dart @@ -153,6 +153,14 @@ class VditorEditorState extends State { } catch (_) {} } + Future _requestHeightUpdate() async { + if (_controller == null || !_isReady) return; + try { + await Future.delayed(const Duration(milliseconds: 300)); + await _controller!.evaluateJavascript(source: 'requestHeightUpdate()'); + } catch (_) {} + } + void _onVditorReady() { if (_isReady) return; _fallbackTimer?.cancel(); @@ -276,10 +284,13 @@ class VditorEditorState extends State { } debugPrint('[VditorEditor] loading: $initialUrl'); - // 键盘弹出时,滚动到光标位置 + // 键盘弹出时,滚动到光标位置;键盘收起时,重新通知高度 final keyboardH = MediaQuery.of(context).viewInsets.bottom; if (keyboardH > 0 && _lastKeyboardH == 0 && _isReady) { Future.microtask(() => _scrollToCursor()); + } else if (keyboardH == 0 && _lastKeyboardH > 0 && _isReady) { + // 键盘收起,请求 JS 端重新计算高度 + Future.microtask(() => _requestHeightUpdate()); } _lastKeyboardH = keyboardH;