Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding extra padding to TextSpan background in RichText widget

Tags:

flutter

dart

I have a RichText with some TextSpan. I want one of the TextSpan widgets to have a background on it so I used background: Paint()..color = Colors.redAccent as its text style.

Is there a way to add some extra red space/padding to the background.

This is how it currently looks:

before padding on text

This is how I want it to look (notice the extra red on the top and bottom of the highlighted text):

after padding on text (desired)

This is the code used:

Scaffold(
  appBar: new AppBar(),
  body: new RichText(
    text: new TextSpan(
      text: 'This is a one the lines in the span \n ',
      style: TextStyle(fontSize: 15.0, color: Colors.black),
      children: <TextSpan>[
        new TextSpan(
            text: 'This is the second line',
            style: new TextStyle(fontWeight: FontWeight.bold)),
        new TextSpan(
            text: ' background on me ',
            style: new TextStyle(
              fontWeight: FontWeight.bold,
              background: Paint()..color = Colors.redAccent,
            )),
        new TextSpan(text: ' This is another of the lines in the span!'),
      ],
    ),
  ), // This trailing comma makes auto-formatting nicer for build methods.
)

I tried adding height to the text style, but it doesn't affect the height of the background.

like image 392
S.D. Avatar asked Sep 29 '18 01:09

S.D.


People also ask

How do you add padding to rich text in flutter?

How do you give padding and margin in Flutter? Apply Padding and Margin using Container Widget: Container( //apply margin and padding using Container Widget. padding: EdgeInsets. all(20), //You can use EdgeInsets like above margin: EdgeInsets.

What is a rich text widget?

The rich text widget allows you to enter freestyle text in your page and edit it with an editor. You can also make a query and then display the results of that query within the widget. You can also style the text any way you want. REQUIRED USER ROLE.

How do you show paragraphs in flutter?

RichText is a very useful widget in Flutter, which is used for displaying a paragraph of text on the UI with multiple styles. Inside the widget, we can have different styles by giving it a tree of TextSpan widgets. Each TextSpan can set its own style for overriding the default style.


2 Answers

The new solution in 2019 with flutter 1.7.8 is WidgetSpan, you could just add a Container and a Padding or any combination of widget to make more variety!

Here is an example to use on the case:

WidgetSpan(
  child: Container(
    color: Colors.red,
    padding: EdgeInsets.all(8.0),
    child: Text("background on me", style: ...),
  )
)

And don't forget to change <TextSpan>[] to <InlineSpan>[], that's It!

like image 171
Tokenyet Avatar answered Oct 06 '22 19:10

Tokenyet


Quite ugly solution, but I haven't found other (

TextStyle(fontWeight: FontWeight.bold,
  background: Paint()..color = Colors.redAccent
    ..strokeWidth = 16.5
    ..style = PaintingStyle.stroke,)

strokeWidth have to be big enough to cover height of text. In my case 16.5 is minimal suitable value

like image 29
Andrey Turkovsky Avatar answered Oct 06 '22 19:10

Andrey Turkovsky