Skip to content

Commit

Permalink
Added screenshots
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiago Ribeiro committed Mar 15, 2019
1 parent d3103f0 commit 2084a08
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 50 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"configurations": [
{
"name": "Flutter",
"type": "dart",
"request": "launch",
"type": "dart"
"program": "./example/lib/main.dart"
}
]
}
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# flutter_pdf_viewer
# flutter_plugin_pdf_viewer

A cross platform plugin for handling PDF files.
A flutter plugin for handling PDF files. Works on both Android & iOS

## Installation

Add *flutter_pdf_viewer* as a dependency in [your pubspec.yaml file](https://flutter.io/platform-plugins/).
Add *flutter_plugin_pdf_viewer* as a dependency in [your pubspec.yaml file](https://flutter.io/platform-plugins/).
```
flutter_pdf_viewer: any
flutter_plugin_pdf_viewer: any
```

## Android
Expand All @@ -15,7 +15,7 @@ No permissions required. Uses application cache directory.
## iOS
No permissions required.

### How-to
## How-to

#### Load PDF
```
Expand All @@ -33,10 +33,7 @@ PDFDocument doc = await PDFDocument.fromFile(file);
#### Load pages
```
// Load specific page
PDFPage pageOne = await doc.get(1);
// Load all pages
List<PDFPage> pages = await doc.getAll();
PDFPage pageOne = await doc.get(page: _number);
```

#### Pre-built viewer
Expand All @@ -46,7 +43,7 @@ Use the pre-built PDF Viewer
Widget build(BuildContext context) {
Scaffold(
appBar: AppBar(
title: const Text('FlutterPDFViewer'),
title: Text('Example'),
),
body: Center(
child: _isLoading
Expand Down
File renamed without changes.
41 changes: 26 additions & 15 deletions lib/src/document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,50 @@ class PDFDocument {
String _filePath;
int count;

/// Load a PDF File from a given File
///
///
static Future<PDFDocument> fromFile(File f) async {
PDFDocument document = PDFDocument();
document._filePath = f.path;
var pageCount =
await _channel.invokeMethod('getNumberOfPages', {'filePath': f.path});
document.count = int.parse(pageCount);
try {
var pageCount =
await _channel.invokeMethod('getNumberOfPages', {'filePath': f.path});
document.count = document.count = int.parse(pageCount);
} catch (e) {
throw Exception('Error reading PDF!');
}
return document;
}

/// Load a PDF File from a given URL
///
/// Load a PDF File from a given URL.
/// File is saved in cache
///
static Future<PDFDocument> fromURL(String url) async {
// Download into cahce
// Download into cache
File f = await DefaultCacheManager().getSingleFile(url);
PDFDocument document = PDFDocument();
document._filePath = f.path;
var pageCount =
await _channel.invokeMethod('getNumberOfPages', {'filePath': f.path});
document.count = int.parse(pageCount);
try {
var pageCount =
await _channel.invokeMethod('getNumberOfPages', {'filePath': f.path});
document.count = document.count = int.parse(pageCount);
} catch (e) {
throw Exception('Error reading PDF!');
}
return document;
}

/// Load a PDF File from assets folder
///
///
static Future<PDFDocument> fromAsset(String asset) async {
PDFDocument document = PDFDocument();
ByteData data = await rootBundle.load(asset);
File f = File.fromRawPath(data.buffer.asUint8List());
document._filePath = f.path;
try {
var pageCount =
await _channel.invokeMethod('getNumberOfPages', {'filePath': f.path});
await _channel.invokeMethod('getNumberOfPages', {'filePath': asset});
document.count = document.count = int.parse(pageCount);
} catch(e) {
} catch (e) {
throw Exception('Error reading PDF!');
}
return document;
Expand All @@ -62,6 +73,6 @@ class PDFDocument {
/// Load all pages
///
Future<List<dynamic>> getAll() async {
return [];
throw Exception("Not yet implemented");
}
}
39 changes: 15 additions & 24 deletions lib/src/viewer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,27 @@ class PDFViewer extends StatefulWidget {
class _PDFViewerState extends State<PDFViewer> {
bool _isLoading = true;
int _pageNumber = 1;
int _oldPage = 1;
int _oldPage = 0;
PDFPage _page;

_PDFViewerState() {
@override
void didChangeDependencies() {
super.didChangeDependencies();
_loadPage();
}

@override
void didUpdateWidget (PDFViewer oldWidget) {
super.didUpdateWidget(oldWidget);
_loadPage();
}

_loadPage() async {
if (_oldPage != _pageNumber) {
if (_oldPage == 0) {
_page = await widget.document.get(page: _pageNumber);
setState(() => _isLoading = false);
}
else if (_oldPage != _pageNumber) {
_oldPage = _pageNumber;
setState(() => _isLoading = true);
_page = await widget.document.get(page: _pageNumber);
Expand Down Expand Up @@ -90,27 +102,6 @@ class _PDFViewerState extends State<PDFViewer> {
});
}

@override
void didChangeDependencies() {
super.didChangeDependencies();
_repaint();
}

@override
void didUpdateWidget(PDFViewer oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.document != widget.document) {
_repaint();
}
}

_repaint() {
_isLoading = true;
_oldPage = 0;
_pageNumber = 1;
setState(() {});
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down

0 comments on commit 2084a08

Please sign in to comment.