1

Currently, the limit counter is placed below, but I would like to have it inside the TextFormField at the end. How can I do that?

        child: TextFormField(
                      controller: nameController,
                      maxLength: 50,
                      validator: (String? value) {
                        if (value!.trim().isEmpty) {
                          return 'error';
                        }
                        return null;
                      },
                      onSaved: (String? value) {
                        name = value!.trim();
                      },
                    ),
TarHalda
  • 1,050
  • 1
  • 9
  • 27
John
  • 13
  • 3

1 Answers1

0

According to the documentation for the maxLength property on a TextFormField, you have no control over where that is displayed. You can set up a similar counter using the onChanged property of the TextFormField and the suffixText property of the inputDecorator property of the TextFormField (documentation). This requires that the TextFormField be part of a StatefulWidget.

String counterStr = "0";
....
child: TextFormField(
                  controller: nameController,
                  maxLength: 50,
                  inputDecorator: InputDecorator(
                    suffixText: counterStr
                  ),
                  validator: (String? value) {
                    if (value!.trim().isEmpty) {
                      return 'error';
                    }
                    return null;
                  },
                  onSaved: (String? value) {
                    name = value!.trim();
                  },
                  onChanged: (){
                    setState((){
                      counterStr = nameController.text.length.toString();
                    });
                  }
                ),

TarHalda
  • 1,050
  • 1
  • 9
  • 27
  • 1
    This is working just like I want it to - Thanks! I did change the suffixText to '$counterStr/50' to still be able to show the users how many characters they can enter – John Oct 20 '22 at 07:26