From a7a95daa52bf69ed42b7e15bb8be184ca6a84352 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Fri, 7 Apr 2017 11:13:10 -0700 Subject: [PATCH] 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. --- .../lib/src/widgets/editable_text.dart | 3 +- .../test/material/text_field_test.dart | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index f8b4c82b4a..c1fcecb97f 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -381,12 +381,13 @@ class EditableTextState extends State 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( diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index 4cf3486b69..cb5435f4e2 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -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();