Flutter如何在ios中打印到打印机
我正在制作一个用 flutter 在 android 和 ios 中打印的函数。 我在搜索打印插件时发现了打印
。
我已经通过这个插件中的示例实现了pdf创建和打印。
在Android中,打印正常,但在IOS中,出现错误。
错误
我认为这是 MaterialApp 和 CupertinoApp 中的小部件的问题,具体取决于平台。
所以我根据平台设置了Widget。
按钮小部件
Widget _wifiButton(BuildContext context){
return Expanded(
flex: 1,
child: Container(
width: this.size!.width,
height: this.size!.height,
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(8.0),
),
child: Platform.isAndroid ? TextButton(
onPressed: () async {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => WifiPrint())
);
},
child: Text(
'W i - F i',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white
),
),
) : CupertinoButton(
onPressed: () async {
Navigator.push(
context,
CupertinoPageRoute(builder: (context) => WifiPrint()),
);
},
child: Text(
'W i - F i',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white
),
),
),
),
);
}
类 WifiPrint
@override
Widget build(BuildContext context) {
return Platform.isAndroid
? MaterialApp(
home: Scaffold(
body: PdfPreview(
build: (format) => _generatePdf(format, context),
),
)
)
: CupertinoApp(
home: CupertinoPageScaffold(
child: PdfPreview(
build: (format) => _generatePdf(format, context),
),
),
);
}
Future<Uint8List> _generatePdf(PdfPageFormat format,
BuildContext context) async {
final pdf = pdfWidget.Document(version: PdfVersion.pdf_1_5, compress: true);
//final String documentPath = (await getTemporaryDirectory()).path;
for (int nCount = 0; nCount < Provider.of<ScoreProvider>(context, listen: false).scoreImageList.length; nCount++) {
final image = await imageFromAssetBundle('${Provider.of<ScoreProvider>(context, listen: false).scoreImageList[nCount]}');
pdf.addPage(
pdfWidget.Page(
pageFormat: format,
build: (pdfWidget.Context context) {
return pdfWidget.Center(
child: pdfWidget.Padding(
padding: pdfWidget.EdgeInsets.zero,
child: pdfWidget.Image(image, fit: pdfWidget.BoxFit.fill),
),
);
}
)
);
}
return pdf.save();
}
但我遇到了同样的错误。你知道如何解决这个问题吗?
I'm making a function to print in android and ios with flutter.
I found printing
while searching for a printing plugin.
I have implemented pdf creation and printing through examples in this plugin.
In Android, it was printed normally, but in IOS, an error occurred.
Error
I thought this was an issue with the widgets in the MaterialApp and CupertinoApp depending on the platform.
So I set the Widget according to the Platform.
button widget
Widget _wifiButton(BuildContext context){
return Expanded(
flex: 1,
child: Container(
width: this.size!.width,
height: this.size!.height,
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(8.0),
),
child: Platform.isAndroid ? TextButton(
onPressed: () async {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => WifiPrint())
);
},
child: Text(
'W i - F i',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white
),
),
) : CupertinoButton(
onPressed: () async {
Navigator.push(
context,
CupertinoPageRoute(builder: (context) => WifiPrint()),
);
},
child: Text(
'W i - F i',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white
),
),
),
),
);
}
class WifiPrint
@override
Widget build(BuildContext context) {
return Platform.isAndroid
? MaterialApp(
home: Scaffold(
body: PdfPreview(
build: (format) => _generatePdf(format, context),
),
)
)
: CupertinoApp(
home: CupertinoPageScaffold(
child: PdfPreview(
build: (format) => _generatePdf(format, context),
),
),
);
}
Future<Uint8List> _generatePdf(PdfPageFormat format,
BuildContext context) async {
final pdf = pdfWidget.Document(version: PdfVersion.pdf_1_5, compress: true);
//final String documentPath = (await getTemporaryDirectory()).path;
for (int nCount = 0; nCount < Provider.of<ScoreProvider>(context, listen: false).scoreImageList.length; nCount++) {
final image = await imageFromAssetBundle('${Provider.of<ScoreProvider>(context, listen: false).scoreImageList[nCount]}');
pdf.addPage(
pdfWidget.Page(
pageFormat: format,
build: (pdfWidget.Context context) {
return pdfWidget.Center(
child: pdfWidget.Padding(
padding: pdfWidget.EdgeInsets.zero,
child: pdfWidget.Image(image, fit: pdfWidget.BoxFit.fill),
),
);
}
)
);
}
return pdf.save();
}
But I got the same error. Do you know how to solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用 Material() 包装 DropdownButton
您还可以将 pdf 分配给新文档,然后添加到其中。
示例:最终文档 pdf = Document();
wrap the DropdownButton with Material()
you could also assign pdf to a new document, then add to it.
example: final Document pdf = Document();
已解决
Solved