Flutter如何在ios中打印到打印机

发布于 2025-01-10 15:01:54 字数 3151 浏览 0 评论 0原文

我正在制作一个用 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

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紙鸢 2025-01-17 15:01:54

用 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();

云醉月微眠 2025-01-17 15:01:54

已解决

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: TextButton(
          onPressed: () {
            _generatePdf(PdfPageFormat.a4, context);
          },
          child: Text(
            'W i - F i',
            style: TextStyle(
                fontWeight: FontWeight.bold,
                color: Colors.white
            ),
          ),
        ),
      ),
    );
  }

 _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();
  }

Solved

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: TextButton(
          onPressed: () {
            _generatePdf(PdfPageFormat.a4, context);
          },
          child: Text(
            'W i - F i',
            style: TextStyle(
                fontWeight: FontWeight.bold,
                color: Colors.white
            ),
          ),
        ),
      ),
    );
  }

 _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();
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文