Update TextEditingValue.selection on selection change (#9284)

Fixes a bug where tapping in the text under edit moved the cursor but
did not push the updated selection to the engine.

Adds a test for caret position updates.
This commit is contained in:
Chris Bracken
2017-04-07 11:13:10 -07:00
committed by GitHub
parent 5b89c10105
commit a7a95daa52
2 changed files with 35 additions and 1 deletions

View File

@@ -381,12 +381,13 @@ class EditableTextState extends State<EditableText> implements TextInputClient {
}
void _handleSelectionChanged(TextSelection selection, RenderEditable renderObject, bool longPress) {
config.controller.selection = selection;
// Note that this will show the keyboard for all selection changes on the
// EditableWidget, not just changes triggered by user gestures.
requestKeyboard();
_hideSelectionOverlayIfNeeded();
config.controller.selection = selection;
if (config.selectionControls != null) {
_selectionOverlay = new TextSelectionOverlay(

View File

@@ -191,6 +191,39 @@ void main() {
await tester.pump();
});
testWidgets('Caret position is updated on tap', (WidgetTester tester) async {
final TextEditingController controller = new TextEditingController();
Widget builder() {
return overlay(new Center(
child: new Material(
child: new TextField(
controller: controller,
),
),
));
}
await tester.pumpWidget(builder());
expect(controller.selection.baseOffset, -1);
expect(controller.selection.extentOffset, -1);
final String testValue = 'abc def ghi';
await tester.enterText(find.byType(EditableText), testValue);
await tester.idle();
await tester.pumpWidget(builder());
// Tap to reposition the caret.
final int tapIndex = testValue.indexOf('e');
final Point ePos = textOffsetToPosition(tester, tapIndex);
await tester.tapAt(ePos);
await tester.pump();
expect(controller.selection.baseOffset, tapIndex);
expect(controller.selection.extentOffset, tapIndex);
});
testWidgets('Can long press to select', (WidgetTester tester) async {
final TextEditingController controller = new TextEditingController();