Flutter TextField get Selected Text

Viewed 2345

In android we can get the starting and ending index of selected text in an EditText using this :

int a = inputET.getSelectionStart();
int b = inputET.getSelectionEnd();

What is the flutter alternative for this in a TextField ?

2 Answers

It can be a bit more straightforward than Fayaz's answer: with a TextField(controller: _textEditingController) one can access the selected text like this:

_textEditingController.selection.textInside(_textEditingController.text)

Lets say we have this widget in widget tree

TextField(controller: _textEditingController),

on some button press, consider to print the selected text

printSelectedText(){
   print(_textEditingController.text.substring(_textEditingController.selection.baseOffset,_textEditingController.selection.extentOffset));
}
Related