pdf plugin
a flutter plugin for ios and android providing a simple way to display pdfs.
features:
display pdf.
installation
first, add pdf_viewer_plugin
as a dependency in your pubspec.yaml file.
ios
add one row to the ios/runner/info.plist
:
...
<key>io.flutter.embedded_views_preview</key>
<true/>
you need to
example
here is an example flutter app.
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,
),
],
),
),
),
);
}
}
Comments are closed.