I want to get the number of words in a text widget in flutter and display them for a user to know the word count in a particular text widget
1 Answer
You can regexp to find the number of spaces in the paragraph and +1 to it to get the count of a number of words. This will probably not work in the case of multiple paragraphs. But you can enhance the regex to modify it according to your use case.
TextField(
onChanged: (value) {
RegExp regExp = RegExp(" ");
print(regExp.allMatches(value).length + 1);
},
)