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
d5626637
Unverified
Commit
d5626637
authored
Mar 11, 2020
by
Gary Qian
Committed by
GitHub
Mar 11, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reland "Improve text formatter repeat filter logic (#52055)" (#52366)
parent
4841a7ed
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
92 additions
and
14 deletions
+92
-14
editable_text.dart
packages/flutter/lib/src/widgets/editable_text.dart
+20
-6
editable_text_test.dart
packages/flutter/test/widgets/editable_text_test.dart
+72
-8
No files found.
packages/flutter/lib/src/widgets/editable_text.dart
View file @
d5626637
...
...
@@ -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.
...
...
@@ -1656,15 +1659,26 @@ 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
;
// 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
);
}
_lastFormattedValue
=
value
;
}
// If the text has changed or the selection has changed, we should update the
// locally stored TextEditingValue to the new one.
if
(!
isRepeatText
||
!
isRepeatSelection
)
{
_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
;
...
...
packages/flutter/test/widgets/editable_text_test.dart
View file @
d5626637
...
...
@@ -4238,12 +4238,76 @@ void main() {
expect
(
formatter
.
log
,
referenceLog
);
});
testWidgets
(
'formatter logic handles repeat filtering'
,
(
WidgetTester
tester
)
async
{
final
MockTextFormatter
formatter
=
MockTextFormatter
();
await
tester
.
pumpWidget
(
MediaQuery
(
data:
const
MediaQueryData
(
devicePixelRatio:
1.0
),
child:
Directionality
(
textDirection:
TextDirection
.
ltr
,
child:
FocusScope
(
node:
focusScopeNode
,
autofocus:
true
,
child:
EditableText
(
backgroundCursorColor:
Colors
.
grey
,
controller:
controller
,
focusNode:
focusNode
,
maxLines:
1
,
// Sets text keyboard implicitly.
style:
textStyle
,
cursorColor:
cursorColor
,
inputFormatters:
<
TextInputFormatter
>[
formatter
],
),
),
),
),
);
await
tester
.
tap
(
find
.
byType
(
EditableText
));
await
tester
.
showKeyboard
(
find
.
byType
(
EditableText
));
controller
.
text
=
''
;
await
tester
.
idle
();
final
EditableTextState
state
=
tester
.
state
<
EditableTextState
>(
find
.
byType
(
EditableText
));
expect
(
tester
.
testTextInput
.
editingState
[
'text'
],
equals
(
''
));
expect
(
state
.
wantKeepAlive
,
true
);
expect
(
formatter
.
formatCallCount
,
0
);
state
.
updateEditingValue
(
const
TextEditingValue
(
text:
'01'
));
expect
(
formatter
.
formatCallCount
,
1
);
state
.
updateEditingValue
(
const
TextEditingValue
(
text:
'012'
));
expect
(
formatter
.
formatCallCount
,
2
);
state
.
updateEditingValue
(
const
TextEditingValue
(
text:
'0123'
));
// Text change causes reformat
expect
(
formatter
.
formatCallCount
,
3
);
state
.
updateEditingValue
(
const
TextEditingValue
(
text:
'0123'
));
// Repeat, does not format
expect
(
formatter
.
formatCallCount
,
3
);
state
.
updateEditingValue
(
const
TextEditingValue
(
text:
'0123'
));
// Repeat, does not format
expect
(
formatter
.
formatCallCount
,
3
);
state
.
updateEditingValue
(
const
TextEditingValue
(
text:
'0123'
,
selection:
TextSelection
.
collapsed
(
offset:
2
)));
// Selection change does not reformat
expect
(
formatter
.
formatCallCount
,
3
);
state
.
updateEditingValue
(
const
TextEditingValue
(
text:
'0123'
,
selection:
TextSelection
.
collapsed
(
offset:
2
)));
// Repeat, does not format
expect
(
formatter
.
formatCallCount
,
3
);
state
.
updateEditingValue
(
const
TextEditingValue
(
text:
'0123'
,
selection:
TextSelection
.
collapsed
(
offset:
2
)));
// Repeat, does not format
expect
(
formatter
.
formatCallCount
,
3
);
const
List
<
String
>
referenceLog
=
<
String
>[
'[1]: , 01'
,
'[1]: normal aa'
,
'[2]: aa, 012'
,
'[2]: normal aaaa'
,
'[3]: aaaa, 0123'
,
'[3]: normal aaaaaa'
,
];
expect
(
formatter
.
log
,
referenceLog
);
});
}
class
MockTextFormatter
extends
TextInputFormatter
{
MockTextFormatter
()
:
_counter
=
0
,
log
=
<
String
>[];
MockTextFormatter
()
:
formatCallCount
=
0
,
log
=
<
String
>[];
int
_counter
;
int
formatCallCount
;
List
<
String
>
log
;
@override
...
...
@@ -4251,8 +4315,8 @@ class MockTextFormatter extends TextInputFormatter {
TextEditingValue
oldValue
,
TextEditingValue
newValue
,
)
{
_counter
++;
log
.
add
(
'[
$
_counter
]:
${oldValue.text}
,
${newValue.text}
'
);
formatCallCount
++;
log
.
add
(
'[
$
formatCallCount
]:
${oldValue.text}
,
${newValue.text}
'
);
TextEditingValue
finalValue
;
if
(
newValue
.
text
.
length
<
oldValue
.
text
.
length
)
{
finalValue
=
_handleTextDeletion
(
oldValue
,
newValue
);
...
...
@@ -4265,14 +4329,14 @@ class MockTextFormatter extends TextInputFormatter {
TextEditingValue
_handleTextDeletion
(
TextEditingValue
oldValue
,
TextEditingValue
newValue
)
{
final
String
result
=
'a'
*
(
_counter
-
2
);
log
.
add
(
'[
$
_counter
]: deleting
$result
'
);
final
String
result
=
'a'
*
(
formatCallCount
-
2
);
log
.
add
(
'[
$
formatCallCount
]: deleting
$result
'
);
return
TextEditingValue
(
text:
result
);
}
TextEditingValue
_formatText
(
TextEditingValue
value
)
{
final
String
result
=
'a'
*
_counter
*
2
;
log
.
add
(
'[
$
_counter
]: normal
$result
'
);
final
String
result
=
'a'
*
formatCallCount
*
2
;
log
.
add
(
'[
$
formatCallCount
]: normal
$result
'
);
return
TextEditingValue
(
text:
result
);
}
}
...
...
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