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.
Related
how i can build a textField to change text direction line by line ( based on the first character )
For example, I want it to be displayed as below when typing 2 letters in different languages in the text field
// this line started with LTR characters, start at left
Hi Good morning
// this line started with RTL characters, start at right
سلام، صبح بخیر
Are you ok?
حالت خوبه؟
I looked at Flutter's code and noticed that Flutter uses text span to create text in the text field (that is, all the text can only be changed), is this possible with Flutter's own text field widget?
I'm working on a Markdown editor in Flutter, built on top of the TextField widget.
A feature I want to implement is configurable spacing between paragraphs (i.e. after newlines), so that the user doesn't need hit return twice and manually insert an empty line between paragraphs, as is required by Markdown. Instead, I want to create the illusion of an empty line by increasing the space between paragraphs and automatically insert them during export.
If you've used Notion before, it does something similar. So does MarkText.
How would that be accomplished in Flutter? Can it be done with the out-of-the-box TextField, or do I need to implement a custom solution around EditableText?
I've messed around with the height property of the TextField's TextStyle style and StrutStyle strutStyle properties, but both seem to affect the text at the line level, so what I get is the same as, say, changing the line spacing in a Word document. Not what I'm going for.
What you can do is listen to the TextField changes, and detect the enter/return press, and you can do that using the built-in onChanged Listener, so you can create a TextField with a TextEditingController, and in the onChanged handler, you can detect the press by checking if the string finishes by \n, and here you can manipulate your string as you want, i.e. you can add empty lines by appending another \n.
Example:
TextEditingController co = TextEditingController();
TextField(
maxLines: 100,
controller: co,
onChanged: (val){
if(val.endsWith('\n')){
co.text = co.text+'\n'; //append new line
co.selection = TextSelection.fromPosition(TextPosition(offset: co.text.length));//set the pointer at the end of the string
}
},
),
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
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.
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