19

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?

IronHawk
  • 365
  • 3
  • 7
  • 1
    you mean that [Icon](https://api.flutter.dev/flutter/widgets/Icon-class.html)? if so, you cannot do that – pskink Sep 13 '19 at 10:54
  • 1
    Yes, I'm about that [Icon](https://api.flutter.dev/flutter/widgets/Icon-class.html). And it is very strange if so (even if the icon is actually not an image but created from TTF font internally). – IronHawk Sep 13 '19 at 11:06
  • 1
    its a `TextSpan` see https://www.crossdart.info/p/flutter/0.0.38-dev/src/widgets/icon.dart.html#line-128 – pskink Sep 13 '19 at 11:20

4 Answers4

21

@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));
Ant D
  • 2,481
  • 6
  • 18
  • 34
  • is there a way to add a GestureDetector on the icon ? – Simran Aswani Feb 16 '20 at 04:35
  • 1) If you mean IconButton widget - It has an onPressed function. 2) If you want to add the above CustomPainter add it as a child to Container. This Container's parent can be a GeatureDetector or InkWell. – Ant D Feb 16 '20 at 13:17
  • You need to also pass the color to TextStyle. Otherwise, you won't see anything. – user2233706 Nov 03 '20 at 15:29
14

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));
Richard Heap
  • 48,344
  • 9
  • 130
  • 112
  • This didn't work for https://pub.dev/packages/font_awesome_flutter, but @Ludonope's answer did – opsb Feb 20 '21 at 12:25
9

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));
Ludonope
  • 963
  • 9
  • 14
  • Thank you this "package: icon.fontPackage" was really the way to go when using icon packages from different source :D – apatrck01 Feb 22 '23 at 18:09
0

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

enter image description here

Doan Bui
  • 3,572
  • 25
  • 36