我正在使用 flutter Quill Editor ,并希望限制用户最多输入 1000 个字符。
最佳答案
我设法通过附加监听器并按以下方式更改文档来解决这个问题:
以 initState 为例:
widget.quillController.document.changes.listen(_onDocumentChange)
类里面的某个地方:
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),
);
}
}
关于flutter - 如何在 flutter quill 编辑器中设置字符限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71294770/