Text input should not be space as initial. once user write some charater it allows to put space between two words. but as an initial character it supports only and only text in text form field.
Asked
Active
Viewed 79 times
0
-
do you mean that you don't want to calculate space as a character especially when the user types it first? – Ahmed Hussein Feb 15 '23 at 11:55
-
Yes. I want to block space as an initial character – wolfsoft Developer Feb 15 '23 at 11:59
-
https://stackoverflow.com/a/67394385/8597548 Please refer this link, it is provided by @rickimaru – Maulik Sinroja Feb 15 '23 at 12:20
1 Answers
1
If you want to restrict space as an initial character, you can achieve that by inputFormatters
, here's an example:
class NoInitialSpaceInputFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
// Prevent entering a space as the initial character
if (newValue.text.startsWith(' ')) {
return oldValue;
}
return newValue;
}
}
And pass it as a inputFormatters
to TextField
like this:
TextField(
inputFormatters: [NoInitialSpaceInputFormatter()],
)

Ahmed Hussein
- 131
- 8