Flutter 如何将数据传递到PDF页面生成函数
我正在构建我的第一个颤振应用程序 我从屏幕上的浮动操作按钮调用了这个函数,但我不知道如何将数据对象传递给这个函数 有人能帮忙吗?该函数不接受任何参数 我这样调用函数:Flutter 如何将数据传递到PDF页面生成函数,flutter,Flutter,我正在构建我的第一个颤振应用程序 我从屏幕上的浮动操作按钮调用了这个函数,但我不知道如何将数据对象传递给这个函数 有人能帮忙吗?该函数不接受任何参数 我这样调用函数: FloatingActionButton( heroTag: 1, backgroundColor: Colors.yellow,
FloatingActionButton(
heroTag: 1,
backgroundColor: Colors.yellow,
onPressed: () {
Printing.layoutPdf(onLayout: buildReceipt);
},
child: Icon(FontAwesomeIcons.print),
),
我这样称呼它:
FloatingActionButton(
heroTag: 1,
backgroundColor: Colors.yellow,
onPressed: () {
Printing.layoutPdf(onLayout: buildReceipt);
},
child: Icon(FontAwesomeIcons.print),
),
这是我的密码:
import 'dart:async';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart';
Future<List<int>> buildReceipt(PdfPageFormat format) async {
final Document pdf = Document();
pdf.addPage(MultiPage(
pageFormat: PdfPageFormat.a4.copyWith(marginBottom: 1.5 * PdfPageFormat.cm),
crossAxisAlignment: CrossAxisAlignment.start,
header: (Context context) {
if (context.pageNumber == 1) {
return null;
}
return Container(
alignment: Alignment.centerRight,
margin: const EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
padding: const EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
decoration: const BoxDecoration(
border:
BoxBorder(bottom: true, width: 0.5, color: PdfColors.grey)),
child: Text('Portable Document Format',
style: Theme.of(context)
.defaultTextStyle
.copyWith(color: PdfColors.grey)));
},
footer: (Context context) {
return Container(
alignment: Alignment.centerRight,
margin: const EdgeInsets.only(top: 1.0 * PdfPageFormat.cm),
child: Text('Page ${context.pageNumber} of ${context.pagesCount}',
style: Theme.of(context)
.defaultTextStyle
.copyWith(color: PdfColors.grey)));
},
build: (Context context) => <Widget>[
Header(
level: 0,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Portable Document Format', textScaleFactor: 2),
PdfLogo()
])),
Padding(padding: const EdgeInsets.all(10)),
Paragraph(
text:
'I want to display this ${data}')
]));
return pdf.save();
}
导入'dart:async';
输入‘package:pdf/pdf.dart’;
导入“package:pdf/widgets.dart”;
Future BuildReceive(PdfPageFormat格式)异步{
最终文件pdf=文件();
pdf.addPage(多页)(
pageFormat:PdfPageFormat.a4.copyWith(marginBottom:1.5*PdfPageFormat.cm),
crossAxisAlignment:crossAxisAlignment.start,
标题:(上下文){
if(context.pageNumber==1){
返回null;
}
返回容器(
对齐:alignment.centerRight,
边距:仅限常量边集(底部:3.0*PdfPageFormat.mm),
填充:仅限常量边集(底部:3.0*PdfPageFormat.mm),
装饰:康斯特盒子装饰(
边界:
BoxBorder(底部:真,宽度:0.5,颜色:PdfColors.grey)),
子项:文本('可移植文档格式',
风格:主题(上下文)
.defaultTextStyle
.copyWith(颜色:PdfColors.grey));
},
页脚:(上下文){
返回容器(
对齐:alignment.centerRight,
边距:仅限常量边集(顶部:1.0*PdfPageFormat.cm),
child:Text(${context.pageScont}的第${context.pageNumber}页,
风格:主题(上下文)
.defaultTextStyle
.copyWith(颜色:PdfColors.grey));
},
构建:(上下文)=>[
标题(
级别:0,
孩子:排(
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
Text('可移植文档格式',textScaleFactor:2),
PdfLogo()
])),
填充(填充:常量边集所有(10)),
段落(
正文:
'我想显示此${data}')
]));
返回pdf.save();
}
您好,您可以将代码更改为
FloatingActionButton(
heroTag: 1,
backgroundColor: Colors.yellow,
onPressed: () {
Printing.layoutPdf(onLayout:(format)=> buildReceipt(format, data1, data2, data3));
},
child: Icon(FontAwesomeIcons.print),
),
然后还更改您的函数以匹配方法调用
Future<List<int>> buildReceipt(PdfPageFormat format, data1, data2, data3) async {
//bla bla bla
}
Future BuildReceive(PdfPageFormat格式,data1、data2、data3)异步{
//呜呜呜呜
}
您好,您可以将代码更改为
FloatingActionButton(
heroTag: 1,
backgroundColor: Colors.yellow,
onPressed: () {
Printing.layoutPdf(onLayout:(format)=> buildReceipt(format, data1, data2, data3));
},
child: Icon(FontAwesomeIcons.print),
),
然后还更改您的函数以匹配方法调用
Future<List<int>> buildReceipt(PdfPageFormat format, data1, data2, data3) async {
//bla bla bla
}
Future BuildReceive(PdfPageFormat格式,data1、data2、data3)异步{
//呜呜呜呜
}