Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit number of characters in TextFormField?

Tags:

flutter

dart

Is there a way in dart language to keep the maximum input length fixed in TextFormField?

like image 381
Farwa Avatar asked Mar 06 '18 08:03

Farwa


People also ask

How do I limit the number of characters in a TextField flutter?

To set the limit for the input field we will use maxLength: propery.

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 use TextFormField in flutter?

How to Handle Input Data In TextFormField In Flutter Using Controller. To handle user input in TextFormField, create an object of TextEditingController class. Create a TextEditingController object like below and assign it to the controller property of TextFormField. Its object will hold the input data.

What is TextEditingController flutter?

TextEditingController class Null safety. A controller for an editable text field. Whenever the user modifies a text field with an associated TextEditingController, the text field updates value and the controller notifies its listeners.


2 Answers

Use inputFormatters property. The following code would limit the textFormField to 42 in length :

new TextFormField(
  inputFormatters: [
    new LengthLimitingTextInputFormatter(42),
  ],
);

UPDATE: Import the package first. import 'package:flutter/services.dart';

like image 130
Rémi Rousselet Avatar answered Oct 23 '22 12:10

Rémi Rousselet


use maxLength

TextFormField(
  controller: _deskripsiController,
  onFieldSubmitted: (String value){
        setState(() {
          _deskripsiController.text = value;
        });
  },
  decoration: const InputDecoration(
        border: const UnderlineInputBorder(),
        labelText: "Deskripsi",
  ),
  maxLength: 8,
)
like image 27
Hanhan Avatar answered Oct 23 '22 12:10

Hanhan