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

TextfieldFlutter

Textfield Problem Overview


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?

Textfield Solutions


Solution 1 - Textfield

Use inputFormatters property

example:

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

namespace

import 'package:flutter/services.dart';

Solution 2 - Textfield

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: ''
  ),
)

Solution 3 - Textfield

maxLength property is available in Flutter.

https://docs.flutter.io/flutter/material/TextField/maxLength.html

TextField(
  maxLength: 45,
)

Solution 4 - Textfield

I had to add an additional snippet to what RSproute mentioned. The full code is here:

TextEditingController _controller = new TextEditingController();
String text = ""; // empty string to carry what was there before it 
onChanged
int maxLength = ...
...
new TextField(
    controller: _controller,
    onChange: (String newVal) {
        if(newVal.length <= maxLength){
            text = newVal;
        }else{
            _controller.value = new TextEditingValue(
                text: text,
                selection: new TextSelection(
                    baseOffset: maxLength,
                    extentOffset: maxLength,
                    affinity: TextAffinity.downstream,
                    isDirectional: false
                ),
                composing: new TextRange(
                    start: 0, end: maxLength
                )
            );
            _controller.text = text;
        } 
    }
);

Solution 5 - Textfield

You can control everything about the Text Field with the TextEditingController. So if you were to pair this information with an onChanged event from the TextField you could perform any logic you like in there. For example:

TextEditingController _controller = new TextEditingController();
String text = ""; // empty string to carry what was there before it onChanged
int maxLength = ...
...
new TextField(
    controller: _controller,
    onChanged: (String newVal) {
        if(newVal.length <= maxLength){
            text = newVal;
        }else{
            _controller.text = text;
        }

    }
)

I am able to control the text field to stay within the guidelines because if it ever goes over it, it will revert to what it was before the last type.

Solution 6 - Textfield

With th version 1.25 this is even easier. The maxLengthEnforced property needs to be set to true. Otherwise the response above will not work.

     Container(
                  margin: EdgeInsets.only(right: 5),
                  child: SizedBox(
                    width: 70,
                    child: TextField(
                      keyboardType: TextInputType.number,
                      maxLengthEnforced: true,
                      maxLength: 2,
                      decoration: InputDecoration(
                        labelText: 'Hours',
                        counterText: '',
                      ),
                      controller: hourField,
                    ),
                  ),
                ),

enter image description here

Solution 7 - Textfield

You can set LengthLimitingTextInputFormatter in input formatters

TextField(
   keyboardType: TextInputType.number,
   inputFormatters: [
     FilteringTextInputFormatter.digitsOnly,
     LengthLimitingTextInputFormatter(n,), //n is maximum number of characters you want in textfield
   ],
),

Solution 8 - Textfield

You can use this code snipped to limit length and hide counter:

TextFormField(
   maxLength: 10,
   buildCounter: (BuildContext context, { int currentLength, int maxLength, bool isFocused }) => null,
);

Original answer you can find here.

Solution 9 - Textfield

There are two ways to do that

Solution One

LengthLimitingTextInputFormatter Creates a formatter that prevents the insertion of more characters than a limit.

      TextField(              
              inputFormatters: [
                new LengthLimitingTextInputFormatter(5), /// here char limit is 5
              ],
                ///....
              )

Solution Two:

          TextField(
                  maxLength: 5, /// here char limit is 5

If you don't want to show counter text at TextField bottom then add empty counterText in InputDecoration

Example:

TextField(
              maxLength: 100,
              decoration: InputDecoration(
                  counterText: '',
                  ///....

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnonkView Question on Stackoverflow
Solution 1 - TextfieldShyju MView Answer on Stackoverflow
Solution 2 - TextfieldSami KanafaniView Answer on Stackoverflow
Solution 3 - TextfieldUpaJahView Answer on Stackoverflow
Solution 4 - TextfieldAnonkView Answer on Stackoverflow
Solution 5 - TextfieldRSprouleView Answer on Stackoverflow
Solution 6 - TextfieldAlpha BAView Answer on Stackoverflow
Solution 7 - TextfieldHamza AliView Answer on Stackoverflow
Solution 8 - TextfieldVolodymyr YatsykivView Answer on Stackoverflow
Solution 9 - TextfieldJitesh MohiteView Answer on Stackoverflow