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
ee8131b4
Unverified
Commit
ee8131b4
authored
Mar 18, 2020
by
Gary Qian
Committed by
GitHub
Mar 18, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reland formatter changes (#52765)
parent
ee845255
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
350 additions
and
20 deletions
+350
-20
editable_text.dart
packages/flutter/lib/src/widgets/editable_text.dart
+39
-12
editable_text_test.dart
packages/flutter/test/widgets/editable_text_test.dart
+311
-8
No files found.
packages/flutter/lib/src/widgets/editable_text.dart
View file @
ee8131b4
...
...
@@ -1230,6 +1230,9 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
// _lastFormattedUnmodifiedTextEditingValue tracks the last value
// that the formatter ran on and is used to prevent double-formatting.
TextEditingValue
_lastFormattedUnmodifiedTextEditingValue
;
// _lastFormattedValue tracks the last post-format value, so that it can be
// reused without rerunning the formatter when the input value is repeated.
TextEditingValue
_lastFormattedValue
;
// _receivedRemoteTextEditingValue is the direct value last passed in
// updateEditingValue. This value does not get updated with the formatted
// version.
...
...
@@ -1660,15 +1663,30 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
// Check if the new value is the same as the current local value, or is the same
// as the post-formatting value of the previous pass.
final
bool
textChanged
=
_value
?.
text
!=
value
?.
text
;
final
bool
isRepeat
=
value
?.
text
==
_lastFormattedUnmodifiedTextEditingValue
?.
text
;
if
(
textChanged
&&
!
isRepeat
&&
widget
.
inputFormatters
!=
null
&&
widget
.
inputFormatters
.
isNotEmpty
)
{
for
(
final
TextInputFormatter
formatter
in
widget
.
inputFormatters
)
final
bool
isRepeatText
=
value
?.
text
==
_lastFormattedUnmodifiedTextEditingValue
?.
text
;
final
bool
isRepeatSelection
=
value
?.
selection
==
_lastFormattedUnmodifiedTextEditingValue
?.
selection
;
final
bool
isRepeatComposing
=
value
?.
composing
==
_lastFormattedUnmodifiedTextEditingValue
?.
composing
;
// Only format when the text has changed and there are available formatters.
if
(!
isRepeatText
&&
textChanged
&&
widget
.
inputFormatters
!=
null
&&
widget
.
inputFormatters
.
isNotEmpty
)
{
for
(
final
TextInputFormatter
formatter
in
widget
.
inputFormatters
)
{
value
=
formatter
.
formatEditUpdate
(
_value
,
value
);
}
// Always pass the text through the whitespace directionality formatter to
// maintain expected behavior with carets on trailing whitespace.
value
=
_whitespaceFormatter
.
formatEditUpdate
(
_value
,
value
);
_lastFormattedValue
=
value
;
}
// If the text, selection, or composing region has changed, we should update the
// locally stored TextEditingValue to the new one.
if
(!
isRepeatText
||
!
isRepeatSelection
||
!
isRepeatComposing
)
{
_value
=
value
;
_updateRemoteEditingValueIfNeeded
();
}
else
{
_value
=
value
;
}
else
if
(
textChanged
&&
_lastFormattedValue
!=
null
)
{
_value
=
_lastFormattedValue
;
}
// Always attempt to send the value. If the value has changed, then it will send,
// otherwise, it will short-circuit.
_updateRemoteEditingValueIfNeeded
();
if
(
textChanged
&&
widget
.
onChanged
!=
null
)
widget
.
onChanged
(
value
.
text
);
_lastFormattedUnmodifiedTextEditingValue
=
_receivedRemoteTextEditingValue
;
...
...
@@ -2207,14 +2225,22 @@ class _WhitespaceDirectionalityFormatter extends TextInputFormatter {
// We add/subtract from these as we insert/remove markers.
int
selectionBase
=
newValue
.
selection
.
baseOffset
;
int
selectionExtent
=
newValue
.
selection
.
extentOffset
;
int
composingStart
=
newValue
.
composing
.
start
;
int
composingEnd
=
newValue
.
composing
.
end
;
void
addTo
Selection
()
{
void
addTo
Length
()
{
selectionBase
+=
outputCodepoints
.
length
<=
selectionBase
?
1
:
0
;
selectionExtent
+=
outputCodepoints
.
length
<=
selectionExtent
?
1
:
0
;
composingStart
+=
outputCodepoints
.
length
<=
composingStart
?
1
:
0
;
composingEnd
+=
outputCodepoints
.
length
<=
composingEnd
?
1
:
0
;
}
void
subtractFrom
Selection
()
{
void
subtractFrom
Length
()
{
selectionBase
-=
outputCodepoints
.
length
<
selectionBase
?
1
:
0
;
selectionExtent
-=
outputCodepoints
.
length
<
selectionExtent
?
1
:
0
;
composingStart
-=
outputCodepoints
.
length
<
composingStart
?
1
:
0
;
composingEnd
-=
outputCodepoints
.
length
<
composingEnd
?
1
:
0
;
}
bool
previousWasWhitespace
=
false
;
...
...
@@ -2230,11 +2256,11 @@ class _WhitespaceDirectionalityFormatter extends TextInputFormatter {
// If we already added directionality for this run of whitespace,
// "shift" the marker added to the end of the whitespace run.
if
(
previousWasWhitespace
)
{
subtractFrom
Selection
();
subtractFrom
Length
();
outputCodepoints
.
removeLast
();
}
outputCodepoints
.
add
(
codepoint
);
addTo
Selection
();
addTo
Length
();
outputCodepoints
.
add
(
_previousNonWhitespaceDirection
==
TextDirection
.
rtl
?
_rlm
:
_lrm
);
previousWasWhitespace
=
true
;
...
...
@@ -2243,7 +2269,7 @@ class _WhitespaceDirectionalityFormatter extends TextInputFormatter {
// Handle pre-existing directionality markers. Use pre-existing marker
// instead of the one we add.
if
(
previousWasWhitespace
)
{
subtractFrom
Selection
();
subtractFrom
Length
();
outputCodepoints
.
removeLast
();
}
outputCodepoints
.
add
(
codepoint
);
...
...
@@ -2256,7 +2282,7 @@ class _WhitespaceDirectionalityFormatter extends TextInputFormatter {
if
(!
previousWasDirectionalityMarker
&&
previousWasWhitespace
&&
getDirection
(
codepoint
)
==
_previousNonWhitespaceDirection
)
{
subtractFrom
Selection
();
subtractFrom
Length
();
outputCodepoints
.
removeLast
();
}
// Normal character, track its codepoint add it to the string.
...
...
@@ -2276,6 +2302,7 @@ class _WhitespaceDirectionalityFormatter extends TextInputFormatter {
affinity:
newValue
.
selection
.
affinity
,
isDirectional:
newValue
.
selection
.
isDirectional
),
composing:
TextRange
(
start:
composingStart
,
end:
composingEnd
),
);
}
return
newValue
;
...
...
packages/flutter/test/widgets/editable_text_test.dart
View file @
ee8131b4
This diff is collapsed.
Click to expand it.
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