Commit 856115b7 authored by Jason Simmons's avatar Jason Simmons Committed by GitHub

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

Fixes https://github.com/flutter/flutter/issues/10911
parent 128967cd
......@@ -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
......
......@@ -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));
}
);
}
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