singerdmx / flutter-quill

Rich text editor for Flutter
https://pub.dev/packages/flutter_quill
MIT License
2.6k stars 839 forks source link

Word Counter for The Editor #2302

Open m-1226 opened 1 month ago

m-1226 commented 1 month ago

Is there an existing issue for this?

The question

how to I set a word counter for the editor and when it exceed it it highlight it as red color?

yzxh24 commented 1 month ago

You can get the content length with the following code:

int getContentLength() {
  return quillController.document.toPlainText().trimRight().length;
}

Then add a totalListener to the quillController:

ValueNotifier<int> total = ValueNotifier(0);
void totalListener() {
  WidgetsBinding.instance.addPostFrameCallback((_) {
    total.value = getContentLength();
  });
}

Register this Listener in initState():

@override
void initState() {
  super.initState();
  quillController.addListener(totalListener);
}

and dispose:

@override
void dispose() {
  quillController.removeListener(totalListener);
  quillController.dispose();

  super.dispose();
}

This way you can get the number of characters in real time

scenephile commented 1 month ago

thank you worked for me