Flutter 如何在颤振中覆盖pdf文件?

Flutter 如何在颤振中覆盖pdf文件?,flutter,pdf,pdf-generation,flutter-dependencies,saving-data,Flutter,Pdf,Pdf Generation,Flutter Dependencies,Saving Data,我正在创建一个pdf文件,像这样的写入方法。。。使用此软件包 writeOnPdf(上下文)异步{ final roboto=wait rootBundle.load('assets/fonts/roboto Regular.ttf'); var roboto=pw.Font.ttf(roboto); final robotobold=wait rootBundle.load('assets/fonts/Roboto Bold.ttf'); var robotoB=pw.Font.ttf(rob

我正在创建一个pdf文件,像这样的写入方法。。。使用此软件包

writeOnPdf(上下文)异步{
final roboto=wait rootBundle.load('assets/fonts/roboto Regular.ttf');
var roboto=pw.Font.ttf(roboto);
final robotobold=wait rootBundle.load('assets/fonts/Roboto Bold.ttf');
var robotoB=pw.Font.ttf(robotobold);
pdf.addPage(pw.MultiPage(
主题:pw.ThemeData.withFont(
基地:机器人,
粗体:机器人,
),
页面格式:PdfPageFormat.a4,
裕度:从LTRB(24,24,24,24)开始的边沿集,
//标题:,
maxPages:1,
构建:(pw.Context){
返回[
pw.柱(
儿童:[
容器(
身高:40,
颜色:PdfColors.grey200,
填充:从LTRB(15,0,15,0)开始的pw.EdgeInsets,
孩子:世界银行(
mainAxisAlignment:pw.mainAxisAlignment.spaceBetween,
儿童:[
扩大的(
child:pw.Text(
“说明/项目”,
样式:pw.TextStyle(
字体:robotoB,
//fontWeight:pw.fontwweight.bold,
),
),
),
容器(
宽度:1,
身高:25,
颜色:PdfColors.black,
填充:从LTRB(10,0,10,0)开始的pw.EdgeInsets,
),
容器(
宽度:70,
孩子:普华永道中心(
child:pw.Text(
“Qt.$数量”,
textAlign:pw.textAlign.center,
样式:pw.TextStyle(
字体:robotoB,
//fontWeight:pw.fontwweight.bold,
),
),
),
),
容器(
宽度:1,
身高:25,
颜色:PdfColors.black,
填充:从LTRB(10,0,10,0)开始的pw.EdgeInsets,
),
容器(
宽度:120,
子:pw.Align(
对齐:pw.alignment.centerRight,
child:pw.Text(
“$Amount”,
textAlign:pw.textAlign.right,
样式:pw.TextStyle(
字体:robotoB,
//fontWeight:pw.fontwweight.bold,
),
),
),
),
],
),
),
],
),
];
},
));
}
现在我用下面的函数保存文件

Future<File> savePDF() async {
    final appPath = Directory("storage/emulated/0/Myxyzapp/Invoices").path;
    pdfFile = File('$appPath/myxyzapp-invoice-$invoiceNo.pdf');
    await pdfFile.writeAsBytes(pdf.save());
    return pdfFile;
  }

    InkWell(
  onTap: () async {
    await writeOnPdf(context);
    await savePDF().then((value) {
      showDialog(
          context: (context),
          builder: (context) => AlertDialog(
                shape: ContinuousRectangleBorder(
                    borderRadius:
                        BorderRadius.circular(20)),
                content: Text(
                    'Invoice PDF regenerated successfully.'),
                actions: <Widget>[
                  FlatButton(
                    onPressed: () => Navigator.pop(
                        context, true),
                    child: Text(
                      "OK",
                      style:
                          TextStyle(fontSize: 14),
                    ),
                  ),
                ],
              ));
    });
  },
  child: Container(
    padding: EdgeInsets.fromLTRB(10, 7, 10, 7),
    decoration: BoxDecoration(
        color: Colors.grey[200],
        borderRadius: BorderRadius.circular(5)),
    child: Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(
          'Re-generate PDF ',
          style: TextStyle(
            color: Colors.black54,
            fontSize: 14,
          ),
        ),
        Icon(
          Icons.insert_drive_file,
          color: Colors.black54,
          size: 18,
        ),
      ],
    ),
  ),
),
Future savePDF()异步{
最终应用路径=目录(“存储/仿真/0/Myxyzapp/Invoices”).path;
pdfFile=File(“$appPath/myxyzapp invoice-$invoiceNo.pdf”);
等待pdfFile.writeAsBytes(pdf.save());
返回pdfFile;
}
墨水池(
onTap:()异步{
等待写入PDF(上下文);
等待savePDF()。然后((值){
显示对话框(
上下文:(上下文),
生成器:(上下文)=>AlertDialog(
形状:连续矩形边框(
边界半径:
边界半径。圆形(20)),
内容:文本(
“发票PDF已成功重新生成。”),
行动:[
扁平按钮(
onPressed:()=>Navigator.pop(
上下文,正确),
子:文本(
“好的”,
风格:
文本样式(字体大小:14),
),
),
],
));
});
},
子:容器(
填充:来自LTRB(10,7,10,7)的边缘设置,
装饰:盒子装饰(
颜色:颜色。灰色[200],
边界半径:边界半径。圆形(5)),
孩子:排(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
正文(
“重新生成PDF”,
样式:TextStyle(
颜色:颜色。黑色54,
尺寸:14,
),
),
图标(
Icons.insert_驱动器_文件,
颜色:颜色。黑色54,
尺码:18,
),
],
),
),
),
但是当我重新生成这个文件并试图打开它时,它仍然显示以前的数据

注意:我所有的flatter代码和writeToPdf()小部件都在同一页上。。。基本上,我无法将它们分开,因为我正在从服务器获取数据并创建动态PDF文件

我试过:

  • 通过“pdfFile.delete();”删除旧文件,然后将文件保存到路径
  • 只有当我从文件夹中手动删除文件,然后再次打开应用程序并生成我的pdf时,这才有效


    有任何建议或建议我在哪里出错吗?

    您只需检查文件是否已存在,然后将其删除即可

    await File( YOURFILE ).exists();
    

    如果返回true,则使用以下命令删除文件:

    await File( YOURFILE ).delete();
    

    此时,您可以创建具有相同名称和路径的新Pdf文件


    注意使用pdfFile.delete()时,您删除的不是已经存在的文件,而是刚刚创建的文件。

    我尝试过,但再次得到相同的结果。。。如果我在1-2分钟后生成,或者在关闭并重新打开应用程序并重新生成时,它才起作用。
    File( YOURFILE ).existsSync();
    
    await File( YOURFILE ).delete();
    
    File( YOURFILE ).deleteSync();