Flutter: How to paint an Icon on Canvas?

断了今生、忘了曾经 提交于 2020-01-24 20:37:05

问题


I'm using a CustomPainter to draw in Flutter like this:

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

How to draw an Icon on canvas?


回答1:


Create a Paragraph containing the code point in the correct font, style it as needed, then draw it.

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));



回答2:


@Richard Heap and @pskink based on your answers I was trying and came up with this: Thank you guys this is what I too was searching for.

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));


来源:https://stackoverflow.com/questions/57922017/flutter-how-to-paint-an-icon-on-canvas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!