jurgenizer
jurgenizer

Reputation: 559

Flutter: how to paint a Font Awesome Icon?

I am using CustomPainter to draw in flutter as per this question: Flutter: How to paint an Icon on Canvas?

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

This works for Material Design Icons. However I would like to use the same techniqe to paint Font Awesome Icons. I am using the font_awesome_flutter 8.5.0 package, and this is my code:

final icon = Icon(FontAwesomeIcons.fish, size: 20, color: Colors.teal[700]);
TextPainter textPainter = TextPainter(textDirection: TextDirection.rtl);
textPainter.text = TextSpan(
    text: String.fromCharCode(icon.codePoint),
    style: TextStyle(fontSize: 20.0, fontFamily: icon.fontFamily));
textPainter.layout();
textPainter.paint(canvas, Offset(200.0, 200.0));

I am getting a message from the IDE saying that "The getter 'codePoint' isn't defined for the class 'Icon'". How can I fix this please?

Upvotes: 0

Views: 1817

Answers (1)

jurgenizer
jurgenizer

Reputation: 559

Thanks to @pskink for helping me figure out this answer:

final fishCodePoint = FontAwesomeIcons.fish.codePoint;
print('The codePoint is $fishCodePoint');
final fishFontPackage = FontAwesomeIcons.fish.fontPackage;
print('The fontPackage is $fishFontPackage');
final fishFontFamily = FontAwesomeIcons.fish.fontFamily;
print('The fontFamily is $fishFontFamily');

TextPainter textPainter = TextPainter(textDirection: TextDirection.rtl);
textPainter.text = TextSpan(
    text: String.fromCharCode(fishCodePoint),
    style: TextStyle(
        fontSize: 40.0,
        fontFamily: fishFontFamily,
        package: fishFontPackage));

textPainter.paint(canvas, Offset(50.0, 50.0));

Upvotes: 8

Related Questions