flutter- put cursor on a textfield using local offset - flutter

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?

Related

VSCode : possible to synchronize scroll in column view?

Can VSCode display text "newspaper style," with text flowing over into the next column?
Use case: view what would ordinarily be multiple pages, but at once, in sync.
This ...that Yay for
is continues using my
some in the wide
text... next column! monitor.
And if you scroll:
some in the
text... next column!
This ...that Yay for
is continues using my

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

TextField's current focus position in 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.

how to add a new line button to numeric keyboard. TextFormField with phones list

I need to make a long TextFormField to insert a lot of phones(for example maxLines:10). Each line is a single phone. Have no idea how to make my keyboard with numbers and break (next line) button only.
TextInputType.number and TextInputType.phone cannot make a new line.
What to do?
why don't you display the lines as a list view/column and add a row each time you will enter the next number - so instead of one field with wrapped lines use multiple input fields

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.