Aleksei
Committed by David PHAM-VAN

Implement `scrollToPage` method

@@ -110,6 +110,8 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom> @@ -110,6 +110,8 @@ 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;
  114 +
113 bool infoLoaded = false; 115 bool infoLoaded = false;
114 116
115 int? preview; 117 int? preview;
@@ -172,6 +174,28 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom> @@ -172,6 +174,28 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom>
172 super.didChangeDependencies(); 174 super.didChangeDependencies();
173 } 175 }
174 176
  177 + @override
  178 + void initState() {
  179 + super.initState();
  180 + _pageGlobalKeys = List.generate(
  181 + pages.length,
  182 + (index) => GlobalKey(debugLabel: 'pdf-page-$index'),
  183 + );
  184 + }
  185 +
  186 + void scrollToPage(
  187 + int index, {
  188 + Duration duration = Duration.zero,
  189 + Curve curve = Curves.ease,
  190 + ScrollPositionAlignmentPolicy alignmentPolicy =
  191 + ScrollPositionAlignmentPolicy.explicit,
  192 + }) {
  193 + assert(index >= 0);
  194 + final pageKey = _pageGlobalKeys[index];
  195 + Scrollable.ensureVisible(pageKey.currentContext!,
  196 + duration: duration, curve: curve, alignmentPolicy: alignmentPolicy);
  197 + }
  198 +
175 Widget _showError(Object error) { 199 Widget _showError(Object error) {
176 if (widget.onError != null) { 200 if (widget.onError != null) {
177 return widget.onError!(context, error); 201 return widget.onError!(context, error);
@@ -215,6 +239,7 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom> @@ -215,6 +239,7 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom>
215 }); 239 });
216 }, 240 },
217 child: PdfPreviewPage( 241 child: PdfPreviewPage(
  242 + key: _pageGlobalKeys[index],
218 pageData: pages[index], 243 pageData: pages[index],
219 pdfPreviewPageDecoration: widget.pdfPreviewPageDecoration, 244 pdfPreviewPageDecoration: widget.pdfPreviewPageDecoration,
220 pageMargin: widget.previewPageMargin, 245 pageMargin: widget.previewPageMargin,