我有RichText分加TextSpan分.我想让TextSpan个小部件中的一个有它的背景,所以我使用background: 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.
)

我try 将height添加到文本样式,但它不影响背景高度.

推荐答案

2019年推出的Flitter 1.7.8的新解决方案是WidgetSpan,您只需添加ContainerPadding或任何组合的小部件,就可以实现更多的多样性!

以下是 case 中使用的示例:

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

别忘了把<TextSpan>[]改成<InlineSpan>[],就是这样!

Flutter相关问答推荐

Flutter -带有TextAlign.right的文本字段不显示空白

我希望我的flutter容器具有与css类似的squireCircle UI

为什么小部件会在其父文件中重建,但放在单独文件中时却不会重建?

在Flutter中,CachedNetworks Image不工作(不从CachedData加载)

当MyChangeNotifier更改时如何计算函数,而无需在构建方法中进行不必要的调用- Flutter

如何更改ElevatedButton的 colored颜色 以按下它?

ShaderMask上的文本渐变问题

如何在Firebase项目中更改包名称和包ID

IsScrollable为True时如何删除Flutter 选项卡栏左侧填充

CMakeLists.txt处的CMake错误:3(项目)找不到指定的Visual Studio实例

你能go 掉Flutter 小部件测试中的样板吗?

为什么Spotify Auth API返回400,而";code_verier不正确?

Ffmpeg_kit_fltter:^6.0.3版权问题

Flutter类调用另一个类中的另一个方法失败

参数类型void Function()无法分配给参数类型void Function(LongPressEndDetails)?

如何从 Firebase `get()` 获取 `DocumentReference`

如何在手势检测器中获得涟漪效应

Positioned 的宽度或 SizedBox 的宽度都不适用于堆栈小部件 - Flutter

我收到这个错误我无法解决

Android Studio 模拟器屏幕闪烁