Flutter:如何在画布上绘制图标?

时间:2019-09-13 10:36:11

标签: canvas flutter icons custom-painting

我正在使用CustomPainter这样绘制Flutter:

@override
void paint(Canvas canvas, Size size) {
  canvas.drawRect(...);
  canvas.drawImage(...);
  ...
}

如何在Icon上绘制canvas

3 个答案:

答案 0 :(得分:4)

创建一个包含Paragraph的代码点的正确字体,根据需要设置样式,然后绘制。

final icon = Icons.add;
var builder = ui.ParagraphBuilder(ui.ParagraphStyle(
  fontFamily: icon.fontFamily,
))
  ..addText(String.fromCharCode(icon.codePoint));
var para = builder.build();
para.layout(const ui.ParagraphConstraints(width: 60));
canvas.drawParagraph(para, const Offset(20, 20));

答案 1 :(得分:1)

只需添加一个很小但很重要的细节,如果您尝试从外部图标包(例如Material Design Icons或FontAwesome)中渲染图标,则需要在{{1 }}。

package

答案 2 :(得分:0)

@Richard Heap和@pskink根据您尝试的答案,提出了以下建议:谢谢,这也是我也在寻找的内容。

final icon = Icons.add;
TextPainter textPainter = TextPainter(textDirection: TextDirection.rtl);
textPainter.text = TextSpan(text: String.fromCharCode(icon.codePoint),
        style: TextStyle(fontSize: 40.0,fontFamily: icon.fontFamily));
textPainter.layout();
textPainter.paint(canvas, Offset(50.0,50.0));