Skip to content
This repository was archived by the owner on Nov 18, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 29
compileSdkVersion 31

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
Expand All @@ -43,7 +43,7 @@ android {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "jp.espresso3389.pdf_render_example"
minSdkVersion 21
targetSdkVersion 28
targetSdkVersion 31
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand Down
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.3.50'
ext.kotlin_version = '1.4.10'
repositories {
google()
jcenter()
Expand Down
1 change: 1 addition & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class _MyAppState extends State<MyApp> {
params: const PdfViewerParams(
padding: 10,
minScale: 1.0,
// scrollDirection: Axis.horizontal,
),
),
floatingActionButton: Column(
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ packages:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
version: "0.1.2"
meta:
dependency: transitive
description:
Expand Down
59 changes: 41 additions & 18 deletions lib/pdf_render_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,17 @@ class _PdfDocumentAwaiter {

final FutureOr<PdfDocument> _docFuture;
final OnError? onError;
late PdfDocument _cached;
bool _firstTime = true;
PdfDocument? _cached;

Future<PdfDocument> getValue() async {
if (_firstTime) {
_firstTime = false;
if (_cached == null) {
try {
_cached = await _docFuture;
} catch (e) {
onError?.call(e);
}
}
return _cached;
return _cached!;
}
}

Expand Down Expand Up @@ -193,7 +191,9 @@ class _PdfDocumentLoaderState extends State<PdfDocumentLoader> {
void _setPageSize(int pageNumber, Size? size) {
_lastPageSize = size;
if (pageNumber > 0 && pageNumber <= _doc!.pageCount) {
_cachedPageSizes ??= List<Size?>.filled(_doc!.pageCount, null);
if (_cachedPageSizes == null || _cachedPageSizes?.length != _doc!.pageCount) {
_cachedPageSizes = List<Size?>.filled(_doc!.pageCount, null);
}
_cachedPageSizes![pageNumber - 1] = size;
}
}
Expand Down Expand Up @@ -549,6 +549,9 @@ class PdfViewerParams {
/// Custom page decoration such as drop-shadow.
final BoxDecoration? pageDecoration;

/// Scrolling direction.
final Axis scrollDirection;

/// See [InteractiveViewer] for more info.
final bool alignPanAxis;

Expand Down Expand Up @@ -587,6 +590,7 @@ class PdfViewerParams {
this.buildPagePlaceholder,
this.buildPageOverlay,
this.pageDecoration,
this.scrollDirection = Axis.vertical,
this.alignPanAxis = false,
this.boundaryMargin = EdgeInsets.zero,
this.maxScale = 20,
Expand All @@ -606,6 +610,7 @@ class PdfViewerParams {
BuildPageContentFunc? buildPagePlaceholder,
BuildPageContentFunc? buildPageOverlay,
BoxDecoration? pageDecoration,
Axis? scrollDirection,
bool? alignPanAxis,
EdgeInsets? boundaryMargin,
bool? panEnabled,
Expand All @@ -624,6 +629,7 @@ class PdfViewerParams {
buildPagePlaceholder: buildPagePlaceholder ?? this.buildPagePlaceholder,
buildPageOverlay: buildPageOverlay ?? this.buildPageOverlay,
pageDecoration: pageDecoration ?? this.pageDecoration,
scrollDirection: scrollDirection ?? this.scrollDirection,
alignPanAxis: alignPanAxis ?? this.alignPanAxis,
boundaryMargin: boundaryMargin ?? this.boundaryMargin,
panEnabled: panEnabled ?? this.panEnabled,
Expand All @@ -647,6 +653,7 @@ class PdfViewerParams {
other.buildPagePlaceholder == buildPagePlaceholder &&
other.buildPageOverlay == buildPageOverlay &&
other.pageDecoration == pageDecoration &&
other.scrollDirection == scrollDirection &&
other.alignPanAxis == alignPanAxis &&
other.boundaryMargin == boundaryMargin &&
other.panEnabled == panEnabled &&
Expand All @@ -667,6 +674,7 @@ class PdfViewerParams {
buildPagePlaceholder.hashCode ^
buildPageOverlay.hashCode ^
pageDecoration.hashCode ^
scrollDirection.hashCode ^
alignPanAxis.hashCode ^
boundaryMargin.hashCode ^
panEnabled.hashCode ^
Expand Down Expand Up @@ -974,19 +982,34 @@ class _PdfViewerState extends State<PdfViewer> with SingleTickerProviderStateMix
_determinePagesToShow();
}

/// Default page layout logic that layouts pages vertically.
/// Default page layout logic that layouts pages vertically or horizontally.
void _relayoutDefault(Size viewSize) {
final maxWidth = _pages!.fold<double>(0.0, (maxWidth, page) => max(maxWidth, page.pageSize.width));
final ratio = (viewSize.width - _padding * 2) / maxWidth;
var top = _padding;
for (int i = 0; i < _pages!.length; i++) {
final page = _pages![i];
final w = page.pageSize.width * ratio;
final h = page.pageSize.height * ratio;
page.rect = Rect.fromLTWH(_padding, top, w, h);
top += h + _padding;
}
_docSize = Size(viewSize.width, top);
if (widget.params?.scrollDirection == Axis.horizontal) {
final maxHeight = _pages!.fold<double>(0.0, (maxHeight, page) => max(maxHeight, page.pageSize.height));
final ratio = (viewSize.height - _padding * 2) / maxHeight;
var left = _padding;
for (int i = 0; i < _pages!.length; i++) {
final page = _pages![i];
final w = page.pageSize.width * ratio;
final h = page.pageSize.height * ratio;
page.rect = Rect.fromLTWH(left, _padding, w, h);
left += w + _padding;
}
_docSize = Size(left, viewSize.height);
}
else {
final maxWidth = _pages!.fold<double>(0.0, (maxWidth, page) => max(maxWidth, page.pageSize.width));
final ratio = (viewSize.width - _padding * 2) / maxWidth;
var top = _padding;
for (int i = 0; i < _pages!.length; i++) {
final page = _pages![i];
final w = page.pageSize.width * ratio;
final h = page.pageSize.height * ratio;
page.rect = Rect.fromLTWH(_padding, top, w, h);
top += h + _padding;
}
_docSize = Size(viewSize.width, top);
}
}

Iterable<Widget> iterateLaidOutPages(Size viewSize) sync* {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ packages:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
version: "0.1.2"
meta:
dependency: transitive
description:
Expand Down