0

CustomPainter根据这个问题,我正在使用颤振来绘制:颤振:如何在画布上绘制图标?

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

这适用于材料设计图标。但是我想使用相同的技术来绘制 Font Awesome Icons。我正在使用font_awesome_flutter 8.5.0 包,这是我的代码:

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

我从 IDE 收到一条消息,说“没有为类 'Icon' 定义 getter 'codePoint'”。请问我该如何解决这个问题?

4

1 回答 1

6

感谢@pskink 帮助我找出这个答案:

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));
于 2020-01-12T09:23:46.323 回答