From eaff8643d5abaddf1b9200208bf4b6f9cbe1acf1 Mon Sep 17 00:00:00 2001 From: deakjahn Date: Mon, 17 Jan 2022 10:45:07 +0100 Subject: [PATCH 1/3] Cache error, late initialization error --- example/pubspec.lock | 2 +- lib/pdf_render_widgets.dart | 12 ++++++------ pubspec.lock | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/example/pubspec.lock b/example/pubspec.lock index 146afeb..cdf9102 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -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: diff --git a/lib/pdf_render_widgets.dart b/lib/pdf_render_widgets.dart index 9513e88..1fc40a8 100644 --- a/lib/pdf_render_widgets.dart +++ b/lib/pdf_render_widgets.dart @@ -60,19 +60,17 @@ class _PdfDocumentAwaiter { final FutureOr _docFuture; final OnError? onError; - late PdfDocument _cached; - bool _firstTime = true; + PdfDocument? _cached; Future getValue() async { - if (_firstTime) { - _firstTime = false; + if (_cached == null) { try { _cached = await _docFuture; } catch (e) { onError?.call(e); } } - return _cached; + return _cached!; } } @@ -193,7 +191,9 @@ class _PdfDocumentLoaderState extends State { void _setPageSize(int pageNumber, Size? size) { _lastPageSize = size; if (pageNumber > 0 && pageNumber <= _doc!.pageCount) { - _cachedPageSizes ??= List.filled(_doc!.pageCount, null); + if (_cachedPageSizes == null || _cachedPageSizes?.length != _doc!.pageCount) { + _cachedPageSizes = List.filled(_doc!.pageCount, null); + } _cachedPageSizes![pageNumber - 1] = size; } } diff --git a/pubspec.lock b/pubspec.lock index fbf126c..aed78f4 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -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: From 3b299ee4078b54ee106d22bfa80fd86318d73901 Mon Sep 17 00:00:00 2001 From: deakjahn Date: Mon, 17 Jan 2022 11:39:35 +0100 Subject: [PATCH 2/3] Support horizontal scrolling --- example/android/app/build.gradle | 4 +-- example/android/build.gradle | 2 +- example/lib/main.dart | 1 + lib/pdf_render_widgets.dart | 47 ++++++++++++++++++++++++-------- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index dc96da7..48db0d6 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -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' @@ -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 } diff --git a/example/android/build.gradle b/example/android/build.gradle index c505a86..dc8b8ec 100644 --- a/example/android/build.gradle +++ b/example/android/build.gradle @@ -1,5 +1,5 @@ buildscript { - ext.kotlin_version = '1.3.50' + ext.kotlin_version = '1.4.10' repositories { google() jcenter() diff --git a/example/lib/main.dart b/example/lib/main.dart index 47adbb6..7789227 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -56,6 +56,7 @@ class _MyAppState extends State { params: const PdfViewerParams( padding: 10, minScale: 1.0, + scrollDirection: Axis.horizontal, ), ), floatingActionButton: Column( diff --git a/lib/pdf_render_widgets.dart b/lib/pdf_render_widgets.dart index 1fc40a8..caaae82 100644 --- a/lib/pdf_render_widgets.dart +++ b/lib/pdf_render_widgets.dart @@ -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; @@ -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, @@ -606,6 +610,7 @@ class PdfViewerParams { BuildPageContentFunc? buildPagePlaceholder, BuildPageContentFunc? buildPageOverlay, BoxDecoration? pageDecoration, + Axis? scrollDirection, bool? alignPanAxis, EdgeInsets? boundaryMargin, bool? panEnabled, @@ -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, @@ -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 && @@ -667,6 +674,7 @@ class PdfViewerParams { buildPagePlaceholder.hashCode ^ buildPageOverlay.hashCode ^ pageDecoration.hashCode ^ + scrollDirection.hashCode ^ alignPanAxis.hashCode ^ boundaryMargin.hashCode ^ panEnabled.hashCode ^ @@ -974,19 +982,34 @@ class _PdfViewerState extends State 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(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(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(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 iterateLaidOutPages(Size viewSize) sync* { From 4c9c61d38e126ff96ecf2efe05d53d913a8afd85 Mon Sep 17 00:00:00 2001 From: deakjahn Date: Mon, 17 Jan 2022 11:40:28 +0100 Subject: [PATCH 3/3] Support horizontal scrolling --- example/lib/main.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 7789227..05380f1 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -56,7 +56,7 @@ class _MyAppState extends State { params: const PdfViewerParams( padding: 10, minScale: 1.0, - scrollDirection: Axis.horizontal, + // scrollDirection: Axis.horizontal, ), ), floatingActionButton: Column(