Search code examples
flutterdartquill

How can i set character limit in flutter quill editor


I am using flutter Quill Editor , and want to limit the user to enter maximum 1000 char.


Solution

  • I managed to solve it by attaching listener and changing the document the following way:

    in initState for example:

    widget.quillController.document.changes.listen(_onDocumentChange)
    

    somewhere in class:

    void _onDocumentChange(Tuple3<quill.Delta, quill.Delta, quill.ChangeSource> tuple) {
        final documentLength = widget.quillController.document.length;
        if (documentLength > widget.limit) {
          final latestIndex = widget.limit - 1;
          widget.quillController.replaceText(
            latestIndex,
            documentLength - widget.limit,
            '',
            TextSelection.collapsed(offset: latestIndex),
          );
        }
     }