Flutter 使用颤振在特定位置绘制文本
对于画布上的文本绘制,可以使用相当简单的构造:Flutter 使用颤振在特定位置绘制文本,flutter,Flutter,对于画布上的文本绘制,可以使用相当简单的构造: void drawName(Canvas context, String name, double x, double y) { TextSpan span = new TextSpan( style: new TextStyle(color: Colors.blue[800], fontSize: 24.0, fontFamily: 'Roboto'), text: name); TextP
void drawName(Canvas context, String name, double x, double y)
{
TextSpan span = new TextSpan(
style: new TextStyle(color: Colors.blue[800], fontSize: 24.0,
fontFamily: 'Roboto'), text: name);
TextPainter tp = new TextPainter(
text: span, textAlign: TextAlign.left, textDirection: `
` TextDirection.ltr);
tp.layout();
tp.paint(context, new Offset(x, y));
}
是否可以以一定角度绘制文本,例如45度或90度(从下到上的垂直角度)?听起来您正在寻找转换。有一个通用的,但也有一个更具体的,听起来它将是一个完美的适合你
new RotatedBox(
quarterTurns: 3,
child: const Text('Hello World!'),
)
如果您需要更多地控制旋转(使用90度增量以外的其他方式),您应该能够实现这一点,并且
要旋转画布上的文本,可以使用画布变换,而不是旋转整个画布 看起来是这样的:
@override
void paint(Canvas canvas, Size size) {
// save is optional, only needed you want to draw other things non-rotated & translated
canvas.save();
canvas.translate(100.0, 100.0);
canvas.rotate(3.14159/4.0);
TextSpan span = new TextSpan(
style: new TextStyle(color: Colors.blue[800], fontSize: 24.0,
fontFamily: 'Roboto'), text: "text");
TextPainter tp = new TextPainter(
text: span, textDirection: TextDirection.ltr);
tp.layout();
tp.paint(canvas, new Offset(0.0, 0.0));
// optional, if you saved earlier
canvas.restore();
}
请注意,我先平移,然后旋转,因为如果在偏移之后平移,甚至使用偏移,可能会得到不同于所需的结果。此外,一旦开始使用变换(平移和旋转),您可能希望保存变换状态,然后在绘制要变换的内容后恢复,至少在绘制旋转文本以外的任何内容时是这样。以指定角度绘制文本的函数:
void drawText(Canvas context, String name, double x, double y, double angleRotationInRadians)
{
context.save();
context.translate(x, y);
context.rotate(angleRotationInRadians);
TextSpan span = new TextSpan(
style: new TextStyle(color: Colors.blue[800], fontSize: 24.0,
fontFamily: 'Roboto'), text: name);
TextPainter tp = new TextPainter(
text: span, textAlign: TextAlign.left,
textDirection: TextDirection.ltr);
tp.layout();
tp.paint(context, new Offset(0.0,0.0));
context.restore();
}
PS.“rmtmckenzie”,非常感谢您的帮助。不清楚您是在寻找“如何旋转文本”还是“如何在某处放置文本”。我提供了一个关于旋转的答案,我刚刚写了一个答案——希望它能澄清在画布上旋转的问题,就像你在问什么。但是,如果你真的想让每个字符都笔直,但是整个文本的方向是向下的,那么你可能只需要插入一堆新行!OP要求在画布上旋转文本,我认为.Offset.zero是一个很好的常数。
void drawText(Canvas context, String name, double x, double y, double angleRotationInRadians)
{
context.save();
context.translate(x, y);
context.rotate(angleRotationInRadians);
TextSpan span = new TextSpan(
style: new TextStyle(color: Colors.blue[800], fontSize: 24.0,
fontFamily: 'Roboto'), text: name);
TextPainter tp = new TextPainter(
text: span, textAlign: TextAlign.left,
textDirection: TextDirection.ltr);
tp.layout();
tp.paint(context, new Offset(0.0,0.0));
context.restore();
}