How to set a max character limit? #860
-
I need to set a max character limit of let's say 1000 characters. I can get the length of the character using quillController.document.length and then added a listener which checks the limit but I can't figure out how to restrict the addition of new characters |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
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),
);
}
} |
Beta Was this translation helpful? Give feedback.
-
if you typing icon, there will be problems with logic |
Beta Was this translation helpful? Give feedback.
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: