【问题标题】:How to open pdf, pptx, doc, docx files in flutter app?如何在 Flutter 应用中打开 pdf、pptx、doc、docx 文件?
【发布时间】:2020-02-12 14:03:47
【问题描述】:

如果我点击文件,如何在 Flutter 应用程序中打开该文件。 这就是我所做的:

这个show的代码在listView中运行:

ListView.builder(
              scrollDirection: Axis.vertical,
              shrinkWrap: true,
              itemCount: attachments.length,
              itemBuilder: (BuildContext ctxt, int index) {
                print (index);
                return ListView(
                    scrollDirection: Axis.vertical,
                    shrinkWrap: true,
                    children: <Widget>[
                      Padding(
                        padding: const EdgeInsets.all(1.0),
                        child: ListTile(
                          leading: CircleAvatar(
                            child: _getIcon(
                                attachments[index].split('/').removeLast()),
                            backgroundColor: Colors.white,
                          ),
                          subtitle:
                          Text(attachments[index].split('/').removeLast()),
                          onTap: (){}
                        ),
                      ),
                    ]);
              })

我使用了 open_file 包,但它不起作用这是我所做的代码:

            onTap: () async {
            String fileUrl = widget.schoolDcUrl + "/container"attachments[index];
            final _openFile = await OpenFile.open(fileUrl);
             Logger.i(_openFile);
                              }

当我点击文件时,它不会打开文件,它会提示找不到终端文件。

我希望 onTap 在应用程序中打开该文件。

【问题讨论】:

  • 在应用程序中打开文件需要大量代码才能处理所有格式,强烈建议使用专用应用程序,您可以使用:pub.dev/packages/open_file
  • 我使用了 open_file 包,但它不起作用——这是我在 onTap 上完成的代码:() async { String fileUrl = widget.schoolDcUrl + "/container" + attachments[index];字符串 fileUrl1 = Uri.encodeFull(fileUrl); final _openFile = 等待 OpenFile.open(fileUrl);记录器.i(_openFile); }
  • 这是完美的答案stackoverflow.com/a/60203399/15601357,没有任何样板

标签: flutter dart


【解决方案1】:

pubspec.yaml 中添加:

dev_dependencies:
   open_file: ^3.0.1

在代码中添加:

import 'package:open_file/open_file.dart';

OpenFile.open("/sdcard/example.pptx");


open_file 文档:
https://pub.dev/packages/open_file#-readme-tab-

【讨论】:

    【解决方案2】:

    您应该能够使用打开所有这些文件格式的flutter_filereader 包。

    确保遵循 README 中的配置和示例。

    【讨论】:

    • 我的文件来自 api--- String fileUrl = widget.schoolDcUrl + "/container"attachments[index];这个文件路径我们可以使用 filereader 包打开它吗
    • 我不明白你的问题。
    • 这是我的文件地址:String fileUrl = widget.schoolDcUrl + "/container"attachments[index];现在我点击文件如何在颤振应用程序中打开
    • 您是否尝试使用我在回答中链接的包?
    • 文件是本地的还是远程的?
    【解决方案3】:

    无需下载文件,您可以使用包从 URL 打开:url_launcher 5.4.1

    _launchURL(String url) async {
      const url = url; // you can use your url
      if (await canLaunch(url)) {
        await launch(url);
      } else {
        throw 'Unable to open url : $url';
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-27
      • 2010-12-06
      • 2013-10-30
      • 2020-10-03
      • 2014-01-24
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      • 1970-01-01
      相关资源
      最近更新 更多