简体   繁体   English

在 RichText 小部件中向 TextSpan 背景添加额外的填充

[英]Adding extra padding to TextSpan background in RichText widget

I have a RichText with some TextSpan .我有一个带有一些TextSpanRichText 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.我希望其中一个TextSpan小部件具有背景,因此我使用了background: Paint()..color = Colors.redAccent作为其文本样式。

Is there a way to add some extra red space/padding to the background.有没有办法在背景中添加一些额外的红色空间/填充。

This is how it currently looks:这是它目前的样子:

在填充文本之前

This is how I want it to look (notice the extra red on the top and bottom of the highlighted text):这就是我希望它的外观(注意突出显示文本顶部和底部的额外红色):

在文本上填充后(需要)

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.我尝试为文本样式添加height ,但它不会影响背景的高度。

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! 2019 年 Flutter 1.7.8 的新解决方案是WidgetSpan ,您可以添加一个Container和一个Padding或任何小部件的组合来增加多样性!

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!并且不要忘记将<TextSpan>[]更改为<InlineSpan>[] ,就是这样!

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. strokeWidth 必须足够大以覆盖文本的高度。 In my case 16.5 is minimal suitable value在我的情况下,16.5 是最小的合适值

you can use widgetSpan that inside RichText so that you can add any widget inside RichText.您可以在 RichText 中使用 widgetSpan,以便您可以在 RichText 中添加任何小部件。

RichText(
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          text: TextSpan(
            text: "text one", // text of exemple
            children: [

              // add a space between texts
              WidgetSpan(
                child: Padding(
                  padding: const EdgeInsets.only(left: 10.0),
                ),
              ),

              TextSpan(
                text: "text two", // text exemple
              ),
            ],
          ),
        ),

You could use the Padding class. 您可以使用Padding类。

Maybe this question's answer can help you. 也许这个问题的 答案可以帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM