我想在一个带有参数paint的小部件上方写入文本,该参数需要Paint()。该小部件在图形节点之间绘制边缘,并且它不接受任何像 TextPaint() 那样的参数。
最佳答案
下面是在 CustomPainter 或 CustomDecoration 内绘制文本的示例。您所需要的只是在 Canvas 上使用 TextPainter。
final textPainter = TextPainter(
text: TextSpan(
text: 'Foo\nBar',
style: TextStyle(
color: Colors.black,
fontSize: 30,
),
),
textDirection: TextDirection.ltr,
textAlign: TextAlign.center
);
textPainter.layout();
// Draw the text centered around the point (50, 100) for instance
final offset = Offset(50 - (textPainter.width / 2), 100 - (textPainter.height / 2));
textPainter.paint(canvas, offset);
关于flutter - 如何在 flutter 中使用 Paint() 在自定义 Paint 中编写文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64184263/