'How to print a List of Widgets to PDF
How can I print this list of widgets using the printing package?
I get an Unhandled Exception: type List<Widget> is not a subtype of type List<Widget> in type cast
The error I keep getting is that the packages do not match. I even tried casting the getBarcodes() as List<pw.Widget> to see if it would work.
In the end, what I want is a sheet of barcodes that I can print out. I think the problem is the SinglechildScrollview but I'm not certain of that.
class _BarcodeScreenState extends State<BarcodeScreen> {
final doc = pw.Document();
List<Widget> getBarcodes() {
List<Widget> barcodeList = List.generate(
3,
(index) => SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: List.generate(
3,
(index) => Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.black)),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: BarcodeWidget(
data:
'${DateFormat('MM d y').format(DateTime.now())} ${nanoid(5)}',
barcode: Barcode.code128(),
),
),
),
],
),
),
),
)));
return barcodeList;
}
Above is my list of widgets and below is the code I use to try and print it.
@override
Widget build(BuildContext context) {
return SteriCodeBoiler(
appBarTitle: "Barcodes",
print: IconButton(
icon: Icon(Icons.print),
onPressed:() async{
doc.addPage(pw.Page(
pageFormat: PdfPageFormat.a4,
build: (pw.Context context) {
return pw.Container(
child: pw.ListView(
children: getBarcodes()
)
);
}));
await Printing.layoutPdf(
onLayout: (PdfPageFormat format) async => doc.save());
},
),
screenContent: Container(
child: ListView(
children: getBarcodes(),
)),
);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
