Unverified Commit 7809651c authored by Michael Goderbauer's avatar Michael Goderbauer Committed by GitHub

Put cursor at end of field if textfield is focused (#17367)

parent 501316fd
......@@ -620,6 +620,9 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
if (!_hasFocus) {
// Clear the selection and composition state if this widget lost focus.
_value = new TextEditingValue(text: _value.text);
} else if (!_value.selection.isValid) {
// Place cursor at the end if the selection is invalid when we receive focus.
widget.controller.selection = new TextSelection.collapsed(offset: _value.text.length);
}
updateKeepAlive();
}
......
......@@ -783,6 +783,31 @@ void main() {
expect(render.text.style.fontStyle, FontStyle.italic);
});
testWidgets('autofocus sets cursor to the end of text', (WidgetTester tester) async {
const String text = 'hello world';
final FocusScopeNode focusScopeNode = new FocusScopeNode();
final FocusNode focusNode = new FocusNode();
controller.text = text;
await tester.pumpWidget(new Directionality(
textDirection: TextDirection.ltr,
child: new FocusScope(
node: focusScopeNode,
autofocus: true,
child: new EditableText(
controller: controller,
focusNode: focusNode,
autofocus: true,
style: textStyle,
cursorColor: cursorColor,
),
),
));
expect(focusNode.hasFocus, true);
expect(controller.selection.isCollapsed, true);
expect(controller.selection.baseOffset, text.length);
});
}
class MockTextSelectionControls extends Mock implements TextSelectionControls {}
......
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