Skip to content

Commit c5fe945

Browse files
committed
allow preloading all pages
1 parent c76d690 commit c5fe945

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

example/lib/main.dart

+3-1
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@ class _MyAppState extends State<MyApp> {
7474
? Center(child: CircularProgressIndicator())
7575
: PDFViewer(
7676
document: document,
77+
//uncomment below line to preload all pages
78+
// lazyLoad: false,
7779
// uncomment below line to scroll vertically
7880
// scrollDirection: Axis.vertical,
79-
81+
8082
//uncomment below code to replace bottom navigation with your own
8183
/* navigationBuilder:
8284
(context, page, totalPages, jumpToPage, animateToPage) {

lib/src/document.dart

+18
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class PDFDocument {
1212

1313
String _filePath;
1414
int count;
15+
List<PDFPage> _pages = [];
16+
bool _preloaded = false;
1517

1618
/// Load a PDF File from a given File
1719
///
@@ -81,6 +83,7 @@ class PDFDocument {
8183
Future<PDFPage> get(
8284
{int page = 1, final Function(double) onZoomChanged}) async {
8385
assert(page > 0);
86+
if (_preloaded && _pages.isNotEmpty) return _pages[page - 1];
8487
var data = await _channel
8588
.invokeMethod('getPage', {'filePath': _filePath, 'pageNumber': page});
8689
return new PDFPage(
@@ -90,6 +93,21 @@ class PDFDocument {
9093
);
9194
}
9295

96+
Future<void> preloadPages({final Function(double) onZoomChanged}) async {
97+
int countvar = 1;
98+
await Future.forEach<int>(List(count), (i) async {
99+
final data = await _channel.invokeMethod(
100+
'getPage', {'filePath': _filePath, 'pageNumber': countvar});
101+
_pages.add(PDFPage(
102+
data,
103+
countvar,
104+
onZoomChanged: onZoomChanged,
105+
));
106+
countvar++;
107+
});
108+
_preloaded = true;
109+
}
110+
93111
// Stream all pages
94112
Stream<PDFPage> getAll({final Function(double) onZoomChanged}) {
95113
return Future.forEach<PDFPage>(List(count), (i) async {

lib/src/viewer.dart

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class PDFViewer extends StatefulWidget {
1515
final PDFViewerTooltip tooltip;
1616
final bool enableSwipeNavigation;
1717
final Axis scrollDirection;
18+
final bool lazyLoad;
1819
final Widget Function(
1920
BuildContext,
2021
int pageNumber,
@@ -27,6 +28,7 @@ class PDFViewer extends StatefulWidget {
2728
{Key key,
2829
@required this.document,
2930
this.scrollDirection,
31+
this.lazyLoad = true,
3032
this.indicatorText = Colors.white,
3133
this.indicatorBackground = Colors.black54,
3234
this.showIndicator = true,
@@ -55,6 +57,8 @@ class _PDFViewerState extends State<PDFViewer> {
5557
super.initState();
5658
_pages = List(widget.document.count);
5759
_pageController = PageController();
60+
if (!widget.lazyLoad)
61+
widget.document.preloadPages(onZoomChanged: onZoomChanged);
5862
}
5963

6064
@override

0 commit comments

Comments
 (0)