canvasfluttericonscustom-painting

Flutter: How to paint an Icon on Canvas?


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?


Solution

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