From cbcc023f0305d69b67630078d7cff98ffddfebf5 Mon Sep 17 00:00:00 2001 From: Huan Du Date: Thu, 25 Jul 2024 22:24:34 +0800 Subject: [PATCH] fix: hide toolbar if editor loses focus (#2066) --- lib/src/editor/editor.dart | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/src/editor/editor.dart b/lib/src/editor/editor.dart index a46fba7e..57a6cfa8 100644 --- a/lib/src/editor/editor.dart +++ b/lib/src/editor/editor.dart @@ -174,16 +174,28 @@ class QuillEditorState extends State @override void initState() { super.initState(); - widget.configurations.controller.editorFocusNode ??= widget.focusNode; - if (configurations.autoFocus) { - widget.configurations.controller.editorFocusNode?.requestFocus(); - } + _editorKey = configurations.editorKey ?? GlobalKey(); _selectionGestureDetectorBuilder = _QuillEditorSelectionGestureDetectorBuilder( this, configurations.detectWordBoundary, ); + + final focusNode = + widget.configurations.controller.editorFocusNode ?? widget.focusNode; + widget.configurations.controller.editorFocusNode = focusNode; + + if (configurations.autoFocus) { + focusNode.requestFocus(); + } + + // Hide toolbar when the editor loses focus. + focusNode.addListener(() { + if (!focusNode.hasFocus) { + _editorKey.currentState?.hideToolbar(); + } + }); } @override