【发布时间】:2021-06-10 17:22:43
【问题描述】:
我需要在 Flutter pdf 中添加图标。与在颤振中添加图标相比,这完全不同。我正在使用https://pub.dev/packages/pdf这个包。
代码如下:
pw.Icon(pw.IconData(0xe047));
错误是:
ArgumentError (Invalid argument (string): Contains invalid characters.: "")
【问题讨论】:
我需要在 Flutter pdf 中添加图标。与在颤振中添加图标相比,这完全不同。我正在使用https://pub.dev/packages/pdf这个包。
代码如下:
pw.Icon(pw.IconData(0xe047));
错误是:
ArgumentError (Invalid argument (string): Contains invalid characters.: "")
【问题讨论】:
检查页面主题下是否添加了 PdfGoogleFonts.materialIcons():
theme: pw.ThemeData.withFont(
base: await PdfGoogleFonts.openSansRegular(),
bold: await PdfGoogleFonts.openSansBold(),
icons: await PdfGoogleFonts.materialIcons(), // this line
)
【讨论】:
使用www.fluttericon.com设置您的自定义图标
像这样使用 pw.ThemeData.withFont
var pathToFile = await rootBundle.load('- your icon font file (.ttf) -');
final ttf = pw.Font.ttf(pathToFile);
// load ttf to pdf theme
final theme = pw.ThemeData.withFont(
base: await PdfGoogleFonts.robotoCondensedRegular(),
bold: await PdfGoogleFonts.robotoCondensedBold(),
icons: ttf,
);
final pw.Icon(pw.IconData(customIcon.icon.codePoint), size: 10)
渲染pdf文件后的输出是,
【讨论】:
你必须添加打印模块
https://pub.dev/packages/printing
dependencies:
printing: ^5.6.0
在你的 dart 文件中导入包
import 'package:printing/printing.dart';
并设置您的主题:
final pdf = pw.Document();
pdf.addPage(
pw.Page(
theme: pw.ThemeData.withFont(
base: await PdfGoogleFonts.varelaRoundRegular(),
bold: await PdfGoogleFonts.varelaRoundRegular(),
icons: await PdfGoogleFonts.materialIcons(),
),
pageFormat: PdfPageFormat.a4,
build: (pw.Context context) {
return pw.Center(
child: pw.Text("Hello World"),
);
},
),
);
这个答案的来源在这里
https://github.com/DavBfr/dart_pdf/blob/master/demo/lib/examples/resume.dart
【讨论】:
要在 Pdf 包中使用 Material Icons,您只需按照此处的代码 sn-ps 操作即可。
将material导入为mt,将pdf导入为dynamic
import 'package:flutter/material.dart' as mt;
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart';
现在试试下面的代码,它会完美运行。
Icon(IconData(mt.Icons.check.codePoint),
color:
value PdfColors.grey,
size: 18,
)
【讨论】: