Deprecate `maxLengthEnforced` for text fields (#72043)
diff --git a/packages/flutter/lib/src/cupertino/text_field.dart b/packages/flutter/lib/src/cupertino/text_field.dart
index 9ba66a7..49cfa7a 100644
--- a/packages/flutter/lib/src/cupertino/text_field.dart
+++ b/packages/flutter/lib/src/cupertino/text_field.dart
@@ -262,6 +262,11 @@
this.minLines,
this.expands = false,
this.maxLength,
+ @Deprecated(
+ 'Use maxLengthEnforcement parameter which provides more specific '
+ 'behavior related to the maxLength limit. '
+ 'This feature was deprecated after v1.25.0-5.0.pre.'
+ )
this.maxLengthEnforced = true,
this.maxLengthEnforcement,
this.onChanged,
@@ -409,6 +414,11 @@
this.minLines,
this.expands = false,
this.maxLength,
+ @Deprecated(
+ 'Use maxLengthEnforcement parameter which provides more specific '
+ 'behavior related to the maxLength limit. '
+ 'This feature was deprecated after v1.25.0-5.0.pre.'
+ )
this.maxLengthEnforced = true,
this.maxLengthEnforcement,
this.onChanged,
@@ -654,6 +664,11 @@
///
/// If true, prevents the field from allowing more than [maxLength]
/// characters.
+ @Deprecated(
+ 'Use maxLengthEnforcement parameter which provides more specific '
+ 'behavior related to the maxLength limit. '
+ 'This feature was deprecated after v1.25.0-5.0.pre.'
+ )
final bool maxLengthEnforced;
/// Determines how the [maxLength] limit should be enforced.
@@ -808,7 +823,7 @@
FocusNode get _effectiveFocusNode => widget.focusNode ?? (_focusNode ??= FocusNode());
MaxLengthEnforcement get _effectiveMaxLengthEnforcement => widget.maxLengthEnforcement
- ?? LengthLimitingTextInputFormatter.inferredDefaultMaxLengthEnforcement;
+ ?? LengthLimitingTextInputFormatter.getDefaultMaxLengthEnforcement();
bool _showSelectionHandles = false;
diff --git a/packages/flutter/lib/src/material/text_field.dart b/packages/flutter/lib/src/material/text_field.dart
index d274257..367f41a 100644
--- a/packages/flutter/lib/src/material/text_field.dart
+++ b/packages/flutter/lib/src/material/text_field.dart
@@ -359,6 +359,11 @@
this.minLines,
this.expands = false,
this.maxLength,
+ @Deprecated(
+ 'Use maxLengthEnforcement parameter which provides more specific '
+ 'behavior related to the maxLength limit. '
+ 'This feature was deprecated after v1.25.0-5.0.pre.'
+ )
this.maxLengthEnforced = true,
this.maxLengthEnforcement,
this.onChanged,
@@ -606,6 +611,11 @@
///
/// If true, prevents the field from allowing more than [maxLength]
/// characters.
+ @Deprecated(
+ 'Use maxLengthEnforcement parameter which provides more specific '
+ 'behavior related to the maxLength limit. '
+ 'This feature was deprecated after v1.25.0-5.0.pre.'
+ )
final bool maxLengthEnforced;
/// Determines how the [maxLength] limit should be enforced.
@@ -856,7 +866,7 @@
FocusNode get _effectiveFocusNode => widget.focusNode ?? (_focusNode ??= FocusNode());
MaxLengthEnforcement get _effectiveMaxLengthEnforcement => widget.maxLengthEnforcement
- ?? LengthLimitingTextInputFormatter.inferredDefaultMaxLengthEnforcement;
+ ?? LengthLimitingTextInputFormatter.getDefaultMaxLengthEnforcement(Theme.of(context).platform);
bool _isHovering = false;
diff --git a/packages/flutter/lib/src/material/text_form_field.dart b/packages/flutter/lib/src/material/text_form_field.dart
index 0e60d52..dd77378 100644
--- a/packages/flutter/lib/src/material/text_form_field.dart
+++ b/packages/flutter/lib/src/material/text_form_field.dart
@@ -165,6 +165,11 @@
'This feature was deprecated after v1.19.0.'
)
bool autovalidate = false,
+ @Deprecated(
+ 'Use maxLengthEnforcement parameter which provides more specific '
+ 'behavior related to the maxLength limit. '
+ 'This feature was deprecated after v1.25.0-5.0.pre.'
+ )
bool maxLengthEnforced = true,
MaxLengthEnforcement? maxLengthEnforcement,
int? maxLines = 1,
diff --git a/packages/flutter/lib/src/services/text_formatter.dart b/packages/flutter/lib/src/services/text_formatter.dart
index d1ad8ed..c10cc3a 100644
--- a/packages/flutter/lib/src/services/text_formatter.dart
+++ b/packages/flutter/lib/src/services/text_formatter.dart
@@ -415,7 +415,8 @@
/// {@macro flutter.services.textFormatter.maxLengthEnforcement}
final MaxLengthEnforcement? maxLengthEnforcement;
- /// Return an effective [MaxLengthEnforcement] according the target platform.
+ /// Returns a [MaxLengthEnforcement] that follows the specified [platform]'s
+ /// convention.
///
/// {@template flutter.services.textFormatter.effectiveMaxLengthEnforcement}
/// ### Platform specific behaviors
@@ -432,11 +433,13 @@
/// [MaxLengthEnforcement.truncateAfterCompositionEnds]. These platforms
/// allow the composition to exceed by default.
/// {@endtemplate}
- static MaxLengthEnforcement get inferredDefaultMaxLengthEnforcement {
+ static MaxLengthEnforcement getDefaultMaxLengthEnforcement([
+ TargetPlatform? platform,
+ ]) {
if (kIsWeb) {
return MaxLengthEnforcement.truncateAfterCompositionEnds;
} else {
- switch (defaultTargetPlatform) {
+ switch (platform ?? defaultTargetPlatform) {
case TargetPlatform.android:
case TargetPlatform.windows:
return MaxLengthEnforcement.enforced;
@@ -493,7 +496,7 @@
assert(maxLength > 0);
- switch (maxLengthEnforcement ?? inferredDefaultMaxLengthEnforcement) {
+ switch (maxLengthEnforcement ?? getDefaultMaxLengthEnforcement()) {
case MaxLengthEnforcement.none:
return newValue;
case MaxLengthEnforcement.enforced:
diff --git a/packages/flutter/test/services/text_formatter_test.dart b/packages/flutter/test/services/text_formatter_test.dart
index 0458ce9..b5cb9dd 100644
--- a/packages/flutter/test/services/text_formatter_test.dart
+++ b/packages/flutter/test/services/text_formatter_test.dart
@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
@@ -460,6 +461,28 @@
expect(formatted.text, 'bbbbbbbbbb');
});
});
+
+ group('get enforcement from target platform', () {
+ // The enforcement on Web will be always `MaxLengthEnforcement.truncateAfterCompositionEnds`
+
+ test('with TargetPlatform.windows', () async {
+ final MaxLengthEnforcement enforcement = LengthLimitingTextInputFormatter.getDefaultMaxLengthEnforcement(
+ TargetPlatform.windows,
+ );
+ if (kIsWeb) {
+ expect(enforcement, MaxLengthEnforcement.truncateAfterCompositionEnds);
+ } else {
+ expect(enforcement, MaxLengthEnforcement.enforced);
+ }
+ });
+
+ test('with TargetPlatform.macOS', () async {
+ final MaxLengthEnforcement enforcement = LengthLimitingTextInputFormatter.getDefaultMaxLengthEnforcement(
+ TargetPlatform.macOS,
+ );
+ expect(enforcement, MaxLengthEnforcement.truncateAfterCompositionEnds);
+ });
+ });
});
test('FilteringTextInputFormatter should return the old value if new value contains non-white-listed character', () {