在Flatter中显示iOS的PDF内联文件

在Flatter中显示iOS的PDF内联文件,ios,pdf,flutter,Ios,Pdf,Flutter,我正在为iOS专门开发一个应用程序,我需要在其中添加PDF文件。问题是颤振没有显示PDF文件的原生方式(据我所研究) 由此看来,使用插件向iOS设备添加PDF支持应该不会太难。然而,我仍然对如何将其集成到颤振应用程序中感到困惑 任何帮助都将不胜感激 根据您的应用程序支持的最低版本,如果是iOS 11或更高版本,您可以使用PDFKit。否则,WebKit也是一个不错的选择。当我实现PDF viewer的功能时,没有PDF插件 然而,有趣的是,一位工作的朋友发现,已经有一个PDF查看器实现了颤振,我

我正在为iOS专门开发一个应用程序,我需要在其中添加PDF文件。问题是颤振没有显示PDF文件的原生方式(据我所研究)

由此看来,使用插件向iOS设备添加PDF支持应该不会太难。然而,我仍然对如何将其集成到颤振应用程序中感到困惑


任何帮助都将不胜感激

根据您的应用程序支持的最低版本,如果是iOS 11或更高版本,您可以使用
PDFKit
。否则,
WebKit
也是一个不错的选择。

当我实现PDF viewer的功能时,没有PDF插件

然而,有趣的是,一位工作的朋友发现,已经有一个PDF查看器实现了颤振,我们最终使用了它


注意:在写问题的时候,16.08还没有任何可用的插件。所提到的是在2008年8月30日创建的。

在pubspec.yaml中添加依赖项

公共规范yaml

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2
  pdf_viewer_plugin: ^1.0.0+2
  path_provider: ^1.6.1
  http: ^0.12.0+4
main.dart

import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:pdf_viewer_plugin/pdf_viewer_plugin.dart';
import 'package:path_provider/path_provider.dart';
import 'package:http/http.dart' as http;

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
  String path;
  @override
  initState() {
    super.initState();
  }
  Future<String> get _localPath async {
    final directory = await getApplicationDocumentsDirectory();

    return directory.path;
  }

  Future<File> get _localFile async {
    final path = await _localPath;
    return File('$path/teste.pdf');
  }

  Future<File> writeCounter(Uint8List stream) async {
    final file = await _localFile;
    // Write the file
    return file.writeAsBytes(stream);
  }

  Future<Uint8List> fetchPost() async {
    final response = await http.get(
        'https://expoforest.com.br/wp-content/uploads/2017/05/exemplo.pdf');
    final responseJson = response.bodyBytes;

    return responseJson;
  }

  loadPdf() async {
    writeCounter(await fetchPost());
    path = (await _localFile).path;

    if (!mounted) return;

    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: <Widget>[
              if (path != null)
                Container(
                  height: 300.0,
                  child: PdfViewer(
                    filePath: path,
                  ),
                )
              else
                Text("Pdf is not Loaded"),
              RaisedButton(
                child: Text("Load pdf"),
                onPressed: loadPdf,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
导入'dart:async';
导入“dart:io”;
导入“dart:键入的_数据”;
进口“包装:颤振/材料.省道”;
导入“package:pdf_viewer_plugin/pdf_viewer_plugin.dart”;
导入“package:path_provider/path_provider.dart”;
将“package:http/http.dart”导入为http;
void main()=>runApp(MyApp());
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
字符串路径;
@凌驾
initState(){
super.initState();
}
未来获取\u本地路径异步{
最终目录=等待getApplicationDocumentsDirectory();
返回directory.path;
}
未来获取\u本地文件异步{
最终路径=等待_localPath;
返回文件(“$path/teste.pdf”);
}
未来写计数器(Uint8List流)异步{
final file=wait\u localFile;
//写文件
返回文件.writeAsBytes(流);
}
Future fetchPost()异步{
最终响应=等待http.get(
'https://expoforest.com.br/wp-content/uploads/2017/05/exemplo.pdf');
final responseJson=response.bodyBytes;
返回响应;
}
loadPdf()异步{
writeCounter(等待fetchPost());
路径=(等待_localFile).path;
如果(!已安装)返回;
setState((){});
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:appBar(
标题:文本(“插件示例应用程序”),
),
正文:中(
子:列(
儿童:[
if(路径!=null)
容器(
高度:300.0,
儿童:PdfViewer(
filePath:path,
),
)
其他的
文本(“未加载Pdf”),
升起的按钮(
子项:文本(“加载pdf”),
onPressed:loadPdf,
),
],
),
),
),
);
}
}

如果您正在寻找快速简便的PDF显示方式,这可能就是其中之一

从网络加载PDF: 从以下文件加载PDF: 从资源加载PDF:

没有工作代码-
将此插件添加到pubspec.yaml-
>>颤振\u完整\u pdf\u查看器:^1.0.6
在“警报”对话框中单击“积极”按钮“我重定向”以查看pdf-
未来onPositiveButtonClick()异步{
字符串_filePath='';
String _directory=wait ExtStorage.getExternalStoragePublicDirectory(ExtStorage.directory_下载);
//todo geeting此处的右目录路径
打印(“目录”+_目录);
_filePath='$_目录/$fileName';
pushReplacement(context,MaterialPageRoute(builder:(context)=>ActivityPdfView(_filePath));
}
这是我的ActivityPDView,用于显示pdf-
进口“包装:颤振/cupertino.dart”;
进口“包装:颤振/材料.省道”;
导入“package:flatter_full_pdf_viewer/flatter_full_pdf_viewer.dart”;
类ActivityPDView扩展StatefulWidget{
字符串文件路径;
ActivityPDView(字符串文件路径){
this.filePath=filePath;
}
@凌驾
_ActivityDfViewState createState()=>\u ActivityDfViewState();
}
类_ActivityPDViewState扩展状态{
@凌驾
小部件构建(构建上下文){
返回材料PP(
home:MyPDFList(widget.filePath),//调用MyPDF列表文件
);
}
}
类MyPDFList扩展了无状态小部件{
字符串pathPDF=“”;
MyPDFList(字符串文件路径){
this.pathPDF=文件路径;
}
@凌驾
小部件构建(构建上下文){
返回PDFViewerScaffold(
appBar:appBar(
标题:文本(“文件”),
背景颜色:Colors.deeporangeacent,
),
路径:pathPDF
);
}
}

如何使用它们?如何导入,
import'包:…'
没有任何与PDF相关的内容。文件编制也力求迅速和客观-C@TunaTheDog这是一个关于颤振的应用程序。实际上没有文档。你能给我们一个小样本吗?@AGoranov使用这段代码在android设备上显示pdf很好
PDF.network(
        'https://raw.githubusercontent.com/FlutterInThai/Dart-for-Flutter-Sheet-cheet/master/Dart-for-Flutter-Cheat-Sheet.pdf',
        height: 500,
        width: 300,
        )
File fileName;  
PDF.file(
    fileName,
    height: 200,
    width: 100,
)
PDF.assets(
    "assets/pdf/demo.pdf",
    height: 200,
    width: 100,
)
there working code -

Add this plugin to pubspec.yaml - 

>> flutter_full_pdf_viewer: ^1.0.6

on alert dialog positive button click I redirecting to view pdf -

 Future<void> onPositiveButtonClick() async {
    String _filePath = '';

String _directory = await ExtStorage.getExternalStoragePublicDirectory(ExtStorage.DIRECTORY_DOWNLOADS);
    //todo geeting right directory path here
    print("directory" + _directory);
    _filePath = '$_directory/$fileName';

  Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => ActivityPdfView(_filePath)));

  }

and this is my Activitypdfview to show pdf -

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_full_pdf_viewer/flutter_full_pdf_viewer.dart';

class ActivityPdfView extends StatefulWidget {

  String filePath;

  ActivityPdfView(String filePath){
    this.filePath = filePath;
}

  @override
  _ActivityPdfViewState createState() => _ActivityPdfViewState();
}

class _ActivityPdfViewState extends State<ActivityPdfView> {
  @override
  Widget build(BuildContext context) {
    return  MaterialApp(
      home: MyPDFList(widget.filePath), //call MyPDF List file
    );
  }
}

class MyPDFList extends StatelessWidget {

  String pathPDF = "";
  MyPDFList(String filePath){
    this.pathPDF = filePath;
  }

  @override
  Widget build(BuildContext context) {

    return PDFViewerScaffold(
        appBar: AppBar(
          title: Text("Document"),
          backgroundColor: Colors.deepOrangeAccent,
        ),
        path: pathPDF
    );
  }

}