Clear the selection when a text widget loses focus (#10938)

Fixes https://github.com/flutter/flutter/issues/10911
This commit is contained in:
Jason Simmons 2017-06-23 14:22:13 -07:00 committed by GitHub
parent 128967cd2a
commit 856115b7e2
2 changed files with 41 additions and 0 deletions

View File

@ -531,6 +531,10 @@ class EditableTextState extends State<EditableText> implements TextInputClient {
_openOrCloseInputConnectionIfNeeded();
_startOrStopCursorTimerIfNeeded();
_updateOrDisposeSelectionOverlayIfNeeded();
if (!_hasFocus) {
// Clear the selection and composition state if this widget lost focus.
_value = new TextEditingValue(text: _value.text);
}
}
@override

View File

@ -1503,4 +1503,41 @@ void main() {
expect(scrollableState.position.pixels, isNot(equals(0.0)));
}
);
testWidgets(
'Text field drops selection when losing focus',
(WidgetTester tester) async {
final Key key1 = new UniqueKey();
final TextEditingController controller1 = new TextEditingController();
final Key key2 = new UniqueKey();
Widget builder() {
return overlay(new Center(
child: new Material(
child: new Column(
children: <Widget>[
new TextField(
key: key1,
controller: controller1
),
new TextField(key: key2),
],
),
),
));
}
await tester.pumpWidget(builder());
await tester.tap(find.byKey(key1));
await tester.enterText(find.byKey(key1), 'abcd');
await tester.pump();
controller1.selection = const TextSelection(baseOffset: 0, extentOffset: 3);
await tester.pump();
expect(controller1.selection, isNot(equals(TextRange.empty)));
await tester.tap(find.byKey(key2));
await tester.pump();
expect(controller1.selection, equals(TextRange.empty));
}
);
}