177

enter image description here

I'm facing a small problem. As you can see, i have set maxLength 1 of TextField in Flutter, But i'm unable to hide bottom label of text counter.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Sunil
  • 3,211
  • 3
  • 21
  • 21

22 Answers22

350

To hide counter value from TextField or TextFormField widget while using maxLength attribute, try following:

TextField(
  decoration: InputDecoration(
    hintText: "Email",
    counterText: "",
  ),
  maxLength: 40,
),

In this, I have set counterText attribute inside InputDecoration property with empty value. Hope it will help.

John Alexander Betts
  • 4,718
  • 8
  • 47
  • 72
Rahul Sharma
  • 12,515
  • 6
  • 21
  • 30
  • 9
    This is great if you just want to hide it, but it's still there and take up space often resulting in an uneven text box. Replacing maxLength with `TextField( inputFormatters: [LengthLimitingTextInputFormatter(mMaxLength)] )` Will solve it, but requires you to add an extra library to the file. – Joel Broström Feb 11 '19 at 12:58
  • 1
    this is working but `counterText` taking space height and affecting the UI – Farhana Naaz Ansari Feb 19 '19 at 07:33
  • 18
    for me doesn't keep the empty space, its being removed, running latest version of flutter helps? – voytez Oct 21 '19 at 14:09
  • @FarhanaNaazAnsari also set your helperText = ' ' . if it doesn't work wrap it with container – Pius T.K Jul 09 '21 at 17:53
162

This is the proper approach, it will prevent extra space below the Text Field, and also avoid extra code to set a null widget.

You can use input formatters in TextField

The following is:

    inputFormatters:[
      LengthLimitingTextInputFormatter(1),
    ]

Thank You!

Cassio Seffrin
  • 7,293
  • 1
  • 54
  • 54
Varun
  • 1,658
  • 1
  • 7
  • 11
46

you can use InputDecoratoin to hide letter counter.

TextFormField(
   decoration: InputDecoration(
     labelText: "username",
     counterStyle: TextStyle(height: double.minPositive,),
     counterText: ""
)
Mahdi Tohidloo
  • 2,968
  • 1
  • 17
  • 17
42

You can hide the counter by adding counterText: '', inside the textfield decoration. It will simply display an empty String.

Rahul Sharma
  • 12,515
  • 6
  • 21
  • 30
petras J
  • 2,538
  • 3
  • 16
  • 23
35

Simply set counter to Offstage() will do the trick.

TextField(
    maxLines: 1,
    decoration: InputDecoration(
      counter: Offstage(),
    ),
),
Yingkun
  • 351
  • 3
  • 3
  • 1
    This solution still affects the GUI (displaces the input text above the max length counter). The best solution (atleast for my use-case) was the ``counterText=""`` solution. – SilSur Jul 31 '22 at 10:04
22

You can do:

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

Most of the answers seem to work. Another way would be to assign the counter with a shrink SizeBox.

TextField(decoration: InputDecoration(
    hintText: "Email",
    counter: SizedBox.shrink()
),
  maxLength: 40,
),
Kevin Nzioka
  • 785
  • 1
  • 6
  • 11
12

Add counterText: "", to InputDecoration

Image example of counterText

TextField(
    decoration: InputDecoration(
        counterText: "",
    ),
    maxLength: 10,
),
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Sonu kumar
  • 351
  • 3
  • 7
10

This alone solved my problem!

TextField(
   decoration: InputDecoration(
     labelText: "Table Number",
     counterText: ""
)
TuGordoBello
  • 4,350
  • 9
  • 52
  • 78
Dangdat
  • 361
  • 4
  • 7
9

Whenever you don't need something in flutter just put an empty container!

TextField(
  decoration: InputDecoration(
    hintText: "Email",
    counter: Container(),
  ),
  maxLength: 20,
),
Boken
  • 4,825
  • 10
  • 32
  • 42
JIJO J
  • 169
  • 3
  • 7
9

Simply set buildCounter to null.
It is a callback that generates a custom [InputDecorator.counter] widget

TextField(
   maxLength: (some length),
   buildCounter: (BuildContext context, {int currentLength, int maxLength, bool isFocused}) => null,
);
Terry
  • 101
  • 1
  • 4
7

For null-safety use this:

TextField(
   maxLength: 10,
   buildCounter: (BuildContext context, {int? currentLength, int? maxLength, bool? isFocused}) => null,
)
aytunch
  • 1,326
  • 1
  • 16
  • 31
  • 1
    This is the best answer IMO to remove the counter completely since setting the "counter" parameter in the InputDecoration to OffStage() or other zero space widget may hide the counter, but I have noticed that there is a 4 pixel height padding that remains - so this "buildCounter" method completely removes it. – Pip Apr 28 '22 at 20:09
  • 1
    I agree. This answer is the most flexible and isn't that too difficult to implement considering the added amount of control you now have. – enchance Oct 27 '22 at 06:56
5

You can use input formatters in TextField setting a limit to input and this is the best approach if you just want to hide the counter making a input limit.

import 'package:flutter/services.dart';

inputFormatters:[
      LengthLimitingTextInputFormatter(1),
]

Or you can customize the decoration making a counter = Container():

 decoration: InputDecoration(
    hintText: "Email",
    counter: Container(),
  ),

You if prefer to customize the buildCounter, here is how to do it properly (you can also customise the font, color etc). When you text field loses the focus the counter limits will disappears. Or you can just set

        TextField(
          controller: _clienteTextEditingController,
          maxLength: 50,
          buildCounter: (BuildContext context,
              {int currentLength, int maxLength, bool isFocused}) {
            return isFocused
                ? Text(
                    'The Input Limits are: $currentLength/$maxLength ',
                    style: new TextStyle(
                      fontSize: 10.0,
                    ),
                    semanticsLabel: 'Input constraints',
                  )
                : null;
          },
        ),
Cassio Seffrin
  • 7,293
  • 1
  • 54
  • 54
4

you can use InputDecoratoin to hide the letter counter.

TextField(
            keyboardType: TextInputType.number,
            maxLength: 10,
            decoration: InputDecoration(
            **counterText:""**)
           )
Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
3

Another solution is to hide the counter widget by using SizedBox:

TextFormField(
...
decoration: InputDecoration(
    contentPadding: EdgeInsets.all(10.0),
    counter: new SizedBox(
      height: 0.0,
    )),
...
)
Maybelle Pacate
  • 481
  • 7
  • 5
3

You can follow the below codes.

 TextField(
     controller: myController,
     maxLength: 3,
     buildCounter: (BuildContext context, {int currentLength, int maxLength, bool isFocused}) =>null
    )
Salim Murshed
  • 1,423
  • 1
  • 8
  • 19
3
    Container(
                    height: 48,
                    alignment: Alignment.centerLeft,
                    padding: EdgeInsets.symmetric(horizontal: 16),
                    decoration: BoxDecoration(
                        border: Border.all(
                          color: CustomColors.kToDark,
                        ),
                        color: CustomColors.White,
                        borderRadius: BorderRadius.all(Radius.circular(8))),
                    child: TextFormField(
                      textAlign: TextAlign.left,
                        cursorColor: CustomColors.kToDark,
                        maxLength: 30,
                        controller: _titleController,
                        keyboardType: TextInputType.text,
                        decoration: InputDecoration(
                          counterText: "",
                            border: InputBorder.none,
                            isDense: true,
                            contentPadding: EdgeInsets.all(0))),
                  ),

enter image description here

enter image description here

Use counterText: "" inside InputDecoration()

Diksha Pruthi
  • 276
  • 2
  • 5
2

In case you are still looking for the answer in 2020 here goes:

decoration: InputDecoration(                                                       
    counter: Spacer(),                                                             
    labelText: "National ID #",                                                    
    border: InputBorder.none,                                                      
    hintText: 'e.g 01-1234567A12',                                                 
    hintStyle: TextStyle(color: Colors.grey)),

In the TextField, use a Spacer() as counter... hope this helps, i dont know if it breaks anything else but mine works fine.

helvete
  • 2,455
  • 13
  • 33
  • 37
1

Not pretty but works by removing the counter:

TextFormField(
  buildCounter: (
    BuildContext context, {
    required int currentLength,
    int? maxLength,
    required bool isFocused,
  }) => null,
Ced
  • 15,847
  • 14
  • 87
  • 146
1

Use a SizedBox.shrink() for the counter widget in InputDecoration:

enter image description here

Brian Y.
  • 41
  • 2
0

Use a SizedBox with zero height and width:

TextField(
          maxLength: 400,
          decoration: InputDecoration(
            counter: SizedBox(
              width: 0,
              height: 0,
            ),),)
Hadi
  • 119
  • 10
0

As mentioned by @user10481267 in the best answer would be use buildCounter property. This gives extreme flexibility and you can even decide dynamically to show the counter or not.

I was building a dynamic form with the required properties in JSON. My implementation is as follows:

TextFormField(
    buildCounter: (BuildContext context,
    {
            int currentLength,
            int maxLength,
            bool isFocused}) {
                    if (isFocused)
                            return formFields[index]["max"] == null
                                    ? null
                                    : Text(
                                        '$currentLength / $maxLength',
                                        semanticsLabel: 'character count',
                                      );
                              else
                                return null;
                            },
                            maxLength: formFields[index]["max"] ?? 100,
                            decoration: new InputDecoration(
                              labelText: formFields[index]["hint"] ?? "",
                              fillColor: Colors.green,
                              border: new OutlineInputBorder(
                                borderRadius: new BorderRadius.circular(15.0),
                                borderSide: new BorderSide(),
                              ),
                            )
                          )
Lakshman Pilaka
  • 1,803
  • 2
  • 24
  • 48