'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 1:[1]

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

Solution 2:[2]

Just to add a small but important detail, if you are trying to render an icon from an external icon pack (such a Material Design Icons or FontAwesome) you need to add the package parameter in the TextStyle.

final icon = MdiIcons.check;
TextPainter textPainter = TextPainter(textDirection: TextDirection.ltr);
textPainter.text = TextSpan(
  text: String.fromCharCode(icon.codePoint),
  style: TextStyle(
    color: Colors.black,
    fontSize: size,
    fontFamily: icon.fontFamily,
    package: icon.fontPackage, // This line is mandatory for external icon packs
  ),
);
textPainter.layout();
textPainter.paint(canvas, Offset(x, y));

Solution 3:[3]

If you have SVG file design. You can use this website to automatically generated CustomPainter file dart.

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Ludonope
Solution 3 Doan Bui