Remove TextField.noMaxLength, use maxLength = -1 instead (#24999)
This commit is contained in:
parent
1a60b166f2
commit
4c24bcc862
@ -76,10 +76,11 @@ class TextField extends StatefulWidget {
|
|||||||
///
|
///
|
||||||
/// The [maxLength] property is set to null by default, which means the
|
/// The [maxLength] property is set to null by default, which means the
|
||||||
/// number of characters allowed in the text field is not restricted. If
|
/// number of characters allowed in the text field is not restricted. If
|
||||||
/// [maxLength] is set, a character counter will be displayed below the
|
/// [maxLength] is set a character counter will be displayed below the
|
||||||
/// field, showing how many characters have been entered and how many are
|
/// field showing how many characters have been entered. If the value is
|
||||||
/// allowed unless the value is set to [noMaxLength] in which case only the
|
/// set to a positive integer it will also display the maximum allowed
|
||||||
/// current length is displayed.
|
/// number of characters to be entered. If the value is set to
|
||||||
|
/// [TextField.noMaxLength] then only the current length is displayed.
|
||||||
///
|
///
|
||||||
/// After [maxLength] characters have been input, additional input
|
/// After [maxLength] characters have been input, additional input
|
||||||
/// is ignored, unless [maxLengthEnforced] is set to false. The TextField
|
/// is ignored, unless [maxLengthEnforced] is set to false. The TextField
|
||||||
@ -135,7 +136,7 @@ class TextField extends StatefulWidget {
|
|||||||
assert(maxLengthEnforced != null),
|
assert(maxLengthEnforced != null),
|
||||||
assert(scrollPadding != null),
|
assert(scrollPadding != null),
|
||||||
assert(maxLines == null || maxLines > 0),
|
assert(maxLines == null || maxLines > 0),
|
||||||
assert(maxLength == null || maxLength > 0),
|
assert(maxLength == null || maxLength == TextField.noMaxLength || maxLength > 0),
|
||||||
keyboardType = keyboardType ?? (maxLines == 1 ? TextInputType.text : TextInputType.multiline),
|
keyboardType = keyboardType ?? (maxLines == 1 ? TextInputType.text : TextInputType.multiline),
|
||||||
assert(enableInteractiveSelection != null),
|
assert(enableInteractiveSelection != null),
|
||||||
super(key: key);
|
super(key: key);
|
||||||
@ -233,20 +234,25 @@ class TextField extends StatefulWidget {
|
|||||||
|
|
||||||
/// If [maxLength] is set to this value, only the "current input length"
|
/// If [maxLength] is set to this value, only the "current input length"
|
||||||
/// part of the character counter is shown.
|
/// part of the character counter is shown.
|
||||||
static const int noMaxLength = 9007199254740992; // math.pow(2, 53);
|
static const int noMaxLength = -1;
|
||||||
|
|
||||||
/// The maximum number of characters (Unicode scalar values) to allow in the
|
/// The maximum number of characters (Unicode scalar values) to allow in the
|
||||||
/// text field.
|
/// text field.
|
||||||
///
|
///
|
||||||
/// If set, a character counter will be displayed below the
|
/// If set, a character counter will be displayed below the
|
||||||
/// field, showing how many characters have been entered and how many are
|
/// field showing how many characters have been entered. If set to a number
|
||||||
/// allowed. After [maxLength] characters have been input, additional input
|
/// greather 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 [maxLengthEnforced] is set to false. The TextField
|
/// is ignored, unless [maxLengthEnforced] is set to false. The TextField
|
||||||
/// enforces the length with a [LengthLimitingTextInputFormatter], which is
|
/// enforces the length with a [LengthLimitingTextInputFormatter], which is
|
||||||
/// evaluated after the supplied [inputFormatters], if any.
|
/// evaluated after the supplied [inputFormatters], if any.
|
||||||
///
|
///
|
||||||
/// This value must be either null or greater than zero. If set to null
|
/// This value must be either null, [TextField.noMaxLength], or greater than 0.
|
||||||
/// (the default), there is no limit to the number of characters allowed.
|
/// If null (the default) then there is no limit to the number of characters
|
||||||
|
/// that can be entered. If set to [TextField.noMaxLength], then no limit will
|
||||||
|
/// be enforced, but the number of characters entered will still be displayed.
|
||||||
///
|
///
|
||||||
/// Whitespace characters (e.g. newline, space, tab) are included in the
|
/// Whitespace characters (e.g. newline, space, tab) are included in the
|
||||||
/// character count.
|
/// character count.
|
||||||
@ -408,11 +414,12 @@ class _TextFieldState extends State<TextField> with AutomaticKeepAliveClientMixi
|
|||||||
String counterText = '$currentLength';
|
String counterText = '$currentLength';
|
||||||
String semanticCounterText = '';
|
String semanticCounterText = '';
|
||||||
|
|
||||||
if (widget.maxLength != TextField.noMaxLength) {
|
// Handle a real maxLength (positive number)
|
||||||
|
if (widget.maxLength > 0) {
|
||||||
|
// Show the maxLength in the counter
|
||||||
counterText += '/${widget.maxLength}';
|
counterText += '/${widget.maxLength}';
|
||||||
final int remaining = (widget.maxLength - currentLength).clamp(0, widget.maxLength);
|
final int remaining = (widget.maxLength - currentLength).clamp(0, widget.maxLength);
|
||||||
semanticCounterText = localizations.remainingTextFieldCharacterCount(remaining);
|
semanticCounterText = localizations.remainingTextFieldCharacterCount(remaining);
|
||||||
}
|
|
||||||
|
|
||||||
// Handle length exceeds maxLength
|
// Handle length exceeds maxLength
|
||||||
if (_effectiveController.value.text.runes.length > widget.maxLength) {
|
if (_effectiveController.value.text.runes.length > widget.maxLength) {
|
||||||
@ -425,6 +432,8 @@ class _TextFieldState extends State<TextField> with AutomaticKeepAliveClientMixi
|
|||||||
semanticCounterText: semanticCounterText,
|
semanticCounterText: semanticCounterText,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return effectiveDecoration.copyWith(
|
return effectiveDecoration.copyWith(
|
||||||
counterText: counterText,
|
counterText: counterText,
|
||||||
semanticCounterText: semanticCounterText,
|
semanticCounterText: semanticCounterText,
|
||||||
|
@ -134,10 +134,10 @@ class LengthLimitingTextInputFormatter extends TextInputFormatter {
|
|||||||
/// Creates a formatter that prevents the insertion of more characters than a
|
/// Creates a formatter that prevents the insertion of more characters than a
|
||||||
/// limit.
|
/// limit.
|
||||||
///
|
///
|
||||||
/// The [maxLength] must be null or greater than zero. If it is null, then no
|
/// The [maxLength] must be null, -1 or greater than zero. If it is null or -1
|
||||||
/// limit is enforced.
|
/// then no limit is enforced.
|
||||||
LengthLimitingTextInputFormatter(this.maxLength)
|
LengthLimitingTextInputFormatter(this.maxLength)
|
||||||
: assert(maxLength == null || maxLength > 0);
|
: assert(maxLength == null || maxLength == -1 || maxLength > 0);
|
||||||
|
|
||||||
/// The limit on the number of characters (i.e. Unicode scalar values) this formatter
|
/// The limit on the number of characters (i.e. Unicode scalar values) this formatter
|
||||||
/// will allow.
|
/// will allow.
|
||||||
@ -171,7 +171,7 @@ class LengthLimitingTextInputFormatter extends TextInputFormatter {
|
|||||||
TextEditingValue oldValue, // unused.
|
TextEditingValue oldValue, // unused.
|
||||||
TextEditingValue newValue,
|
TextEditingValue newValue,
|
||||||
) {
|
) {
|
||||||
if (maxLength != null && newValue.text.runes.length > maxLength) {
|
if (maxLength != null && maxLength > 0 && newValue.text.runes.length > maxLength) {
|
||||||
final TextSelection newSelection = newValue.selection.copyWith(
|
final TextSelection newSelection = newValue.selection.copyWith(
|
||||||
baseOffset: math.min(newValue.selection.start, maxLength),
|
baseOffset: math.min(newValue.selection.start, maxLength),
|
||||||
extentOffset: math.min(newValue.selection.end, maxLength),
|
extentOffset: math.min(newValue.selection.end, maxLength),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user