Reputation: 7024
Is there a way in dart language to keep the maximum input length fixed in TextFormField
?
Upvotes: 36
Views: 42924
Reputation: 111
try with the below code, here is one TextFormField where I'm taking a mobile number with max 10 digits
TextFormField(inputFormatters: [LengthLimitingTextInputFormatter(10)])
// replace your max length instead of 10
Upvotes: 0
Reputation: 933
Additionally to hahnsaja answer after you add maxLength - counter appears at the bottom of the TextFormField widget. To be able to hide it, add this to InputDecoration :
counterText: '',
counterStyle: TextStyle(fontSize: 0),
Example:
TextFormField(
controller: _deskripsiController,
onFieldSubmitted: (String value){
setState(() {
_deskripsiController.text = value;
});
},
decoration: const InputDecoration(
border: const UnderlineInputBorder(),
labelText: "Deskripsi",
counterText: '',
counterStyle: TextStyle(fontSize: 0),
),
maxLength: 8,
)
Upvotes: 3
Reputation: 1637
best way is inputFormatters option:
inputFormatters: [
new LengthLimitingTextInputFormatter(11),// for mobile
],
Upvotes: 6
Reputation: 1339
use maxLength
TextFormField(
controller: _deskripsiController,
onFieldSubmitted: (String value){
setState(() {
_deskripsiController.text = value;
});
},
decoration: const InputDecoration(
border: const UnderlineInputBorder(),
labelText: "Deskripsi",
),
maxLength: 8,
)
Upvotes: 30
Reputation: 277027
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';
Upvotes: 94