Skip to content

Commit 7d53904

Browse files
dont mess with user supplied list (flutter#69594)
1 parent 88809aa commit 7d53904

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -878,13 +878,13 @@ class _CupertinoTextFieldState extends State<CupertinoTextField> with Restoratio
878878
super.build(context); // See AutomaticKeepAliveClientMixin.
879879
assert(debugCheckHasDirectionality(context));
880880
final TextEditingController controller = _effectiveController;
881-
final List<TextInputFormatter> formatters = widget.inputFormatters ?? <TextInputFormatter>[];
882881
final TextSelectionControls textSelectionControls = widget.selectionControls ?? cupertinoTextSelectionControls;
883882
final bool enabled = widget.enabled ?? true;
884883
final Offset cursorOffset = Offset(_iOSHorizontalCursorOffsetPixels / MediaQuery.of(context).devicePixelRatio, 0);
885-
if (widget.maxLength != null && widget.maxLengthEnforced) {
886-
formatters.add(LengthLimitingTextInputFormatter(widget.maxLength));
887-
}
884+
final List<TextInputFormatter> formatters = <TextInputFormatter>[
885+
...?widget.inputFormatters,
886+
if (widget.maxLength != null && widget.maxLengthEnforced) LengthLimitingTextInputFormatter(widget.maxLength)
887+
];
888888
final CupertinoThemeData themeData = CupertinoTheme.of(context);
889889

890890
final TextStyle? resolvedStyle = widget.style?.copyWith(

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,9 +1261,10 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements
12611261
final Brightness keyboardAppearance = widget.keyboardAppearance ?? theme.primaryColorBrightness;
12621262
final TextEditingController controller = _effectiveController;
12631263
final FocusNode focusNode = _effectiveFocusNode;
1264-
final List<TextInputFormatter> formatters = widget.inputFormatters ?? <TextInputFormatter>[];
1265-
if (widget.maxLength != null && widget.maxLengthEnforced)
1266-
formatters.add(LengthLimitingTextInputFormatter(widget.maxLength));
1264+
final List<TextInputFormatter> formatters = <TextInputFormatter>[
1265+
...?widget.inputFormatters,
1266+
if (widget.maxLength != null && widget.maxLengthEnforced) LengthLimitingTextInputFormatter(widget.maxLength)
1267+
];
12671268

12681269
TextSelectionControls? textSelectionControls = widget.selectionControls;
12691270
final bool paintCursorAboveText;

packages/flutter/test/cupertino/text_field_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4289,4 +4289,16 @@ void main() {
42894289
final EditableText widget = tester.widget(find.byType(EditableText));
42904290
expect(widget.selectionControls, equals(selectionControl));
42914291
});
4292+
4293+
testWidgets('Do not add LengthLimiting formatter to the user supplied list', (WidgetTester tester) async {
4294+
final List<TextInputFormatter> formatters = <TextInputFormatter>[];
4295+
4296+
await tester.pumpWidget(
4297+
CupertinoApp(
4298+
home: CupertinoTextField(maxLength: 5, inputFormatters: formatters),
4299+
)
4300+
);
4301+
4302+
expect(formatters.isEmpty, isTrue);
4303+
});
42924304
}

packages/flutter/test/material/text_field_test.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3344,6 +3344,22 @@ void main() {
33443344
expect(textController.text, '145623');
33453345
});
33463346

3347+
testWidgets('Do not add LengthLimiting formatter to the user supplied list', (WidgetTester tester) async {
3348+
final List<TextInputFormatter> formatters = <TextInputFormatter>[];
3349+
3350+
await tester.pumpWidget(
3351+
overlay(
3352+
child: TextField(
3353+
decoration: null,
3354+
maxLength: 5,
3355+
inputFormatters: formatters,
3356+
),
3357+
),
3358+
);
3359+
3360+
expect(formatters.isEmpty, isTrue);
3361+
});
3362+
33473363
testWidgets('Text field scrolls the caret into view', (WidgetTester tester) async {
33483364
final TextEditingController controller = TextEditingController();
33493365

0 commit comments

Comments
 (0)