简体   繁体   English

如何在 flutter 中的矩形内绘制文本

[英]How to draw a text inside rectangle in flutter

Hi I want to draw a filled rect with a text inside using canvas.嗨,我想使用 canvas 绘制一个带有文本的填充矩形。

This is my code:这是我的代码:

Rect rect = Rect.fromLTWH(x,y,size,size,);
Paint paint = Paint()..color = Colors.blue;
canvas.drawRRect(RRect.fromRectAndRadius(rect, Radius.circular(100.0)),paint,);

I don't know how to put a text inside a rect.我不知道如何将文本放在矩形内。 Please help请帮忙

Use TextPainter class.使用TextPainter class。 You can play around with the offset yourself你可以自己玩弄偏移量

    TextPainter painter;
    painter = TextPainter(
      textAlign: TextAlign.center,
      textDirection: TextDirection.ltr,
    );
    painter.text = TextSpan(
      text: 'Sample Text',
      style: TextStyle(
        color: Colors.black,
        fontSize: 40.0,
      ),
    );
    Offset position = Offset(
      x+2.0, 
      y+2.0,
    );
    painter.paint(canvas, position);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM