'Restrict user to input 2 spaces at same time Flutter Dart

I am trying to restrict user from double spaces at same time i searched and found in textfield we can use inputFormatter to Block any key in keyboard.

I used this RegExp to block double spaces its working fine , the space button doesn't work when last character is space but when i type next character then its just remove spaces from textfield.

BlacklistingTextInputFormatter(
searchText.substring(searchText.length-1) == " "

? RegExp('[\\-|\\/\|\\:|\\ ]')

: RegExp('[\\-|\\/\|\\:]')),


Solution 1:[1]

Updated: this way you can check the last typed character then update widget state based on that:

//...
bool _isBlockedSpaceKey = false;
//...
TextField(
   // ...
   onChanged:(value) {
      setState(() {
         _isBlockedSpaceKey = value.endsWith(' ');
      });
   }
);

Solution 2:[2]

To restrict user to add 2 or more space together

In below Allow RegExp ther is one space in end and Deny RegExp there is two space

user this input formatter;-

inputFormatters: [FilteringTextInputFormatter.allow(RegExp('[a-z A-Z á-ú Á-Ú 0-9 ]')),FilteringTextInputFormatter.deny('  ')],

you can adjust/update allow and deny RegExp accordingly.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yashwardhan Pauranik
Solution 2 Aris_choice