Skip to content

Commit

Permalink
First import
Browse files Browse the repository at this point in the history
  • Loading branch information
rwbr committed May 5, 2021
0 parents commit d64a6da
Show file tree
Hide file tree
Showing 14 changed files with 1,469 additions and 0 deletions.
74 changes: 74 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
build/

# Android related
**/android/**/gradle-wrapper.jar
**/android/.gradle
**/android/captures/
**/android/gradlew
**/android/gradlew.bat
**/android/local.properties
**/android/**/GeneratedPluginRegistrant.java

# iOS/XCode related
**/ios/**/*.mode1v3
**/ios/**/*.mode2v3
**/ios/**/*.moved-aside
**/ios/**/*.pbxuser
**/ios/**/*.perspectivev3
**/ios/**/*sync/
**/ios/**/.sconsign.dblite
**/ios/**/.tags*
**/ios/**/.vagrant/
**/ios/**/DerivedData/
**/ios/**/Icon?
**/ios/**/Pods/
**/ios/**/.symlinks/
**/ios/**/profile
**/ios/**/xcuserdata
**/ios/.generated/
**/ios/Flutter/App.framework
**/ios/Flutter/Flutter.framework
**/ios/Flutter/Flutter.podspec
**/ios/Flutter/Generated.xcconfig
**/ios/Flutter/app.flx
**/ios/Flutter/app.zip
**/ios/Flutter/flutter_assets/
**/ios/Flutter/flutter_export_environment.sh
**/ios/ServiceDefinitions.json
**/ios/Runner/GeneratedPluginRegistrant.*

# Exceptions to above rules.
!**/ios/**/default.mode1v3
!**/ios/**/default.mode2v3
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
10 changes: 10 additions & 0 deletions .metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: adc687823a831bbebe28bdccfac1a628ca621513
channel: stable

project_type: package
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## [0.0.1] - TODO: Add release date.

* TODO: Describe initial release.
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO: Add your license here.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# flutter_neat_pdf_viewer

A new Flutter package project.

## Getting Started

This project is a starting point for a Dart
[package](https://flutter.dev/developing-packages/),
a library module containing code that can be shared easily across
multiple Flutter or Dart projects.

For help getting started with Flutter, view our
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
# flutter_neat_pdf_viewer
7 changes: 7 additions & 0 deletions lib/flutter_neat_pdf_viewer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
library flutter_neat_pdf_viewer;

export 'src/document.dart' show PDFDocument;
export 'src/page.dart' show PDFPage;
export 'src/viewer.dart' show PDFViewer, IndicatorPosition;
export 'src/tooltip.dart' show PDFViewerTooltip;
export 'package:flutter_cache_manager/flutter_cache_manager.dart';
147 changes: 147 additions & 0 deletions lib/src/document.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import 'dart:async';
import 'dart:io';

import 'package:flutter/services.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:flutter_neat_pdf_viewer/src/page.dart';
import 'package:path_provider/path_provider.dart';

class PDFDocument {
static const MethodChannel _channel =
const MethodChannel('flutter_plugin_pdf_viewer');

String _filePath;
int count;
List<PDFPage> _pages = [];
bool _preloaded = false;

/// Load a PDF File from a given File
/// [File file], file to be loaded
///
static Future<PDFDocument> fromFile(File file) async {
PDFDocument document = PDFDocument();
document._filePath = file.path;
try {
var pageCount = await _channel
.invokeMethod('getNumberOfPages', {'filePath': file.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.
/// File is saved in cache
/// [String url] url of the pdf file
/// [Map<String,String headers] headers to pass for the [url]
/// [CacheManager cacheManager] to provide configuration for
/// cache management
static Future<PDFDocument> fromURL(String url,
{Map<String, String> headers, CacheManager cacheManager}) async {
// Download into cache
File f = await (cacheManager ?? DefaultCacheManager())
.getSingleFile(url, headers: headers);
PDFDocument document = PDFDocument();
document._filePath = f.path;
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
/// [String asset] path of the asset to be loaded
///
static Future<PDFDocument> fromAsset(String asset) async {
File file;
try {
var dir = await getApplicationDocumentsDirectory();
file = File("${dir.path}/file.pdf");
var data = await rootBundle.load(asset);
var bytes = data.buffer.asUint8List();
await file.writeAsBytes(bytes, flush: true);
} catch (e) {
throw Exception('Error parsing asset file!');
}
PDFDocument document = PDFDocument();
document._filePath = file.path;
try {
var pageCount = await _channel
.invokeMethod('getNumberOfPages', {'filePath': file.path});
document.count = document.count = int.parse(pageCount);
} catch (e) {
throw Exception('Error reading PDF!');
}
return document;
}

/// Load specific page
///
/// [page] defaults to `1` and must be equal or above it
Future<PDFPage> get({
int page = 1,
final Function(double) onZoomChanged,
final int zoomSteps,
final double minScale,
final double maxScale,
final double panLimit,
}) async {
assert(page > 0);
if (_preloaded && _pages.isNotEmpty) return _pages[page - 1];
var data = await _channel
.invokeMethod('getPage', {'filePath': _filePath, 'pageNumber': page});
return new PDFPage(
data,
page,
onZoomChanged: onZoomChanged,
zoomSteps: zoomSteps,
minScale: minScale,
maxScale: maxScale,
panLimit: panLimit,
);
}

Future<void> preloadPages({
final Function(double) onZoomChanged,
final int zoomSteps,
final double minScale,
final double maxScale,
final double panLimit,
}) async {
int countvar = 1;
await Future.forEach<int>(List(count), (i) async {
final data = await _channel.invokeMethod(
'getPage', {'filePath': _filePath, 'pageNumber': countvar});
_pages.add(PDFPage(
data,
countvar,
onZoomChanged: onZoomChanged,
zoomSteps: zoomSteps,
minScale: minScale,
maxScale: maxScale,
panLimit: panLimit,
));
countvar++;
});
_preloaded = true;
}

// Stream all pages
Stream<PDFPage> getAll({final Function(double) onZoomChanged}) {
return Future.forEach<PDFPage>(List(count), (i) async {
print(i);
final data = await _channel
.invokeMethod('getPage', {'filePath': _filePath, 'pageNumber': i});
return new PDFPage(
data,
1,
onZoomChanged: onZoomChanged,
);
}).asStream();
}
}
75 changes: 75 additions & 0 deletions lib/src/page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import 'dart:io';
import 'dart:ui';
import 'package:flutter_neat_pdf_viewer/src/zoomable_widget.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/painting.dart';

/// A class to represent PDF page
/// [imgPath], path of the image (pdf page)
/// [num], page number
/// [onZoomChanged], function called when zoom is changed
/// [zoomSteps], number of zoom steps on double tap
/// [minScale] minimum zoom scale
/// [maxScale] maximum zoom scale
/// [panLimit] limit for pan
class PDFPage extends StatefulWidget {
final String imgPath;
final int num;
final Function(double) onZoomChanged;
final int zoomSteps;
final double minScale;
final double maxScale;
final double panLimit;
PDFPage(
this.imgPath,
this.num, {
this.onZoomChanged,
this.zoomSteps = 3,
this.minScale = 1.0,
this.maxScale = 5.0,
this.panLimit = 1.0,
});

@override
_PDFPageState createState() => _PDFPageState();
}

class _PDFPageState extends State<PDFPage> {
ImageProvider provider;

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

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

_repaint() {
provider = FileImage(File(widget.imgPath));
final resolver = provider.resolve(createLocalImageConfiguration(context));
resolver.addListener(ImageStreamListener((imgInfo, alreadyPainted) {
if (!alreadyPainted) setState(() {});
}));
}

@override
Widget build(BuildContext context) {
return Container(
decoration: null,
child: ZoomableWidget(
onZoomChanged: widget.onZoomChanged,
zoomSteps: widget.zoomSteps ?? 3,
minScale: widget.minScale ?? 1.0,
panLimit: widget.panLimit ?? 1.0,
maxScale: widget.maxScale ?? 5.0,
child: Image(image: provider),
));
}
}
16 changes: 16 additions & 0 deletions lib/src/tooltip.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class PDFViewerTooltip {
final String first;
final String previous;
final String next;
final String last;
final String pick;
final String jump;

const PDFViewerTooltip(
{this.first = "First",
this.previous = "Previous",
this.next = "Next",
this.last = "Last",
this.pick = "Pick a page",
this.jump = "Jump"});
}
Loading

0 comments on commit d64a6da

Please sign in to comment.