Unverified Commit 63062a64 authored by Alex Li's avatar Alex Li Committed by GitHub

Deprecate `maxLengthEnforced` for text fields (#72043)

parent a76bb1a0
......@@ -262,6 +262,11 @@ class CupertinoTextField extends StatefulWidget {
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 @@ class CupertinoTextField extends StatefulWidget {
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 @@ class CupertinoTextField extends StatefulWidget {
///
/// 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 @@ class _CupertinoTextFieldState extends State<CupertinoTextField> with Restoratio
FocusNode get _effectiveFocusNode => widget.focusNode ?? (_focusNode ??= FocusNode());
MaxLengthEnforcement get _effectiveMaxLengthEnforcement => widget.maxLengthEnforcement
?? LengthLimitingTextInputFormatter.inferredDefaultMaxLengthEnforcement;
?? LengthLimitingTextInputFormatter.getDefaultMaxLengthEnforcement();
bool _showSelectionHandles = false;
......
......@@ -359,6 +359,11 @@ class TextField extends StatefulWidget {
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 @@ class TextField extends StatefulWidget {
///
/// 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 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements
FocusNode get _effectiveFocusNode => widget.focusNode ?? (_focusNode ??= FocusNode());
MaxLengthEnforcement get _effectiveMaxLengthEnforcement => widget.maxLengthEnforcement
?? LengthLimitingTextInputFormatter.inferredDefaultMaxLengthEnforcement;
?? LengthLimitingTextInputFormatter.getDefaultMaxLengthEnforcement(Theme.of(context).platform);
bool _isHovering = false;
......
......@@ -165,6 +165,11 @@ class TextFormField extends FormField<String> {
'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,
......
......@@ -415,7 +415,8 @@ class LengthLimitingTextInputFormatter extends TextInputFormatter {
/// {@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 @@ class LengthLimitingTextInputFormatter extends TextInputFormatter {
/// [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 @@ class LengthLimitingTextInputFormatter extends TextInputFormatter {
assert(maxLength > 0);
switch (maxLengthEnforcement ?? inferredDefaultMaxLengthEnforcement) {
switch (maxLengthEnforcement ?? getDefaultMaxLengthEnforcement()) {
case MaxLengthEnforcement.none:
return newValue;
case MaxLengthEnforcement.enforced:
......
......@@ -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 @@ void main() {
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', () {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment