1+ import 'dart:async' ;
12import 'dart:math' as math;
23
34import 'package:flutter/cupertino.dart'
@@ -221,6 +222,20 @@ class QuillEditorState extends State<QuillEditor>
221222 _editorKey.currentState? .hideToolbar ();
222223 }
223224 });
225+
226+ controller.addListener (_controllerListener);
227+ }
228+
229+ @override
230+ void dispose () {
231+ controller.removeListener (_controllerListener);
232+ _selectionGestureDetectorBuilder.dispose ();
233+ super .dispose ();
234+ }
235+
236+ void _controllerListener () {
237+ final position = controller.selection.extentOffset;
238+ _selectionGestureDetectorBuilder.setCurrentCursorPosition (position);
224239 }
225240
226241 @override
@@ -499,6 +514,45 @@ class _QuillEditorSelectionGestureDetectorBuilder
499514 pressed.contains (LogicalKeyboardKey .shiftRight));
500515 }
501516
517+ int ? _previousCursorPosition;
518+ int ? _currentCursorPosition;
519+
520+ Timer ? _timer;
521+
522+ @override
523+ void dispose () {
524+ _timer? .cancel ();
525+ super .dispose ();
526+ }
527+
528+ @override
529+ void setCurrentCursorPosition (int ? position) {
530+ if (_currentCursorPosition != null ) {
531+ _previousCursorPosition = _currentCursorPosition;
532+ }
533+
534+ _currentCursorPosition = position;
535+ }
536+
537+ void _handleToggleToolbar () {
538+ final position = renderEditor! .selection.extentOffset;
539+
540+ final shouldShow = _currentCursorPosition != null &&
541+ _currentCursorPosition == _previousCursorPosition;
542+
543+ _currentCursorPosition = position;
544+ _previousCursorPosition = position;
545+
546+ if (shouldShow) {
547+ SchedulerBinding .instance.addPostFrameCallback ((_) {
548+ editor! .showToolbar ();
549+ _currentCursorPosition = null ;
550+ });
551+ } else {
552+ editor! .hideToolbar ();
553+ }
554+ }
555+
502556 @override
503557 void onSingleTapUp (TapUpDetails details) {
504558 if (_state.config.onTapUp != null &&
@@ -510,7 +564,8 @@ class _QuillEditorSelectionGestureDetectorBuilder
510564 return ;
511565 }
512566
513- editor! .hideToolbar ();
567+ _timer? .cancel ();
568+ _timer = Timer (const Duration (milliseconds: 100 ), _handleToggleToolbar);
514569
515570 try {
516571 if (delegate.selectionEnabled && ! _isPositionSelected (details)) {
0 commit comments