Skip to content

Commit 06fb863

Browse files
authored
Revert "Remove deprecated CupertinoTextField, TextField, TextFormField maxLengthEnforced" (#99768)
1 parent df7f05f commit 06fb863

4 files changed

Lines changed: 97 additions & 24 deletions

File tree

packages/flutter/lib/src/cupertino/text_field.dart

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,10 @@ class CupertinoTextField extends StatefulWidget {
194194
/// must not be null.
195195
///
196196
/// The [autocorrect], [autofocus], [clearButtonMode], [dragStartBehavior],
197-
/// [expands], [obscureText], [prefixMode], [readOnly], [scrollPadding],
198-
/// [suffixMode], [textAlign], [selectionHeightStyle], [selectionWidthStyle],
199-
/// [enableSuggestions], and [enableIMEPersonalizedLearning] properties must
200-
/// not be null.
197+
/// [expands], [maxLengthEnforced], [obscureText], [prefixMode], [readOnly],
198+
/// [scrollPadding], [suffixMode], [textAlign], [selectionHeightStyle],
199+
/// [selectionWidthStyle], [enableSuggestions], and [enableIMEPersonalizedLearning]
200+
/// properties must not be null.
201201
///
202202
/// See also:
203203
///
@@ -244,6 +244,12 @@ class CupertinoTextField extends StatefulWidget {
244244
this.minLines,
245245
this.expands = false,
246246
this.maxLength,
247+
@Deprecated(
248+
'Use maxLengthEnforcement parameter which provides more specific '
249+
'behavior related to the maxLength limit. '
250+
'This feature was deprecated after v1.25.0-5.0.pre.',
251+
)
252+
this.maxLengthEnforced = true,
247253
this.maxLengthEnforcement,
248254
this.onChanged,
249255
this.onEditingComplete,
@@ -278,6 +284,11 @@ class CupertinoTextField extends StatefulWidget {
278284
smartDashesType = smartDashesType ?? (obscureText ? SmartDashesType.disabled : SmartDashesType.enabled),
279285
smartQuotesType = smartQuotesType ?? (obscureText ? SmartQuotesType.disabled : SmartQuotesType.enabled),
280286
assert(enableSuggestions != null),
287+
assert(maxLengthEnforced != null),
288+
assert(
289+
maxLengthEnforced || maxLengthEnforcement == null,
290+
'maxLengthEnforced is deprecated, use only maxLengthEnforcement',
291+
),
281292
assert(scrollPadding != null),
282293
assert(dragStartBehavior != null),
283294
assert(selectionHeightStyle != null),
@@ -359,9 +370,9 @@ class CupertinoTextField extends StatefulWidget {
359370
/// must not be null.
360371
///
361372
/// The [autocorrect], [autofocus], [clearButtonMode], [dragStartBehavior],
362-
/// [expands], [obscureText], [prefixMode], [readOnly], [scrollPadding],
363-
/// [suffixMode], [textAlign], [selectionHeightStyle], [selectionWidthStyle],
364-
/// and [enableSuggestions] properties must not be null.
373+
/// [expands], [maxLengthEnforced], [obscureText], [prefixMode], [readOnly],
374+
/// [scrollPadding], [suffixMode], [textAlign], [selectionHeightStyle],
375+
/// [selectionWidthStyle], and [enableSuggestions] properties must not be null.
365376
///
366377
/// See also:
367378
///
@@ -405,6 +416,12 @@ class CupertinoTextField extends StatefulWidget {
405416
this.minLines,
406417
this.expands = false,
407418
this.maxLength,
419+
@Deprecated(
420+
'Use maxLengthEnforcement parameter which provides more specific '
421+
'behavior related to the maxLength limit. '
422+
'This feature was deprecated after v1.25.0-5.0.pre.',
423+
)
424+
this.maxLengthEnforced = true,
408425
this.maxLengthEnforcement,
409426
this.onChanged,
410427
this.onEditingComplete,
@@ -439,6 +456,11 @@ class CupertinoTextField extends StatefulWidget {
439456
smartDashesType = smartDashesType ?? (obscureText ? SmartDashesType.disabled : SmartDashesType.enabled),
440457
smartQuotesType = smartQuotesType ?? (obscureText ? SmartQuotesType.disabled : SmartQuotesType.enabled),
441458
assert(enableSuggestions != null),
459+
assert(maxLengthEnforced != null),
460+
assert(
461+
maxLengthEnforced || maxLengthEnforcement == null,
462+
'maxLengthEnforced is deprecated, use only maxLengthEnforcement',
463+
),
442464
assert(scrollPadding != null),
443465
assert(dragStartBehavior != null),
444466
assert(selectionHeightStyle != null),
@@ -666,6 +688,18 @@ class CupertinoTextField extends StatefulWidget {
666688
/// {@macro flutter.services.lengthLimitingTextInputFormatter.maxLength}
667689
final int? maxLength;
668690

691+
/// If [maxLength] is set, [maxLengthEnforced] indicates whether or not to
692+
/// enforce the limit.
693+
///
694+
/// If true, prevents the field from allowing more than [maxLength]
695+
/// characters.
696+
@Deprecated(
697+
'Use maxLengthEnforcement parameter which provides more specific '
698+
'behavior related to the maxLength limit. '
699+
'This feature was deprecated after v1.25.0-5.0.pre.',
700+
)
701+
final bool maxLengthEnforced;
702+
669703
/// Determines how the [maxLength] limit should be enforced.
670704
///
671705
/// If [MaxLengthEnforcement.none] is set, additional input beyond [maxLength]
@@ -804,6 +838,7 @@ class CupertinoTextField extends StatefulWidget {
804838
properties.add(IntProperty('minLines', minLines, defaultValue: null));
805839
properties.add(DiagnosticsProperty<bool>('expands', expands, defaultValue: false));
806840
properties.add(IntProperty('maxLength', maxLength, defaultValue: null));
841+
properties.add(FlagProperty('maxLengthEnforced', value: maxLengthEnforced, ifTrue: 'max length enforced'));
807842
properties.add(EnumProperty<MaxLengthEnforcement>('maxLengthEnforcement', maxLengthEnforcement, defaultValue: null));
808843
properties.add(DoubleProperty('cursorWidth', cursorWidth, defaultValue: 2.0));
809844
properties.add(DoubleProperty('cursorHeight', cursorHeight, defaultValue: null));
@@ -1157,7 +1192,7 @@ class _CupertinoTextFieldState extends State<CupertinoTextField> with Restoratio
11571192
final Offset cursorOffset = Offset(_iOSHorizontalCursorOffsetPixels / MediaQuery.of(context).devicePixelRatio, 0);
11581193
final List<TextInputFormatter> formatters = <TextInputFormatter>[
11591194
...?widget.inputFormatters,
1160-
if (widget.maxLength != null)
1195+
if (widget.maxLength != null && widget.maxLengthEnforced)
11611196
LengthLimitingTextInputFormatter(
11621197
widget.maxLength,
11631198
maxLengthEnforcement: _effectiveMaxLengthEnforcement,

packages/flutter/lib/src/material/text_field.dart

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,9 @@ class TextField extends StatefulWidget {
251251
/// which is evaluated after the supplied [inputFormatters], if any.
252252
/// The [maxLength] value must be either null or greater than zero.
253253
///
254-
/// If [maxLengthEnforcement] is set to [MaxLengthEnforcement.none], then more
255-
/// than [maxLength] characters may be entered, and the error counter and
256-
/// divider will switch to the [decoration].errorStyle when the limit is
257-
/// exceeded.
254+
/// If [maxLengthEnforced] is set to false, then more than [maxLength]
255+
/// characters may be entered, and the error counter and divider will
256+
/// switch to the [decoration].errorStyle when the limit is exceeded.
258257
///
259258
/// The text cursor is not shown if [showCursor] is false or if [showCursor]
260259
/// is null (the default) and [readOnly] is true.
@@ -265,8 +264,8 @@ class TextField extends StatefulWidget {
265264
/// must not be null.
266265
///
267266
/// The [textAlign], [autofocus], [obscureText], [readOnly], [autocorrect],
268-
/// [scrollPadding], [maxLines], [maxLength], [selectionHeightStyle],
269-
/// [selectionWidthStyle], [enableSuggestions], and
267+
/// [maxLengthEnforced], [scrollPadding], [maxLines], [maxLength],
268+
/// [selectionHeightStyle], [selectionWidthStyle], [enableSuggestions], and
270269
/// [enableIMEPersonalizedLearning] arguments must not be null.
271270
///
272271
/// See also:
@@ -300,6 +299,12 @@ class TextField extends StatefulWidget {
300299
this.minLines,
301300
this.expands = false,
302301
this.maxLength,
302+
@Deprecated(
303+
'Use maxLengthEnforcement parameter which provides more specific '
304+
'behavior related to the maxLength limit. '
305+
'This feature was deprecated after v1.25.0-5.0.pre.',
306+
)
307+
this.maxLengthEnforced = true,
303308
this.maxLengthEnforcement,
304309
this.onChanged,
305310
this.onEditingComplete,
@@ -337,6 +342,11 @@ class TextField extends StatefulWidget {
337342
smartDashesType = smartDashesType ?? (obscureText ? SmartDashesType.disabled : SmartDashesType.enabled),
338343
smartQuotesType = smartQuotesType ?? (obscureText ? SmartQuotesType.disabled : SmartQuotesType.enabled),
339344
assert(enableSuggestions != null),
345+
assert(maxLengthEnforced != null),
346+
assert(
347+
maxLengthEnforced || maxLengthEnforcement == null,
348+
'maxLengthEnforced is deprecated, use only maxLengthEnforcement',
349+
),
340350
assert(scrollPadding != null),
341351
assert(dragStartBehavior != null),
342352
assert(selectionHeightStyle != null),
@@ -550,14 +560,28 @@ class TextField extends StatefulWidget {
550560
/// Whitespace characters (e.g. newline, space, tab) are included in the
551561
/// character count.
552562
///
553-
/// If [maxLengthEnforcement] is [MaxLengthEnforcement.none], then more than
554-
/// [maxLength] characters may be entered, but the error counter and divider
555-
/// will switch to the [decoration]'s [InputDecoration.errorStyle] when the
556-
/// limit is exceeded.
563+
/// If [maxLengthEnforced] is set to false or [maxLengthEnforcement] is
564+
/// [MaxLengthEnforcement.none], then more than [maxLength]
565+
/// characters may be entered, but the error counter and divider will switch
566+
/// to the [decoration]'s [InputDecoration.errorStyle] when the limit is
567+
/// exceeded.
557568
///
558569
/// {@macro flutter.services.lengthLimitingTextInputFormatter.maxLength}
559570
final int? maxLength;
560571

572+
/// If [maxLength] is set, [maxLengthEnforced] indicates whether or not to
573+
/// enforce the limit, or merely provide a character counter and warning when
574+
/// [maxLength] is exceeded.
575+
///
576+
/// If true, prevents the field from allowing more than [maxLength]
577+
/// characters.
578+
@Deprecated(
579+
'Use maxLengthEnforcement parameter which provides more specific '
580+
'behavior related to the maxLength limit. '
581+
'This feature was deprecated after v1.25.0-5.0.pre.',
582+
)
583+
final bool maxLengthEnforced;
584+
561585
/// Determines how the [maxLength] limit should be enforced.
562586
///
563587
/// {@macro flutter.services.textFormatter.effectiveMaxLengthEnforcement}
@@ -789,6 +813,7 @@ class TextField extends StatefulWidget {
789813
properties.add(IntProperty('minLines', minLines, defaultValue: null));
790814
properties.add(DiagnosticsProperty<bool>('expands', expands, defaultValue: false));
791815
properties.add(IntProperty('maxLength', maxLength, defaultValue: null));
816+
properties.add(FlagProperty('maxLengthEnforced', value: maxLengthEnforced, defaultValue: true, ifFalse: 'maxLength not enforced'));
792817
properties.add(EnumProperty<MaxLengthEnforcement>('maxLengthEnforcement', maxLengthEnforcement, defaultValue: null));
793818
properties.add(EnumProperty<TextInputAction>('textInputAction', textInputAction, defaultValue: null));
794819
properties.add(EnumProperty<TextCapitalization>('textCapitalization', textCapitalization, defaultValue: TextCapitalization.none));
@@ -1124,7 +1149,7 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements
11241149
final FocusNode focusNode = _effectiveFocusNode;
11251150
final List<TextInputFormatter> formatters = <TextInputFormatter>[
11261151
...?widget.inputFormatters,
1127-
if (widget.maxLength != null)
1152+
if (widget.maxLength != null && widget.maxLengthEnforced)
11281153
LengthLimitingTextInputFormatter(
11291154
widget.maxLength,
11301155
maxLengthEnforcement: _effectiveMaxLengthEnforcement,
@@ -1304,7 +1329,8 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements
13041329
);
13051330

13061331
final int? semanticsMaxValueLength;
1307-
if (_effectiveMaxLengthEnforcement != MaxLengthEnforcement.none &&
1332+
if (widget.maxLengthEnforced &&
1333+
_effectiveMaxLengthEnforcement != MaxLengthEnforcement.none &&
13081334
widget.maxLength != null &&
13091335
widget.maxLength! > 0) {
13101336
semanticsMaxValueLength = widget.maxLength;

packages/flutter/lib/src/material/text_form_field.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ class TextFormField extends FormField<String> {
118118
SmartDashesType? smartDashesType,
119119
SmartQuotesType? smartQuotesType,
120120
bool enableSuggestions = true,
121+
@Deprecated(
122+
'Use maxLengthEnforcement parameter which provides more specific '
123+
'behavior related to the maxLength limit. '
124+
'This feature was deprecated after v1.25.0-5.0.pre.',
125+
)
126+
bool maxLengthEnforced = true,
121127
MaxLengthEnforcement? maxLengthEnforcement,
122128
int? maxLines = 1,
123129
int? minLines,
@@ -154,6 +160,11 @@ class TextFormField extends FormField<String> {
154160
assert(obscureText != null),
155161
assert(autocorrect != null),
156162
assert(enableSuggestions != null),
163+
assert(maxLengthEnforced != null),
164+
assert(
165+
maxLengthEnforced || maxLengthEnforcement == null,
166+
'maxLengthEnforced is deprecated, use only maxLengthEnforcement',
167+
),
157168
assert(scrollPadding != null),
158169
assert(maxLines == null || maxLines > 0),
159170
assert(minLines == null || minLines > 0),
@@ -212,6 +223,7 @@ class TextFormField extends FormField<String> {
212223
smartDashesType: smartDashesType ?? (obscureText ? SmartDashesType.disabled : SmartDashesType.enabled),
213224
smartQuotesType: smartQuotesType ?? (obscureText ? SmartQuotesType.disabled : SmartQuotesType.enabled),
214225
enableSuggestions: enableSuggestions,
226+
maxLengthEnforced: maxLengthEnforced,
215227
maxLengthEnforcement: maxLengthEnforcement,
216228
maxLines: maxLines,
217229
minLines: minLines,

packages/flutter/test/material/text_field_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4290,7 +4290,7 @@ void main() {
42904290
expect(textController.text, '#一#二#三#四#五');
42914291
});
42924292

4293-
testWidgets("maxLength isn't enforced when maxLengthEnforcement.none.", (WidgetTester tester) async {
4293+
testWidgets("maxLength isn't enforced when maxLengthEnforced is false.", (WidgetTester tester) async {
42944294
final TextEditingController textController = TextEditingController();
42954295

42964296
await tester.pumpWidget(boilerplate(
@@ -4305,7 +4305,7 @@ void main() {
43054305
expect(textController.text, '0123456789101112');
43064306
});
43074307

4308-
testWidgets('maxLength shows warning when maxLengthEnforcement.none.', (WidgetTester tester) async {
4308+
testWidgets('maxLength shows warning when maxLengthEnforced is false.', (WidgetTester tester) async {
43094309
final TextEditingController textController = TextEditingController();
43104310
const TextStyle testStyle = TextStyle(color: Colors.deepPurpleAccent);
43114311

@@ -4335,7 +4335,7 @@ void main() {
43354335
expect(counterTextWidget.style!.color, isNot(equals(Colors.deepPurpleAccent)));
43364336
});
43374337

4338-
testWidgets('maxLength shows warning when maxLengthEnforcement.none with surrogate pairs.', (WidgetTester tester) async {
4338+
testWidgets('maxLength shows warning when maxLengthEnforced is false with surrogate pairs.', (WidgetTester tester) async {
43394339
final TextEditingController textController = TextEditingController();
43404340
const TextStyle testStyle = TextStyle(color: Colors.deepPurpleAccent);
43414341

@@ -4365,7 +4365,7 @@ void main() {
43654365
expect(counterTextWidget.style!.color, isNot(equals(Colors.deepPurpleAccent)));
43664366
});
43674367

4368-
testWidgets('maxLength shows warning when maxLengthEnforcement.none with grapheme clusters.', (WidgetTester tester) async {
4368+
testWidgets('maxLength shows warning when maxLengthEnforced is false with grapheme clusters.', (WidgetTester tester) async {
43694369
final TextEditingController textController = TextEditingController();
43704370
const TextStyle testStyle = TextStyle(color: Colors.deepPurpleAccent);
43714371

0 commit comments

Comments
 (0)