Unverified Commit c412fd99 authored by LongCatIsLooong's avatar LongCatIsLooong Committed by GitHub

Length formatter minor fix (#66567)

parent da1a42b5
......@@ -319,8 +319,8 @@ class LengthLimitingTextInputFormatter extends TextInputFormatter {
/// The limit on the number of characters (i.e. Unicode scalar values) this formatter
/// will allow.
///
/// The value must be null or greater than zero. If it is null, then no limit
/// is enforced.
/// The value must be null or greater than zero. If it is null or -1, then no
/// limit is enforced.
///
/// This formatter does not currently count Unicode grapheme clusters (i.e.
/// characters visible to the user), it counts Unicode scalar values, which leaves
......@@ -379,25 +379,22 @@ class LengthLimitingTextInputFormatter extends TextInputFormatter {
TextEditingValue oldValue,
TextEditingValue newValue,
) {
// Return the new value when the old value has not reached the max
// limit or the old value is composing too.
if (newValue.composing.isValid) {
if (maxLength != null && maxLength! > 0 &&
oldValue.text.characters.length == maxLength! &&
!oldValue.composing.isValid) {
return oldValue;
}
final int? maxLength = this.maxLength;
if (maxLength == null || maxLength == -1 || newValue.text.characters.length <= maxLength)
return newValue;
assert(maxLength > 0);
// If already at the maximum and tried to enter even more, keep the old
// value.
if (oldValue.text.characters.length == maxLength && !oldValue.composing.isValid) {
return oldValue;
}
if (maxLength != null && maxLength! > 0 && newValue.text.characters.length > maxLength!) {
// If already at the maximum and tried to enter even more, keep the old
// value.
if (oldValue.text.characters.length == maxLength) {
return oldValue;
}
return truncate(newValue, maxLength!);
}
return newValue;
// Temporarily exempt `newValue` from the maxLength limit if it has a
// composing text going, until the composing is finished.
return newValue.composing.isValid ? newValue : truncate(newValue, maxLength);
}
}
......
......@@ -5734,6 +5734,43 @@ void main() {
expect(state.currentTextEditingValue.text, '12345');
expect(state.currentTextEditingValue.composing, TextRange.empty);
});
testWidgets('Length formatter handles composing text correctly, continued', (WidgetTester tester) async {
final TextInputFormatter formatter = LengthLimitingTextInputFormatter(5);
final Widget widget = MaterialApp(
home: EditableText(
backgroundCursorColor: Colors.grey,
controller: controller,
focusNode: focusNode,
inputFormatters: <TextInputFormatter>[formatter],
style: textStyle,
cursorColor: cursorColor,
selectionControls: materialTextSelectionControls,
),
);
await tester.pumpWidget(widget);
final EditableTextState state = tester.state<EditableTextState>(find.byType(EditableText));
// Initially we're at maxLength with no composing text.
controller.text = '12345' ;
assert(state.currentTextEditingValue == const TextEditingValue(text: '12345'));
// Should be able to change the editing value if the new value is still shorter
// than maxLength.
state.updateEditingValue(const TextEditingValue(text: '12345', composing: TextRange(start: 2, end: 4)));
expect(state.currentTextEditingValue.composing, const TextRange(start: 2, end: 4));
// Reset.
controller.text = '12345' ;
assert(state.currentTextEditingValue == const TextEditingValue(text: '12345'));
// The text should not change when trying to insert when the text is already
// at maxLength.
state.updateEditingValue(const TextEditingValue(text: 'abcdef', composing: TextRange(start: 5, end: 6)));
expect(state.currentTextEditingValue.text, '12345');
expect(state.currentTextEditingValue.composing, TextRange.empty);
});
}
class MockTextFormatter extends TextInputFormatter {
......
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