-2

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 1

2

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);
                  },
                )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.