Reputation: 551
TextField.maxLines
seems to be an option to limit the height of the Widget, not to limit the number of lines that can be input.
Is there a way to limit the number of inputable lines itself?
Upvotes: 9
Views: 5944
Reputation: 107
You can make your own inputFormat
rule like this:
TextFormField(
inputFormatters: [
TextInputFormatter.withFunction((oldValue, newValue) {
int newLines = newValue.text.split('\n').length;
if (newLines > 6) {
return oldValue;
} else {
return newValue;
}
}),
],
)
When the user tries to input more than 6 new lines ("\n"), it will simply ignore them.
Upvotes: 5
Reputation: 4750
TextField(
maxLength :10,
maxLine : 1)
MaxLength:
The maximum number of characters (Unicode scalar values) to allow in the text field.
If set, a character counter will be displayed below the field showing how many characters have been entered. If set to a number greater than 0, it will also display the maximum number allowed. If set to TextField.noMaxLength then only the current character count is displayed.
After maxLength characters have been input, additional input is ignored, unless maxLengthEnforcement is set to MaxLengthEnforcement.none.
MaxLine:
The maximum number of lines to show at one time, wrapping if necessary.
This affects the height of the field itself and does not limit the number of lines that can be entered into the field.
If this is 1 (the default), the text will not wrap, but will scroll horizontally instead.
If this is null, there is no limit to the number of lines, and the text container will start with enough vertical space for one line and automatically grow to accommodate additional lines as they are entered, up to the height of its constraints.
If this is not null, the value must be greater than zero, and it will lock the input to the given number of lines and take up enough horizontal space to accommodate that number of lines. Setting minLines as well allows the input to grow and shrink between the indicated range.
Upvotes: 1
Reputation: 1
You can set define maxLines with maxLength eg. TextField(maxLines: 4, maxLength: 500)
Upvotes: 0
Reputation: 1537
Method1: You can set maxLines
along with maxLength
.
If you don't want to show counter text at TextField
bottom then add empty counterText
in InputDecoration
.
TextField(
maxLength: 10,
maxLines: 2,
decoration: InputDecoration(
counterText: ''
),
)
Method2: You can use inputFormatters
property:
import 'package:flutter/services.dart';
TextFormField(
inputFormatters: [
LengthLimitingTextInputFormatter(10),
]
)
Upvotes: 5