Skip to content

Commit ebf685f

Browse files
jason-simmonsgspencergoog
authored andcommitted
Clear the selection when a text widget loses focus (flutter#10938)
Fixes flutter#10911
1 parent 9f880f8 commit ebf685f

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

packages/flutter/lib/src/widgets/editable_text.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,10 @@ class EditableTextState extends State<EditableText> implements TextInputClient {
531531
_openOrCloseInputConnectionIfNeeded();
532532
_startOrStopCursorTimerIfNeeded();
533533
_updateOrDisposeSelectionOverlayIfNeeded();
534+
if (!_hasFocus) {
535+
// Clear the selection and composition state if this widget lost focus.
536+
_value = new TextEditingValue(text: _value.text);
537+
}
534538
}
535539

536540
@override

packages/flutter/test/material/text_field_test.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,4 +1503,41 @@ void main() {
15031503
expect(scrollableState.position.pixels, isNot(equals(0.0)));
15041504
}
15051505
);
1506+
1507+
testWidgets(
1508+
'Text field drops selection when losing focus',
1509+
(WidgetTester tester) async {
1510+
final Key key1 = new UniqueKey();
1511+
final TextEditingController controller1 = new TextEditingController();
1512+
final Key key2 = new UniqueKey();
1513+
1514+
Widget builder() {
1515+
return overlay(new Center(
1516+
child: new Material(
1517+
child: new Column(
1518+
children: <Widget>[
1519+
new TextField(
1520+
key: key1,
1521+
controller: controller1
1522+
),
1523+
new TextField(key: key2),
1524+
],
1525+
),
1526+
),
1527+
));
1528+
}
1529+
1530+
await tester.pumpWidget(builder());
1531+
await tester.tap(find.byKey(key1));
1532+
await tester.enterText(find.byKey(key1), 'abcd');
1533+
await tester.pump();
1534+
controller1.selection = const TextSelection(baseOffset: 0, extentOffset: 3);
1535+
await tester.pump();
1536+
expect(controller1.selection, isNot(equals(TextRange.empty)));
1537+
1538+
await tester.tap(find.byKey(key2));
1539+
await tester.pump();
1540+
expect(controller1.selection, equals(TextRange.empty));
1541+
}
1542+
);
15061543
}

0 commit comments

Comments
 (0)