发布于 2018-09-30 22:00:39
当我实现PDF查看器的功能时,没有PDF插件。
然而,有趣的是,工作中的一个朋友发现,已经有一个PDF查看器实现了颤振这里,我们最终使用了它。
注:在撰写问题时,16.08还没有任何插件可用。上述是在30.08上创建的。
发布于 2020-06-28 00:04:14
如果您正在寻找快速和简单的方式显示PDF,这可能是一个。
从网络加载PDF:
PDF.network(
'https://raw.githubusercontent.com/FlutterInThai/Dart-for-Flutter-Sheet-cheet/master/Dart-for-Flutter-Cheat-Sheet.pdf',
height: 500,
width: 300,
)
从档案中载入PDF:
File fileName;
PDF.file(
fileName,
height: 200,
width: 100,
)
从资产加载PDF:
PDF.assets(
"assets/pdf/demo.pdf",
height: 200,
width: 100,
)
发布于 2020-02-24 17:48:59
在pubspec.yaml中添加依赖项
pubspec.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,
),
],
),
),
),
);
}
}
https://stackoverflow.com/questions/51835013
复制相似问题