Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I limit the size of a text field in flutter?

The TextField widget doesn't seem to have a "limit" attribute to limit the number of characters that can be typed. How I can enforce that only a certain number of characters can be provided as input in a TextField Widget. I tried looking at the decoration property and potentially setting the limit there somehow but that didn't seem to work either. Is there a different widget I should be using?

like image 202
Anonk Avatar asked Jul 06 '17 17:07

Anonk


People also ask

How do you restrict the length of a TextField in Flutter?

using maxLength will give you length counter and increases the Field height, so this answer would be the accepted one, this one is working fine. Thank You, It is working fine to me. I used this to restrict the length of AutocompleteTextField.

How do I limit a TextField?

Right-click the text box for which you want to limit characters, and then click Text Box Properties on the shortcut menu. Click the Display tab. Under Options, select the Limit text box to check box, and then specify the number of characters that you want.

How do you change the length of a text in Flutter?

You can change the font size of text in a Text Widget using style property. Create a TextStyle object with fontSize and specify this object as style for Text Widget.


Video Answer


2 Answers

Use inputFormatters property

example:

TextFormField(       inputFormatters: [         LengthLimitingTextInputFormatter(10),       ]     ) 

namespace

import 'package:flutter/services.dart'; 
like image 179
Shyju M Avatar answered Sep 21 '22 10:09

Shyju M


You can use the maxLength property and you can still hide the bottom counter text by setting the counterText to empty string.

TextField(   maxLength: 10,   decoration: InputDecoration(     counterText: ''   ), ) 
like image 27
Sami Kanafani Avatar answered Sep 21 '22 10:09

Sami Kanafani