TextField's current focus position in flutter - flutter

Hey I have a TextField and a controller for it through which I access its current text value and add some text like (Some Special Asterix or text with angular brackets just like hashnode's text editor) to it when needed, adding text at the end is easy.
controller.text += "Something";
The above code will add Something to the end. But I need to know the current TextFields Cursor position and add text according to it.
I really love to know how we can do it in flutter efficiently

Try this to get the cursor position
print(controller.selection.baseOffset);
print(controller.selection.extentOffset);
if they are the same, it is current cursor position.
if not, it means that some of text is selected, baseOffset is start position and extentOffset is end positon.

Related

How to set position label text in TextFormField in outlineborder?

How to set position label in TextFormField in outlineborder?
i want set text position. Because, label prefer to bottom not center in border
According to the documentation for the label
Text that describes the input field.
When the input field is empty and unfocused, the label is displayed on
top of the input field (i.e., at the same location on the screen where
text may be entered in the input field). When the input field receives
focus (or if the field is non-empty), the label moves above (i.e.,
vertically adjacent to) the input field.
So, basically this is the behavior for the label. If you want something else you have to make it on your own ( using a Stack widget with a TextField and a Text ). But with this solution you have to treat the focus properties as well on your own.
You can use this post as a reference
https://stackoverflow.com/a/58039585/10143503

flutter- put cursor on a textfield using local offset

Flutter textfield automatically puts cursor on the desired position when one simply taps on it.
However, I do not know how to achieve this effect when the user clicks on it for the first time, since I am just making the textfield visible.
I have the local offset in which I would like to put my cursor on. However, flutter only has the selection method, in which I have to know the number of text characters rather than the local offset.
contentTextController.selection = TextSelection.fromPosition(
TextPosition(offset: theoffsetnum,)
How can I put the cursor on the text that corresponds to the local offset within the textfield?

How can I add line numbers to TextField on Flutter?

I am trying to add line numbers to text field on Flutter, like on any text editors. I have text that overflows to next line, so I want to show where lines start and end.
First way that came to my mind was to separate the screen into two columns, one for line numbers on left and one for right for the textfield. However, due to screen size differences, I can't know when a line will overflow.
Is there any more formal way to do it?
First, initialize a List of TextField and an currentLine integer variables. TextField must contain properties named textInputAction and onEditingComplete. So each time you press enter, it is considered as finished with current TextField, creating a new TextField and add it into List of TextField and will move the focus to the new TextField. Line numbers will be coming from inputDecoration of each TextField and the value will be from currentLine variable. curentLine++ also ran inside onEditingComplete.
Check out this article about LineMetrics.
As you can see, this gives you a list of metadata concerning your rendered text lines. Using the line numbers and the hardBreak attribute together, you can compute the line numbers to display in the left hand column.
you can use TextSelection()
TextSelection selection = TextSelection(baseOffset: 0, extentOffset: text.length);
List<TextBox> boxes = textPainter.getBoxesForSelection(selection);
int numberOfLines = boxes.length;
get numberOfLines in your Text, count, and show for each line.

read text at cursor position in JFace TextViewer

I have an editor that extends a TextEditor class. I would like to know if it is possible to read the text at the cursor position within the editor.
Using ITextSelection is not useful since it expects that some text string is selected. I require to read text when the cursor is at some point and no text is selected.
Thanks.
ITextViewer.getSelectedRange(). It will return the offset and the size of the selection. Offset is the caret position and size may be 0. To read the text, use ITextViewer.getDocument().get() and analyze the contents at the respective offset.

How to figure out the current caret position in a UITextField?

I want to programmatically paste a string into a text field or text view at the current caret position.
Is there an easy way to do this? I would need to know the current caret position, but there's no method to retrieve it, right? Don't want to call private API. Is there any legal way to do it?
Use UITextView's selectedRange method. If the range has a non-zero length (in the case of the user having selected some text), just take the location (or NSMaxRange(), or replace the entire range...)
Instead of pasting option. Get the string from textfield or textview insert the string which you want to paste it by providing the index and then clear the textfield then pass the string which you inserted.