Home > Net >  How to add spacing between Strings, which is coming dynamically in flutter?
How to add spacing between Strings, which is coming dynamically in flutter?

Time:11-02

I am loading a description dynamically as text, I want to design the text and insert some spacing between the text, Here is the code I am trying.

   Container(
              height: 240.h,
              width: Get.width / 1.1,
              child: SingleChildScrollView(
                child: Text(
                  product.description,
                 style: subHeadingTextStyle6,
                ),
              )),

The output of my code is like;

enter image description here

I want the following spacing of the text

enter image description here

CodePudding user response:

 Text( product.description, style: TextStyle(color: Colors.black, height: 4)),

Update to comment: "thanks for your concern, the height property is good, but it did it equally for all line, what else I have to do to look like the picture, which I have shared in question"

This will do the job!

RichText(
        text: TextSpan(
        children: splitText(text),
              ),
            )

List<TextSpan> splitText(String text) {
  List<TextSpan> reasonList = [];
  text.split(".").forEach((element) {
    reasonList.add(TextSpan(
        text: "$element\n\n\n", style: TextStyle(color: Colors.black)));
  });
  return reasonList;
}

So what are we doing exactly?

First we're turning your text into a list of sentences, with text.split(".") which splits the text whenever it encounters a '.'

Now we are adding it in a list of TextSpan to be able to use it in RichText. Here's the part where it makes space between every sentence "$element\n\n\n" I don't know if you know but \n means insert a new line. As if you hit the enter button. I used 3 \n's so that it will leave 2 lines empty after every sentence.

CodePudding user response:

I think just adding \n\n between the paragraphs should work.

  • Related