How can I change the vertical spacing between paragraphs inside a TextField? - flutter

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
}
},
),

Related

Wrap TextField into multiple lines when not in editing mode

I am trying to wrap a TextField into multiple lines when it is not being edited.
The following code works fine while the user is in editing mode:
TextField(
keyboardType: TextInputType.multiline,
maxLines: null,
)
but when the user leaves the page and comes back, the controller's text inside the TextField is fitted on one line, overflowing horizontally - making it hard for a user to append text. How do I get the text of a TextField to wrap on multiple lines when it is not being edited?
If the textfield is inside a row please wrap it with an flexible or sized box widget.
Edit
Add minLines: 1, and maxLines : 20, by the docs setting it to null means its a single line just displayed in different lines.. setting a minLine and maxLines should work

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

Hide certain characters in a text field in Flutter

I have a text field which I need to style for example with bold or italics parts.
I tried overridding the TextEditingController's buildTextSpan and formatting the text using annotation ranges with custom styles but the edge cases were too much and I really couldn't get it to work.
So, thought about using a formatter where before every change in format I'll add a custom character, like this:
This text is |bBOLD and this is |iITALICS. Would get me this:
This text is BOLD and this is ITALICS. So I override the buildTextSpan to build the TextSpan from a parse function where I split the text by the special characters and check the initial letter of each text for formatting info.
This works well except for the fact that when I press the right arrow to go the next character after the "This text is ", the cursor will stay fixed as it thinks there are two characters but being only for formatting, they aren't there on the render.
Is there any way I could tell the textfield to ignore certain characters when selecting, moving selection or typing?
I think this would work!
static const kCharToBEIgnored = 0x2C;
// Here 0x2C means ',' comma
// For complete list visit https://api.flutter.dev/flutter/charcode/charcode-library.html
String get text {
return String.fromCharCodes(
_value.text.codeUnits.where((ch) => ch != kCharToBEIgnored),
);
}

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.