如何在文本字段中flutter的值的末尾设置光标位置?

问题描述:

问题是我想在文本字段的值末尾添加,00",在iOS设备中效果很好,但是在android中,光标移动到文本字段中值的起点.因此,我无法添加,00".

the problem is i want to add ",00" at the end of the value of the textfield works good in iOS device but in android cursor moves to starting point of the value in textfield. So, i'm unable to add ",00".

我在设置TextEditingController时遇到了同样的问题,这对我有用.

I've been having the same problem with setting a TextEditingController and this is what worked for me.

controller.text = someString;
controller.selection = TextSelection.fromPosition(TextPosition(offset: controller.text.length));

TextSelection.fromPosition()(从文档中)执行以下操作:

TextSelection.fromPosition() does the following (from the documentation):

在给定的文本位置创建一个折叠的选择.崩溃了 选择以相同的偏移量开始和结束,这意味着它包含 零个字符,但用作文本中的插入点.

Creates a collapsed selection at the given text position. A collapsed selection starts and ends at the same offset, which means it contains zero characters but instead serves as an insertion point in the text.