Skip to content

Commit 2851948

Browse files
committed
feat: add tap to toggle tool bar
1 parent fe3ccd3 commit 2851948

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

lib/src/editor/editor.dart

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'dart:async';
12
import 'dart:math' as math;
23

34
import '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)) {

lib/src/editor/widgets/delegate.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,4 +387,8 @@ class EditorTextSelectionGestureDetectorBuilder {
387387
child: child,
388388
);
389389
}
390+
391+
void dispose() {}
392+
393+
void setCurrentCursorPosition(int? position) {}
390394
}

0 commit comments

Comments
 (0)