Committed by
David PHAM-VAN
Make `pageGlobalKeys` populated once
Showing
1 changed file
with
8 additions
and
2 deletions
@@ -110,7 +110,7 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom> | @@ -110,7 +110,7 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom> | ||
110 | with PdfPreviewRaster { | 110 | with PdfPreviewRaster { |
111 | final listView = GlobalKey(); | 111 | final listView = GlobalKey(); |
112 | 112 | ||
113 | - late List<GlobalKey> _pageGlobalKeys; | 113 | + List<GlobalKey> _pageGlobalKeys = <GlobalKey>[]; |
114 | 114 | ||
115 | bool infoLoaded = false; | 115 | bool infoLoaded = false; |
116 | 116 | ||
@@ -174,6 +174,7 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom> | @@ -174,6 +174,7 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom> | ||
174 | super.didChangeDependencies(); | 174 | super.didChangeDependencies(); |
175 | } | 175 | } |
176 | 176 | ||
177 | + /// Ensures that page with [index] is become visible. | ||
177 | Future<void> scrollToPage( | 178 | Future<void> scrollToPage( |
178 | int index, { | 179 | int index, { |
179 | Duration duration = const Duration(milliseconds: 300), | 180 | Duration duration = const Duration(milliseconds: 300), |
@@ -188,6 +189,9 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom> | @@ -188,6 +189,9 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom> | ||
188 | duration: duration, curve: curve, alignmentPolicy: alignmentPolicy); | 189 | duration: duration, curve: curve, alignmentPolicy: alignmentPolicy); |
189 | } | 190 | } |
190 | 191 | ||
192 | + /// Returns the global key for page with [index]. | ||
193 | + Key getPageKey(int index) => _pageGlobalKeys[index]; | ||
194 | + | ||
191 | Widget _showError(Object error) { | 195 | Widget _showError(Object error) { |
192 | if (widget.onError != null) { | 196 | if (widget.onError != null) { |
193 | return widget.onError!(context, error); | 197 | return widget.onError!(context, error); |
@@ -213,10 +217,12 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom> | @@ -213,10 +217,12 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom> | ||
213 | ); | 217 | ); |
214 | } | 218 | } |
215 | 219 | ||
220 | + if (_pageGlobalKeys.isEmpty) { | ||
216 | _pageGlobalKeys = List.generate( | 221 | _pageGlobalKeys = List.generate( |
217 | pages.length, | 222 | pages.length, |
218 | (index) => GlobalKey(debugLabel: 'pdf-page-$index'), | 223 | (index) => GlobalKey(debugLabel: 'pdf-page-$index'), |
219 | ); | 224 | ); |
225 | + } | ||
220 | 226 | ||
221 | if (widget.pagesBuilder != null) { | 227 | if (widget.pagesBuilder != null) { |
222 | return widget.pagesBuilder!(context, pages); | 228 | return widget.pagesBuilder!(context, pages); |
@@ -237,7 +243,7 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom> | @@ -237,7 +243,7 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom> | ||
237 | }); | 243 | }); |
238 | }, | 244 | }, |
239 | child: PdfPreviewPage( | 245 | child: PdfPreviewPage( |
240 | - key: _pageGlobalKeys[index], | 246 | + key: getPageKey(index), |
241 | pageData: pages[index], | 247 | pageData: pages[index], |
242 | pdfPreviewPageDecoration: widget.pdfPreviewPageDecoration, | 248 | pdfPreviewPageDecoration: widget.pdfPreviewPageDecoration, |
243 | pageMargin: widget.previewPageMargin, | 249 | pageMargin: widget.previewPageMargin, |
-
Please register or login to post a comment