native_pdf_viewer_plugin 0.0.2 native_pdf_viewer_plugin: ^0.0.2 copied to clipboard
A new flutter plugin project.
import 'dart:io';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pdf_viewer_plugin/pdf_viewer_scaffold.dart';
void main() {
runApp(MaterialApp(
title: 'Plugin example app',
home: MyApp(),
));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String pathPDF = "";
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
void initPlatformState() {
createFileOfPdfUrl().then((f) {
setState(() {
pathPDF = f.path;
print(pathPDF);
});
});
}
Future<File> createFileOfPdfUrl() async {
String dir = (await getApplicationDocumentsDirectory()).path;
final url = "http://africau.edu/images/default/sample.pdf";
final filename = url.substring(url.lastIndexOf("/") + 1);
File file = new File('$dir/$filename');
if (await file.exists()) {
return file;
}
var request = await HttpClient().getUrl(Uri.parse(url));
var response = await request.close();
var bytes = await consolidateHttpClientResponseBytes(response);
await file.writeAsBytes(bytes);
return file;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Plugin example app')),
body: Center(
child: RaisedButton(
child: Text("Open PDF"),
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (context) => PDFScreen(pathPDF)),
),
),
),
);
}
}
class PDFScreen extends StatelessWidget {
String pathPDF = "";
String password = "";
PDFScreen(this.pathPDF);
GlobalKey<PDFViewScaffoldState> _state = GlobalKey<PDFViewScaffoldState>();
@override
Widget build(BuildContext context) {
return PDFViewerScaffold(
key: _state,
appBar: AppBar(
title: Text("Document"),
actions: <Widget>[
IconButton(
icon: Icon(Icons.share),
onPressed: () {
_state.currentState.shareFile(pathPDF);
},
),
],
),
path: pathPDF,
pass: password,
mode: "fromFile");
}
}