Deprecate maxLengthEnforced
for text fields (#72043)
This commit is contained in:
parent
a76bb1a08e
commit
63062a6443
@ -262,6 +262,11 @@ class CupertinoTextField extends StatefulWidget {
|
|||||||
this.minLines,
|
this.minLines,
|
||||||
this.expands = false,
|
this.expands = false,
|
||||||
this.maxLength,
|
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.maxLengthEnforced = true,
|
||||||
this.maxLengthEnforcement,
|
this.maxLengthEnforcement,
|
||||||
this.onChanged,
|
this.onChanged,
|
||||||
@ -409,6 +414,11 @@ class CupertinoTextField extends StatefulWidget {
|
|||||||
this.minLines,
|
this.minLines,
|
||||||
this.expands = false,
|
this.expands = false,
|
||||||
this.maxLength,
|
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.maxLengthEnforced = true,
|
||||||
this.maxLengthEnforcement,
|
this.maxLengthEnforcement,
|
||||||
this.onChanged,
|
this.onChanged,
|
||||||
@ -654,6 +664,11 @@ class CupertinoTextField extends StatefulWidget {
|
|||||||
///
|
///
|
||||||
/// If true, prevents the field from allowing more than [maxLength]
|
/// If true, prevents the field from allowing more than [maxLength]
|
||||||
/// characters.
|
/// 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;
|
final bool maxLengthEnforced;
|
||||||
|
|
||||||
/// Determines how the [maxLength] limit should be enforced.
|
/// 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());
|
FocusNode get _effectiveFocusNode => widget.focusNode ?? (_focusNode ??= FocusNode());
|
||||||
|
|
||||||
MaxLengthEnforcement get _effectiveMaxLengthEnforcement => widget.maxLengthEnforcement
|
MaxLengthEnforcement get _effectiveMaxLengthEnforcement => widget.maxLengthEnforcement
|
||||||
?? LengthLimitingTextInputFormatter.inferredDefaultMaxLengthEnforcement;
|
?? LengthLimitingTextInputFormatter.getDefaultMaxLengthEnforcement();
|
||||||
|
|
||||||
bool _showSelectionHandles = false;
|
bool _showSelectionHandles = false;
|
||||||
|
|
||||||
|
@ -359,6 +359,11 @@ class TextField extends StatefulWidget {
|
|||||||
this.minLines,
|
this.minLines,
|
||||||
this.expands = false,
|
this.expands = false,
|
||||||
this.maxLength,
|
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.maxLengthEnforced = true,
|
||||||
this.maxLengthEnforcement,
|
this.maxLengthEnforcement,
|
||||||
this.onChanged,
|
this.onChanged,
|
||||||
@ -606,6 +611,11 @@ class TextField extends StatefulWidget {
|
|||||||
///
|
///
|
||||||
/// If true, prevents the field from allowing more than [maxLength]
|
/// If true, prevents the field from allowing more than [maxLength]
|
||||||
/// characters.
|
/// 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;
|
final bool maxLengthEnforced;
|
||||||
|
|
||||||
/// Determines how the [maxLength] limit should be enforced.
|
/// 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());
|
FocusNode get _effectiveFocusNode => widget.focusNode ?? (_focusNode ??= FocusNode());
|
||||||
|
|
||||||
MaxLengthEnforcement get _effectiveMaxLengthEnforcement => widget.maxLengthEnforcement
|
MaxLengthEnforcement get _effectiveMaxLengthEnforcement => widget.maxLengthEnforcement
|
||||||
?? LengthLimitingTextInputFormatter.inferredDefaultMaxLengthEnforcement;
|
?? LengthLimitingTextInputFormatter.getDefaultMaxLengthEnforcement(Theme.of(context).platform);
|
||||||
|
|
||||||
bool _isHovering = false;
|
bool _isHovering = false;
|
||||||
|
|
||||||
|
@ -165,6 +165,11 @@ class TextFormField extends FormField<String> {
|
|||||||
'This feature was deprecated after v1.19.0.'
|
'This feature was deprecated after v1.19.0.'
|
||||||
)
|
)
|
||||||
bool autovalidate = false,
|
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,
|
bool maxLengthEnforced = true,
|
||||||
MaxLengthEnforcement? maxLengthEnforcement,
|
MaxLengthEnforcement? maxLengthEnforcement,
|
||||||
int? maxLines = 1,
|
int? maxLines = 1,
|
||||||
|
@ -415,7 +415,8 @@ class LengthLimitingTextInputFormatter extends TextInputFormatter {
|
|||||||
/// {@macro flutter.services.textFormatter.maxLengthEnforcement}
|
/// {@macro flutter.services.textFormatter.maxLengthEnforcement}
|
||||||
final MaxLengthEnforcement? 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}
|
/// {@template flutter.services.textFormatter.effectiveMaxLengthEnforcement}
|
||||||
/// ### Platform specific behaviors
|
/// ### Platform specific behaviors
|
||||||
@ -432,11 +433,13 @@ class LengthLimitingTextInputFormatter extends TextInputFormatter {
|
|||||||
/// [MaxLengthEnforcement.truncateAfterCompositionEnds]. These platforms
|
/// [MaxLengthEnforcement.truncateAfterCompositionEnds]. These platforms
|
||||||
/// allow the composition to exceed by default.
|
/// allow the composition to exceed by default.
|
||||||
/// {@endtemplate}
|
/// {@endtemplate}
|
||||||
static MaxLengthEnforcement get inferredDefaultMaxLengthEnforcement {
|
static MaxLengthEnforcement getDefaultMaxLengthEnforcement([
|
||||||
|
TargetPlatform? platform,
|
||||||
|
]) {
|
||||||
if (kIsWeb) {
|
if (kIsWeb) {
|
||||||
return MaxLengthEnforcement.truncateAfterCompositionEnds;
|
return MaxLengthEnforcement.truncateAfterCompositionEnds;
|
||||||
} else {
|
} else {
|
||||||
switch (defaultTargetPlatform) {
|
switch (platform ?? defaultTargetPlatform) {
|
||||||
case TargetPlatform.android:
|
case TargetPlatform.android:
|
||||||
case TargetPlatform.windows:
|
case TargetPlatform.windows:
|
||||||
return MaxLengthEnforcement.enforced;
|
return MaxLengthEnforcement.enforced;
|
||||||
@ -493,7 +496,7 @@ class LengthLimitingTextInputFormatter extends TextInputFormatter {
|
|||||||
|
|
||||||
assert(maxLength > 0);
|
assert(maxLength > 0);
|
||||||
|
|
||||||
switch (maxLengthEnforcement ?? inferredDefaultMaxLengthEnforcement) {
|
switch (maxLengthEnforcement ?? getDefaultMaxLengthEnforcement()) {
|
||||||
case MaxLengthEnforcement.none:
|
case MaxLengthEnforcement.none:
|
||||||
return newValue;
|
return newValue;
|
||||||
case MaxLengthEnforcement.enforced:
|
case MaxLengthEnforcement.enforced:
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
// Use of this source code is governed by a BSD-style license that can be
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
|
||||||
@ -460,6 +461,28 @@ void main() {
|
|||||||
expect(formatted.text, 'bbbbbbbbbb');
|
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', () {
|
test('FilteringTextInputFormatter should return the old value if new value contains non-white-listed character', () {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user