Skip to content

Commit

Permalink
Basics -> Widgets -> TextField shows incorrect count of words X-Wei#125
Browse files Browse the repository at this point in the history
… fix
  • Loading branch information
raheemadamboev committed Apr 13, 2022
1 parent 79fd9e5 commit 556bffd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/routes/widgets_textfield_ex.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class _TextFieldExampleState extends State<TextFieldExample> {
borderRadius: BorderRadius.all(Radius.circular(10.0)),
),
),
onSubmitted: (val) =>
Fluttertoast.showToast(msg: 'You entered: ${int.parse(val)}'),
onSubmitted: (val) => Fluttertoast.showToast(msg: 'You entered: ${int.parse(val)}'),
onChanged: (String val) {
final v = int.tryParse(val);
debugPrint('parsed value = $v');
Expand All @@ -45,15 +44,25 @@ class _TextFieldExampleState extends State<TextFieldExample> {
maxLines: 10,
textCapitalization: TextCapitalization.sentences,
decoration: InputDecoration(
counterText: '${this._controller.text.split(' ').length} words',
counterText: '${_countWords(text: this._controller.text)} words',
labelText: 'Enter multiline text:',
alignLabelWithHint: true,
hintText: 'type something...',
border: const OutlineInputBorder(),
),
onChanged: (text) => setState(() {}),
);
}

int _countWords({required String text}) {
final trimmedText = text.trim();
if (trimmedText.isEmpty) {
return 0;
} else {
return trimmedText.split(RegExp("\\s+")).length;
}
}

bool _showPassword = false;

Widget _buildPasswordTextField() {
Expand Down
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.11"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
meta:
dependency: transitive
description:
Expand Down

0 comments on commit 556bffd

Please sign in to comment.