flutter-quill icon indicating copy to clipboard operation
flutter-quill copied to clipboard

How to override the cursor auto-scroll scheme?

Open Halecoder opened this issue 1 month ago • 1 comments

Have you checked for an existing issue?

Use case

Currently, when the cursor moves below the keyboard, QuillEditor automatically triggers scrolling to make the content visible. However, I want to customize the scrolling behavior, for example, triggering automatic scrolling when the cursor moves to just below the center of the screen. This would create more space between the input position and the keyboard. Are there any better methods to achieve this?

Proposal

Attached is the internal implementation:

  void _showCaretOnScreen() {
    if (!widget.config.showCursor || _showCaretOnScreenScheduled) {
      return;
    }

    _showCaretOnScreenScheduled = true;
    SchedulerBinding.instance.addPostFrameCallback((_) {
      if (widget.config.scrollable && _scrollController.hasClients) {
        _showCaretOnScreenScheduled = false;

        if (!mounted) {
          return;
        }

        final viewport = RenderAbstractViewport.of(renderEditor);
        final editorOffset =
            renderEditor.localToGlobal(const Offset(0, 0), ancestor: viewport);
        final offsetInViewport = _scrollController.offset + editorOffset.dy;

        final offset = renderEditor.getOffsetToRevealCursor(
          _scrollController.position.viewportDimension,
          _scrollController.offset,
          offsetInViewport,
        );

        final viewportHeight = scrollController.position.viewportDimension;

        if (offset != null) {
          if (_disableScrollControllerAnimateOnce) {
            _disableScrollControllerAnimateOnce = false;
            return;
          }
          _scrollController.animateTo(
            math.min(offset, _scrollController.position.maxScrollExtent),
            duration: const Duration(milliseconds: 100),
            curve: Curves.fastOutSlowIn,
          );
        }
      }
    });
  }

Halecoder avatar Oct 18 '25 11:10 Halecoder