ajay
ajay

Reputation: 1000

Limit number of characters allowed in TextField

How can I limit the maximum number of characters in TextField, I use maxLength property, but user can still able to type more than maxlenght.

Here is my code:

     TextField(
              maxLength: 10,
              inputFormatters: [
                 LengthLimitingTextInputFormatter(10),
              ],
              maxLengthEnforced: true,
              decoration: InputDecoration(
                border: OutlineInputBorder(),
                labelText: 'Details',
              ),
            )

enter image description here

Upvotes: 0

Views: 800

Answers (2)

ithersta
ithersta

Reputation: 188

There is an issue on GitHub. It's related to Gboard composing. Try to hit space and the text will be truncated to the limit.

Upvotes: 1

Aamil Silawat
Aamil Silawat

Reputation: 8229

Try out below code

inputFormatters: [
                    LengthLimitingTextInputFormatter(6), // set limit according to your requirment 
                                
                 ]

Use this property in your textfield

Upvotes: 2

Related Questions