如何将填充添加到文本小部件的背景颜色

Chr*_*446 3 dart flutter flutter-layout

我想创建一个文本小部件来实现您在下面看到的内容:

在此处输入图片说明

我希望文本的背景颜色有一些额外的填充,但仅限于实际存在文本的地方。

目前,我必须在将文本小部件包装在容器小部件中进行选择(然后将背景颜色添加到第二条较短行的空白区域,这是我不想要的)。或者,我可以选择为文本的 textStyle 小部件使用背景颜色,但它不考虑高度(又名行高)。

有没有办法做到这一点?

Mob*_*ina 5

您可以使用PaintbackgroundTextStyle

Text(
  'Random caption for this post1',
  style: TextStyle(
    fontWeight: FontWeight.bold,
    background: Paint()..color = Colors.white
    ..strokeWidth = 17
    ..style = PaintingStyle.stroke,
  ),
  textAlign: TextAlign.center,
)
Run Code Online (Sandbox Code Playgroud)

结果:

图片