'flutter : change the cursor location to End of text
According this tutorial a have created a custom TextEditController.
In my textEdit controller I added this text : @tst. When I added this text a list appeasers and user can select one string from suggestion list.
And Finally I replace selected string with @tst . But the problem is here ?
The cursor located the first of Text:
I added this function end of my method:
Future.delayed(Duration(milliseconds: 5000), () {
print("=====>last");
textEditCtl.selection = TextSelection.fromPosition(
TextPosition(offset: textEditCtl.text.length));
});
but the cursor location not changed?
void replaceCities() {
var words = textEditCtl.text;
final pattern = chunkString(words);
final lastWord = words.substring(0, words.lastIndexOf(pattern));
words = lastWord + '@' + selectedCities[0];
textEditCtl.text = words;
// textEditCtl.selection = TextSelection.fromPosition(
// TextPosition(offset: textEditCtl.text.length));
Future.delayed(Duration(milliseconds: 5000), () {
print("=====>last");
textEditCtl.selection = TextSelection.fromPosition(
TextPosition(offset: textEditCtl.text.length));
});
selectedCities.clear();
}
How can I change the cursor location to End of text?
I also Overrided this method in custom Controller:
@override
set text(String newText) {
value = value.copyWith(
text: newText,
selection: TextSelection(
baseOffset: newText.length, extentOffset: newText.length),
composing: TextRange.empty,
);
selection =
TextSelection.fromPosition(TextPosition(offset: newText.length));
}
Solution 1:[1]
You need to first assign value to text property of controller then you can do this.
textEditCtl.text = someString;
textEditCtl.selection = TextSelection.fromPosition(TextPosition(offset: textEditCtl.text.length));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hardik Mehta |

