因此,我得到错误“命名参数'body'是没有定义。”在我尝试使用到pdf函数打印一个表后,这里使用的主要包是package:pdf/widgets.dart' as pw。我创建了一个时间表,我想显示它,当我点击我的pdf按钮,但当我写的代码表内我的doc.addpage我得到的错误体是没有定义。
我的代码如下:
已导入包...
class TableWidget extends StatefulWidget {
const TableWidget({Key? key}) : super(key: key);
@override
_TableState createState() => _TableState();
}
class _TableState extends State<TableWidget> {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
automaticallyImplyLeading: true,
title: const Text('Timetable Generated'),
),
body: Center(
child: Column(children: <Widget>[
Container(
padding: const EdgeInsets.all(5),
margin: const EdgeInsets.all(5),
width: double.infinity,
child: Table(
defaultColumnWidth: const FlexColumnWidth(1.0),
border: TableBorder.all(
color: Colors.black, style: BorderStyle.solid, width: 1),
children: [
TableRow(children: [
Column(children: const [
Text('', style: TextStyle(fontSize: 20.0))
]),
Column(children: const [
Text('Lesson 1 \n 08:05 - 08:45',
style: TextStyle(fontSize: 15.0))
]),
Column(children: const [
Text('Lesson 2 \n 09:00 - 09:40',
style: TextStyle(fontSize: 15.0))
]),
Column(children: const [
Text('Lesson 3 \n 09:50 - 10:30',
style: TextStyle(fontSize: 15.0))
]),
Column(children: const [
Text('Lesson 4 \n 10:40 - 11:20',
style: TextStyle(fontSize: 15.0))
]),
Column(children: const [
Text('Lesson 5 \n 11:30 - 12:10',
style: TextStyle(fontSize: 15.0))
]),
Column(children: const [
Text('Lesson 6 \n 12:20 - 13:00',
style: TextStyle(fontSize: 15.0))
]),
Column(children: const [
Text('Lesson 7 \n 14:00 - 14:40',
style: TextStyle(fontSize: 15.0))
]),
]),
TableRow(children: [
Column(children: const [
Text('Mo', style: TextStyle(fontSize: 20.0))
]),
Column(children: const [
Text('Turkish', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Geography', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Physics', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Physics', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Maths', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Business', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Business', style: TextStyle(fontSize: 12.0))
]),
]),
TableRow(children: [
Column(children: const [
Text('Tu', style: TextStyle(fontSize: 20.0))
]),
Column(children: const [
Text('Portuguese', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Portuguese', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Chemistry', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Chemistry', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Biology', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('PE', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('PE', style: TextStyle(fontSize: 12.0))
]),
]),
TableRow(children: [
Column(children: const [
Text('We', style: TextStyle(fontSize: 20.0))
]),
Column(children: const [
Text('English', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('English', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Computer', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Portuguese', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Chemistry', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Biology', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('CVC', style: TextStyle(fontSize: 12.0))
]),
]),
TableRow(children: [
Column(children: const [
Text('Thu', style: TextStyle(fontSize: 20.0))
]),
Column(children: const [
Text('Biology', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Computer', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('English', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('English', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('History', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Maths', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Maths', style: TextStyle(fontSize: 12.0))
]),
]),
TableRow(children: [
Column(children: const [
Text('Fri', style: TextStyle(fontSize: 20.0))
]),
Column(children: const [
Text('Maths', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Maths', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Accounting', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Accounting', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Physics', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('-', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('-', style: TextStyle(fontSize: 12.0))
]),
]),
],
),
),
TextButton(
style: TextButton.styleFrom(
fixedSize: const Size.square(40),
padding: const EdgeInsets.all(2),
foregroundColor: Colors.white,
backgroundColor: Colors.green),
onPressed: _createExcel,
child: const Text('To Excel')),
TextButton(
style: TextButton.styleFrom(
fixedSize: const Size.square(30),
padding: const EdgeInsets.all(5),
foregroundColor: Colors.white,
backgroundColor: Colors.red,
),
onPressed: _createPdf,
child: const Text('To PDF')),
]))),
);
}
//Buttons
//Create PDF and display text
void _createPdf() async {
final doc = pw.Document();
/// for using an image from assets
// final image = await imageFromAssetBundle('assets/image.png');
doc.addPage(
pw.Page(
pageFormat: PdfPageFormat.a4,
build: (pw.Context context) {
return pw.Center(
body: Center(
child: Column(children: <Widget>[
Container(
padding: const EdgeInsets.all(5),
margin: const EdgeInsets.all(5),
width: double.infinity,
child: Table(
defaultColumnWidth: const FlexColumnWidth(1.0),
border: TableBorder.all(
color: Colors.black, style: BorderStyle.solid, width: 1),
children: [
TableRow(children: [
Column(children: const [
Text('', style: TextStyle(fontSize: 20.0))
]),
Column(children: const [
Text('Lesson 1 \n 08:05 - 08:45',
style: TextStyle(fontSize: 15.0))
]),
Column(children: const [
Text('Lesson 2 \n 09:00 - 09:40',
style: TextStyle(fontSize: 15.0))
]),
Column(children: const [
Text('Lesson 3 \n 09:50 - 10:30',
style: TextStyle(fontSize: 15.0))
]),
Column(children: const [
Text('Lesson 4 \n 10:40 - 11:20',
style: TextStyle(fontSize: 15.0))
]),
Column(children: const [
Text('Lesson 5 \n 11:30 - 12:10',
style: TextStyle(fontSize: 15.0))
]),
Column(children: const [
Text('Lesson 6 \n 12:20 - 13:00',
style: TextStyle(fontSize: 15.0))
]),
Column(children: const [
Text('Lesson 7 \n 14:00 - 14:40',
style: TextStyle(fontSize: 15.0))
]),
]),
TableRow(children: [
Column(children: const [
Text('Mo', style: TextStyle(fontSize: 20.0))
]),
Column(children: const [
Text('Turkish', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Geography', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Physics', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Physics', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Maths', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Business', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Business', style: TextStyle(fontSize: 12.0))
]),
]),
TableRow(children: [
Column(children: const [
Text('Tu', style: TextStyle(fontSize: 20.0))
]),
Column(children: const [
Text('Portuguese', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Portuguese', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Chemistry', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Chemistry', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Biology', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('PE', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('PE', style: TextStyle(fontSize: 12.0))
]),
]),
TableRow(children: [
Column(children: const [
Text('We', style: TextStyle(fontSize: 20.0))
]),
Column(children: const [
Text('English', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('English', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Computer', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Portuguese', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Chemistry', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Biology', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('CVC', style: TextStyle(fontSize: 12.0))
]),
]),
TableRow(children: [
Column(children: const [
Text('Thu', style: TextStyle(fontSize: 20.0))
]),
Column(children: const [
Text('Biology', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Computer', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('English', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('English', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('History', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Maths', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Maths', style: TextStyle(fontSize: 12.0))
]),
]),
TableRow(children: [
Column(children: const [
Text('Fri', style: TextStyle(fontSize: 20.0))
]),
Column(children: const [
Text('Maths', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Maths', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Accounting', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Accounting', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('Physics', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('-', style: TextStyle(fontSize: 12.0))
]),
Column(children: const [
Text('-', style: TextStyle(fontSize: 12.0))
]),
]),
],
),
),]))); // Center
},
),
);
await Printing.layoutPdf(
onLayout: (PdfPageFormat format) async => doc.save());
//To share to other apps
await Printing.sharePdf(
bytes: await doc.save(), filename: 'my-document.pdf');
}
1条答案
按热度按时间bvn4nwqk1#
您需要在此处将
body
更改为child
:而不是:
将其替换为:
只有
Scaffold
小组件接受body
属性来设置窗口小组件,但其他Flutter小组件和pw.Center
接受child
属性。