Is there a way in dart language to keep the maximum input length fixed in TextFormField
?
To set the limit for the input field we will use maxLength: propery.
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 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.
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.
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';
use maxLength
TextFormField(
controller: _deskripsiController,
onFieldSubmitted: (String value){
setState(() {
_deskripsiController.text = value;
});
},
decoration: const InputDecoration(
border: const UnderlineInputBorder(),
labelText: "Deskripsi",
),
maxLength: 8,
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With