fix length formatter typo (#79489)

This commit is contained in:
LongCatIsLooong
2021-04-01 00:14:04 -07:00
committed by GitHub
parent 5740a62949
commit 7de099bded
2 changed files with 85 additions and 1 deletions

View File

@@ -502,7 +502,7 @@ class LengthLimitingTextInputFormatter extends TextInputFormatter {
case MaxLengthEnforcement.enforced:
// If already at the maximum and tried to enter even more, and has no
// selection, keep the old value.
if (oldValue.text.characters.length == maxLength && !oldValue.selection.isValid) {
if (oldValue.text.characters.length == maxLength && oldValue.selection.isCollapsed) {
return oldValue;
}

View File

@@ -3864,6 +3864,90 @@ void main() {
expect(textController.text, testValue);
});
testWidgets(
'maxLength limits input in the center of a maxed-out field, with collapsed selection',
(WidgetTester tester) async {
final TextEditingController textController = TextEditingController();
const String testValue = '0123456789';
await tester.pumpWidget(boilerplate(
child: TextField(
controller: textController,
maxLength: 10,
),
));
// Max out the character limit in the field.
await tester.showKeyboard(find.byType(TextField));
tester.testTextInput.updateEditingValue(const TextEditingValue(
text: testValue,
selection: TextSelection.collapsed(offset: 10),
composing: TextRange.empty,
));
await tester.pump();
expect(textController.text, testValue);
// Entering more characters at the end does nothing.
await tester.showKeyboard(find.byType(TextField));
tester.testTextInput.updateEditingValue(const TextEditingValue(
text: testValue + '9999999',
selection: TextSelection.collapsed(offset: 10 + 7),
composing: TextRange.empty,
));
await tester.pump();
expect(textController.text, testValue);
// Entering text in the middle of the field also does nothing.
// Entering more characters at the end does nothing.
await tester.showKeyboard(find.byType(TextField));
tester.testTextInput.updateEditingValue(const TextEditingValue(
text: '0123455555555556789',
selection: TextSelection.collapsed(offset: 19),
composing: TextRange.empty,
));
await tester.pump();
expect(textController.text, testValue);
},
);
testWidgets(
'maxLength limits input in the center of a maxed-out field, with non-collapsed selection',
(WidgetTester tester) async {
final TextEditingController textController = TextEditingController();
const String testValue = '0123456789';
await tester.pumpWidget(boilerplate(
child: TextField(
controller: textController,
maxLength: 10,
),
));
// Max out the character limit in the field.
await tester.showKeyboard(find.byType(TextField));
tester.testTextInput.updateEditingValue(const TextEditingValue(
text: testValue,
selection: TextSelection(baseOffset: 8, extentOffset: 10),
composing: TextRange.empty,
));
await tester.pump();
expect(textController.text, testValue);
// Entering more characters at the end does nothing.
await tester.showKeyboard(find.byType(TextField));
tester.testTextInput.updateEditingValue(const TextEditingValue(
text: '01234569999999',
selection: TextSelection.collapsed(offset: 14),
composing: TextRange.empty,
));
await tester.pump();
expect(textController.text, '0123456999');
},
);
testWidgets('maxLength limits input length even if decoration is null.', (WidgetTester tester) async {
final TextEditingController textController = TextEditingController();