flutter_full_pdf_viewer_null_safe 1.0.0
flutter_full_pdf_viewer_null_safe: ^1.0.0 copied to clipboard
A flutter plugin to display PDF. This works for Android and for iOS.
import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_full_pdf_viewer_null_safe/full_pdf_viewer_scaffold.dart';
import 'package:path_provider/path_provider.dart';
void main() {
runApp(MaterialApp(
title: 'Plugin example app',
home: MyApp(),
));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
String pathPDF = "";
@override
void initState() {
super.initState();
createFileOfPdfUrl().then((f) {
setState(() {
pathPDF = f.path;
print(pathPDF);
});
});
}
Future<File> createFileOfPdfUrl() async {
final url =
"https://firebasestorage.googleapis.com/v0/b/sikh-notes.appspot.com/o/purwqn%20inhMg%20isMGw%CB%86%20dI%20gurIlw%20Xu%60DnIqI%405%400%40Puratan%20Nihang%20Singha%20Di%20Gurella%20Yudhniti.pdf?alt=media&token=e4d04dfa-a21b-4ddd-8647-86fa43392f51";
final filename = url.substring(url.lastIndexOf("/") + 1);
var request = await HttpClient().getUrl(Uri.parse(url));
var response = await request.close();
var bytes = await consolidateHttpClientResponseBytes(response);
String dir = (await getApplicationDocumentsDirectory()).path;
File file = new File('$dir/$filename.pdf');
await file.writeAsBytes(bytes);
return file;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Plugin example app')),
body: Center(
child: ElevatedButton(
child: Text("Open PDF"),
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (context) => PDFScreen(pathPDF)),
),
),
),
);
}
}
class PDFScreen extends StatelessWidget {
final String pathPDF;
PDFScreen(this.pathPDF);
@override
Widget build(BuildContext context) {
return PDFViewerScaffold(
appBar: AppBar(
title: Text("Document"),
actions: <Widget>[
IconButton(
icon: Icon(Icons.share),
onPressed: () {},
),
],
),
path: pathPDF);
}
}