easy_pdf_viewer 1.0.3
easy_pdf_viewer: ^1.0.3 copied to clipboard
A flutter plugin for handling PDF files. Works on both Android & iOS
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:easy_pdf_viewer/easy_pdf_viewer.dart';
void main() => runApp(MyApp(
progressExample: true,
));
class MyApp extends StatefulWidget {
const MyApp({this.progressExample = false});
final bool progressExample;
@override
State<MyApp> createState() {
if (progressExample) return _MyAppStateWithProgress();
return _MyAppState();
}
}
class _MyAppStateWithProgress extends State<MyApp> {
bool _isLoading = true;
PDFDocument document;
DownloadProgress downloadProgress;
@override
void initState() {
loadDocument();
super.initState();
}
@override
void setState(VoidCallback fn) {
if (mounted) {
super.setState(fn);
}
}
void loadDocument() async {
/// Clears the cache before download, so [PDFDocument.fromURLWithDownloadProgress.downloadProgress()]
/// is always executed (meant only for testing).
await DefaultCacheManager().emptyCache();
PDFDocument.fromURLWithDownloadProgress(
'https://www.ecma-international.org/wp-content/uploads/ECMA-262_12th_edition_june_2021.pdf',
downloadProgress: (downloadProgress) => setState(() {
this.downloadProgress = downloadProgress;
}),
onDownloadComplete: (document) => setState(() {
this.document = document;
_isLoading = false;
}),
);
}
Widget buildProgress() {
if (downloadProgress == null) return SizedBox();
String parseBytesToKBs(int bytes) {
return '${(bytes / 1000).toStringAsFixed(2)} KBs';
}
String progressString =
parseBytesToKBs(downloadProgress.downloaded);
if (downloadProgress.totalSize != null) {
progressString += '/ ${parseBytesToKBs(downloadProgress.totalSize)}';
}
return Column(
children: [
SizedBox(height: 20),
Text(progressString),
],
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: _isLoading
? Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
CircularProgressIndicator(),
buildProgress(),
],
),
)
: PDFViewer(
document: document,
),
),
),
);
}
}
class _MyAppState extends State<MyApp> {
bool _isLoading = true;
PDFDocument document;
@override
void initState() {
super.initState();
loadDocument();
}
loadDocument() async {
document = await PDFDocument.fromAsset('assets/sample.pdf');
setState(() => _isLoading = false);
}
changePDF(value) async {
setState(() => _isLoading = true);
if (value == 1) {
document = await PDFDocument.fromAsset('assets/sample2.pdf');
} else if (value == 2) {
document = await PDFDocument.fromURL(
"https://www.ecma-international.org/wp-content/uploads/ECMA-262_12th_edition_june_2021.pdf",
/* cacheManager: CacheManager(
Config(
"customCacheKey",
stalePeriod: const Duration(days: 2),
maxNrOfCacheObjects: 10,
),
), */
);
} else {
document = await PDFDocument.fromAsset('assets/sample.pdf');
}
setState(() => _isLoading = false);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
drawer: Drawer(
child: Column(
children: <Widget>[
SizedBox(height: 36),
ListTile(
title: Text('Load from Assets'),
onTap: () {
changePDF(1);
},
),
ListTile(
title: Text('Load from URL'),
onTap: () {
changePDF(2);
},
),
ListTile(
title: Text('Restore default'),
onTap: () {
changePDF(3);
},
),
],
),
),
appBar: AppBar(
title: const Text('PDFViewer'),
),
body: Center(
child: _isLoading
? Center(child: CircularProgressIndicator())
: PDFViewer(
document: document,
lazyLoad: false,
zoomSteps: 1,
//uncomment below line to preload all pages
// lazyLoad: false,
// uncomment below line to scroll vertically
// scrollDirection: Axis.vertical,
//uncomment below code to replace bottom navigation with your own
/* navigationBuilder:
(context, page, totalPages, jumpToPage, animateToPage) {
return ButtonBar(
alignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
IconButton(
icon: Icon(Icons.first_page),
onPressed: () {
jumpToPage()(page: 0);
},
),
IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
animateToPage(page: page - 2);
},
),
IconButton(
icon: Icon(Icons.arrow_forward),
onPressed: () {
animateToPage(page: page);
},
),
IconButton(
icon: Icon(Icons.last_page),
onPressed: () {
jumpToPage(page: totalPages - 1);
},
),
],
);
}, */
),
),
),
);
}
}