首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Dart / Flutter pdf库中使用drawBox、drawEllipse或drawLine方法

如何在Dart / Flutter pdf库中使用drawBox、drawEllipse或drawLine方法
EN

Stack Overflow用户
提问于 2021-05-31 15:08:42
回答 1查看 293关注 0票数 2

我想要生成PDF文件,包括自行绘制的图形在颤振应用程序。当然,在pdf库中,显示包含两行文本的pdf预览非常简单,但我希望能够插入一些我想自己绘制的图形,因为我需要(自己)绘制一些非常非常规的图形。为了做到这一点,我需要能够在pdf小部件中绘制(一些线条、曲线、点、几种颜色等)。到现在为止,我甚至没有画出一点!,pdf的颤振飞镖库描述了几十种方法,但没有显示任何例子,这实际上是一个小问题。有没有人能帮我“画”PDF飞镖内的图形。PdfLibrary包括PdfGraphics类,它应该有我尝试使用的方法,但没有成功!!

许多人提前感谢

请找到我的密码:

代码语言:javascript
复制
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';

void main() => runApp(const MyApp('Ceci est mon premier PDF'));

class MyApp extends StatelessWidget {
  const MyApp(this.title);

  final String title;

  @override
  Widget build(BuildContext context) {

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text(title)),
        body: PdfPreview(
          build: (format) => _generatePdf(format, title),
        ),
      ),
    );
  }

  Future<Uint8List> _generatePdf(PdfPageFormat format, String title) async {
    final pdf = pw.Document();

    pdf.addPage(
      pw.Page(
        pageFormat: format,
        build: (context) {
          return pw.Center(
            child: pw.Column (
              children: [
                pw.Text(title),
                pw.Text(title),
                //pw.drawBox(10,10,100,100),     <---- if i remove the comment the app 
                                                       crashes saying "Method not found" 
                                                       otherwise i have a PDF generated with two 
                                                       lines of text, and i want that just under a 
                                                       self drawn graphic could be displayed 

              ],
            ),
          );//pw.Text(title),

        },
      ),
    );

    return pdf.save();
  }
}
EN

回答 1

Stack Overflow用户

发布于 2022-06-27 16:00:19

您必须使用CustomPaint类,而不是直接绘图。

试一试

代码语言:javascript
复制
pw.CustomPaint(
  size: const PdfPoint(110, 110),
  painter: (PdfGraphics canvas, PdfPoint size) {
    canvas
      ..setColor(PdfColors.indigo)
      ..drawRect(10, 10, 100, 100)
      ..fillPath();
  },
);

而不是

代码语言:javascript
复制
pw.drawBox(10,10,100,100)

查看此处可绘制形状的列表:https://pub.dev/documentation/pdf/latest/pdf/PdfGraphics-class.html

为那些寻找更多信息的人

CustomPaint期望一个孩子和一两个画家函数。不像颤振的CustomPaint

  • 这使用PdfGraphics而不是画布。
  • 画图函数不是CustomPainters函数

我挣扎了几天才弄明白这一点,并在以下评论中找到了答案:pdf/issues/145#发行-530798498

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67776282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档