Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
c412fd99
Unverified
Commit
c412fd99
authored
Sep 25, 2020
by
LongCatIsLooong
Committed by
GitHub
Sep 25, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Length formatter minor fix (#66567)
parent
da1a42b5
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
19 deletions
+53
-19
text_formatter.dart
packages/flutter/lib/src/services/text_formatter.dart
+16
-19
editable_text_test.dart
packages/flutter/test/widgets/editable_text_test.dart
+37
-0
No files found.
packages/flutter/lib/src/services/text_formatter.dart
View file @
c412fd99
...
...
@@ -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
);
}
}
...
...
packages/flutter/test/widgets/editable_text_test.dart
View file @
c412fd99
...
...
@@ -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
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment