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

前端之家收集整理的这篇文章主要介绍了dart – 在RichText小部件中为TextSpan背景添加额外的填充前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个带有一些TextSpan的RichText.我希望其中一个TextSpan小部件有背景,所以我使用了背景:Paint().. color = Colors.redAccent作为其文本样式.

有没有办法在背景中添加一些额外的红色空间/填充.

这是它目前的样子:

before padding on text

这就是我想要它的样子(注意突出显示文本顶部和底部的额外红色):

after padding on text (desired)

这是使用的代码

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.
)

我尝试在文本样式中添加高度,但它不会影响背景的高度.

解决方法

相当丑陋的解决方案,但我没有找到其他(

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

strokeWidth必须足够大才能覆盖文本的高度.在我的情况下,16.5是最小的合适值

猜你在找的Flutter相关文章