From 2bc581d6d511524968aaa0d3c37a680522c67c39 Mon Sep 17 00:00:00 2001 From: Aleksei <130981115+MacDeveloper1@users.noreply.github.com> Date: Thu, 21 Dec 2023 17:34:47 +0100 Subject: [PATCH 01/14] Implement and apply `PdfActionBarTheme` --- .../lib/src/preview/action_bar_theme.dart | 74 +++++++++++++++++++ printing/lib/src/preview/pdf_preview.dart | 17 ++++- 2 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 printing/lib/src/preview/action_bar_theme.dart diff --git a/printing/lib/src/preview/action_bar_theme.dart b/printing/lib/src/preview/action_bar_theme.dart new file mode 100644 index 00000000..cbabe80c --- /dev/null +++ b/printing/lib/src/preview/action_bar_theme.dart @@ -0,0 +1,74 @@ +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; + +class PdfActionBarTheme with Diagnosticable { + /// Creates a theme for action bar of [PdfPreviewController]. + const PdfActionBarTheme({ + this.backgroundColor, + this.iconColor, + this.height, + this.textStyle, + this.elevation = 4, + this.alignment = WrapAlignment.spaceAround, + }); + + final Color? backgroundColor; + final Color? iconColor; + final double? height; + final TextStyle? textStyle; + final double elevation; + final WrapAlignment alignment; + + /// Creates a copy of this object but with the given fields replaced with the + /// new values. + PdfActionBarTheme copyWith({ + Color? backgroundColor, + Color? iconColor, + double? height, + TextStyle? textStyle, + double? elevation, + WrapAlignment? alignment, + }) { + return PdfActionBarTheme( + backgroundColor: backgroundColor ?? this.backgroundColor, + iconColor: iconColor ?? this.iconColor, + height: height ?? this.height, + textStyle: textStyle ?? this.textStyle, + elevation: elevation ?? this.elevation, + alignment: alignment ?? this.alignment, + ); + } + + @override + int get hashCode => Object.hashAll( + [backgroundColor, iconColor, height, textStyle, elevation, alignment]); + + @override + bool operator ==(Object other) { + if (identical(this, other)) { + return true; + } + if (other.runtimeType != runtimeType) { + return false; + } + return other is PdfActionBarTheme && + other.backgroundColor == backgroundColor && + other.iconColor == iconColor && + other.height == height && + other.textStyle == textStyle && + other.elevation == elevation && + other.alignment == alignment; + } + + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties.add(ColorProperty('backgroundColor', backgroundColor)); + properties.add(ColorProperty('iconColor', iconColor)); + properties.add(DoubleProperty('height', height)); + properties.add(DiagnosticsProperty('textStyle', textStyle)); + properties.add(DoubleProperty('elevation', elevation)); + properties.add(DiagnosticsProperty('alignment', alignment, + defaultValue: WrapAlignment.spaceAround)); + } +} diff --git a/printing/lib/src/preview/pdf_preview.dart b/printing/lib/src/preview/pdf_preview.dart index 87ee3de5..30250f87 100644 --- a/printing/lib/src/preview/pdf_preview.dart +++ b/printing/lib/src/preview/pdf_preview.dart @@ -21,6 +21,7 @@ import 'package:pdf/widgets.dart' as pw; import '../callback.dart'; import '../printing.dart'; import '../printing_info.dart'; +import 'action_bar_theme.dart'; import 'actions.dart'; import 'controller.dart'; import 'custom.dart'; @@ -62,6 +63,7 @@ class PdfPreview extends StatefulWidget { this.loadingWidget, this.onPageFormatChanged, this.dpi, + this.actionBarTheme = const PdfActionBarTheme(), }) : _pagesBuilder = null, super(key: key); @@ -119,6 +121,7 @@ class PdfPreview extends StatefulWidget { this.loadingWidget, this.onPageFormatChanged, this.dpi, + this.actionBarTheme = const PdfActionBarTheme(), required CustomPdfPagesBuilder pagesBuilder, }) : _pagesBuilder = pagesBuilder, super(key: key); @@ -223,6 +226,9 @@ class PdfPreview extends StatefulWidget { /// If not provided, this value is calculated. final double? dpi; + /// The style of actions bar. + final PdfActionBarTheme actionBarTheme; + /// clients can pass this builder to render /// their own pages. final CustomPdfPagesBuilder? _pagesBuilder; @@ -406,16 +412,19 @@ class PdfPreviewState extends State { if (actions.isNotEmpty) IconTheme.merge( data: IconThemeData( - color: iconColor, + color: widget.actionBarTheme.iconColor ?? iconColor, ), child: Material( - elevation: 4, - color: theme.primaryColor, + elevation: widget.actionBarTheme.elevation, + color: + widget.actionBarTheme.backgroundColor ?? theme.primaryColor, + textStyle: widget.actionBarTheme.textStyle, child: SizedBox( width: double.infinity, + height: widget.actionBarTheme.height, child: SafeArea( child: Wrap( - alignment: WrapAlignment.spaceAround, + alignment: widget.actionBarTheme.alignment, children: actions, ), ), From 4348f87c76fe417059f3279023cb9d4b610cfe37 Mon Sep 17 00:00:00 2001 From: Aleksei <130981115+MacDeveloper1@users.noreply.github.com> Date: Fri, 22 Dec 2023 08:50:50 +0100 Subject: [PATCH 02/14] Export `PdfActionBarTheme` --- printing/lib/printing.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/printing/lib/printing.dart b/printing/lib/printing.dart index 1dc67269..06e1ba7d 100644 --- a/printing/lib/printing.dart +++ b/printing/lib/printing.dart @@ -24,6 +24,7 @@ export 'src/asset_utils.dart'; export 'src/cache.dart'; export 'src/callback.dart'; export 'src/fonts/gfonts.dart'; +export 'src/preview/action_bar_theme.dart'; export 'src/preview/actions.dart'; export 'src/preview/pdf_preview.dart'; export 'src/printer.dart'; From 692e61dbb04677fe771d4a0e00e469f310ec3b50 Mon Sep 17 00:00:00 2001 From: Aleksei <130981115+MacDeveloper1@users.noreply.github.com> Date: Fri, 22 Dec 2023 11:31:08 +0100 Subject: [PATCH 03/14] Implement `crossAxisAlignment` for actions --- .../lib/src/preview/action_bar_theme.dart | 21 ++++++++++++++++--- printing/lib/src/preview/pdf_preview.dart | 2 ++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/printing/lib/src/preview/action_bar_theme.dart b/printing/lib/src/preview/action_bar_theme.dart index cbabe80c..f2b84fe4 100644 --- a/printing/lib/src/preview/action_bar_theme.dart +++ b/printing/lib/src/preview/action_bar_theme.dart @@ -10,6 +10,7 @@ class PdfActionBarTheme with Diagnosticable { this.textStyle, this.elevation = 4, this.alignment = WrapAlignment.spaceAround, + this.crossAxisAlignment = WrapCrossAlignment.center, }); final Color? backgroundColor; @@ -18,6 +19,7 @@ class PdfActionBarTheme with Diagnosticable { final TextStyle? textStyle; final double elevation; final WrapAlignment alignment; + final WrapCrossAlignment crossAxisAlignment; /// Creates a copy of this object but with the given fields replaced with the /// new values. @@ -28,6 +30,7 @@ class PdfActionBarTheme with Diagnosticable { TextStyle? textStyle, double? elevation, WrapAlignment? alignment, + WrapCrossAlignment? crossAxisAlignment, }) { return PdfActionBarTheme( backgroundColor: backgroundColor ?? this.backgroundColor, @@ -36,12 +39,20 @@ class PdfActionBarTheme with Diagnosticable { textStyle: textStyle ?? this.textStyle, elevation: elevation ?? this.elevation, alignment: alignment ?? this.alignment, + crossAxisAlignment: crossAxisAlignment ?? this.crossAxisAlignment, ); } @override - int get hashCode => Object.hashAll( - [backgroundColor, iconColor, height, textStyle, elevation, alignment]); + int get hashCode => Object.hashAll([ + backgroundColor, + iconColor, + height, + textStyle, + elevation, + alignment, + crossAxisAlignment + ]); @override bool operator ==(Object other) { @@ -57,7 +68,8 @@ class PdfActionBarTheme with Diagnosticable { other.height == height && other.textStyle == textStyle && other.elevation == elevation && - other.alignment == alignment; + other.alignment == alignment && + other.crossAxisAlignment == crossAxisAlignment; } @override @@ -70,5 +82,8 @@ class PdfActionBarTheme with Diagnosticable { properties.add(DoubleProperty('elevation', elevation)); properties.add(DiagnosticsProperty('alignment', alignment, defaultValue: WrapAlignment.spaceAround)); + properties.add(DiagnosticsProperty( + 'crossAxisAlignment', crossAxisAlignment, + defaultValue: WrapCrossAlignment.center)); } } diff --git a/printing/lib/src/preview/pdf_preview.dart b/printing/lib/src/preview/pdf_preview.dart index 30250f87..cdd8436a 100644 --- a/printing/lib/src/preview/pdf_preview.dart +++ b/printing/lib/src/preview/pdf_preview.dart @@ -425,6 +425,8 @@ class PdfPreviewState extends State { child: SafeArea( child: Wrap( alignment: widget.actionBarTheme.alignment, + crossAxisAlignment: + widget.actionBarTheme.crossAxisAlignment, children: actions, ), ), From 60342b500191d4ea4b49fbb4d1e270717bd8f4ba Mon Sep 17 00:00:00 2001 From: Aleksei <130981115+MacDeveloper1@users.noreply.github.com> Date: Fri, 22 Dec 2023 12:03:16 +0100 Subject: [PATCH 04/14] Apply additional `Wrap` properties --- .../lib/src/preview/action_bar_theme.dart | 20 +++++++++++++++++-- printing/lib/src/preview/pdf_preview.dart | 2 ++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/printing/lib/src/preview/action_bar_theme.dart b/printing/lib/src/preview/action_bar_theme.dart index f2b84fe4..f245cd9d 100644 --- a/printing/lib/src/preview/action_bar_theme.dart +++ b/printing/lib/src/preview/action_bar_theme.dart @@ -8,8 +8,10 @@ class PdfActionBarTheme with Diagnosticable { this.iconColor, this.height, this.textStyle, - this.elevation = 4, + this.elevation = 4.0, + this.actionSpacing = 0.0, this.alignment = WrapAlignment.spaceAround, + this.runAlignment = WrapAlignment.center, this.crossAxisAlignment = WrapCrossAlignment.center, }); @@ -18,7 +20,9 @@ class PdfActionBarTheme with Diagnosticable { final double? height; final TextStyle? textStyle; final double elevation; + final double actionSpacing; final WrapAlignment alignment; + final WrapAlignment runAlignment; final WrapCrossAlignment crossAxisAlignment; /// Creates a copy of this object but with the given fields replaced with the @@ -29,7 +33,9 @@ class PdfActionBarTheme with Diagnosticable { double? height, TextStyle? textStyle, double? elevation, + double? actionSpacing, WrapAlignment? alignment, + WrapAlignment? runAlignment, WrapCrossAlignment? crossAxisAlignment, }) { return PdfActionBarTheme( @@ -38,7 +44,9 @@ class PdfActionBarTheme with Diagnosticable { height: height ?? this.height, textStyle: textStyle ?? this.textStyle, elevation: elevation ?? this.elevation, + actionSpacing: actionSpacing ?? this.actionSpacing, alignment: alignment ?? this.alignment, + runAlignment: runAlignment ?? this.runAlignment, crossAxisAlignment: crossAxisAlignment ?? this.crossAxisAlignment, ); } @@ -50,8 +58,10 @@ class PdfActionBarTheme with Diagnosticable { height, textStyle, elevation, + actionSpacing, alignment, - crossAxisAlignment + runAlignment, + crossAxisAlignment, ]); @override @@ -68,7 +78,9 @@ class PdfActionBarTheme with Diagnosticable { other.height == height && other.textStyle == textStyle && other.elevation == elevation && + other.actionSpacing == actionSpacing && other.alignment == alignment && + other.runAlignment == runAlignment && other.crossAxisAlignment == crossAxisAlignment; } @@ -80,8 +92,12 @@ class PdfActionBarTheme with Diagnosticable { properties.add(DoubleProperty('height', height)); properties.add(DiagnosticsProperty('textStyle', textStyle)); properties.add(DoubleProperty('elevation', elevation)); + properties.add(DoubleProperty('actionSpacing', actionSpacing)); properties.add(DiagnosticsProperty('alignment', alignment, defaultValue: WrapAlignment.spaceAround)); + properties.add(DiagnosticsProperty( + 'runAlignment', runAlignment, + defaultValue: WrapAlignment.center)); properties.add(DiagnosticsProperty( 'crossAxisAlignment', crossAxisAlignment, defaultValue: WrapCrossAlignment.center)); diff --git a/printing/lib/src/preview/pdf_preview.dart b/printing/lib/src/preview/pdf_preview.dart index cdd8436a..6fc3b346 100644 --- a/printing/lib/src/preview/pdf_preview.dart +++ b/printing/lib/src/preview/pdf_preview.dart @@ -424,7 +424,9 @@ class PdfPreviewState extends State { height: widget.actionBarTheme.height, child: SafeArea( child: Wrap( + spacing: widget.actionBarTheme.actionSpacing, alignment: widget.actionBarTheme.alignment, + runAlignment: widget.actionBarTheme.runAlignment, crossAxisAlignment: widget.actionBarTheme.crossAxisAlignment, children: actions, From 1b9e77e5dab0393e16f9ed9faa5e46a76d1fea52 Mon Sep 17 00:00:00 2001 From: Aleksei <130981115+MacDeveloper1@users.noreply.github.com> Date: Fri, 19 Jan 2024 17:16:34 +0100 Subject: [PATCH 05/14] Implement `scrollToPage` method --- printing/lib/src/preview/custom.dart | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/printing/lib/src/preview/custom.dart b/printing/lib/src/preview/custom.dart index 2d554335..795a77a4 100644 --- a/printing/lib/src/preview/custom.dart +++ b/printing/lib/src/preview/custom.dart @@ -110,6 +110,8 @@ class PdfPreviewCustomState extends State with PdfPreviewRaster { final listView = GlobalKey(); + late List _pageGlobalKeys; + bool infoLoaded = false; int? preview; @@ -172,6 +174,28 @@ class PdfPreviewCustomState extends State super.didChangeDependencies(); } + @override + void initState() { + super.initState(); + _pageGlobalKeys = List.generate( + pages.length, + (index) => GlobalKey(debugLabel: 'pdf-page-$index'), + ); + } + + void scrollToPage( + int index, { + Duration duration = Duration.zero, + Curve curve = Curves.ease, + ScrollPositionAlignmentPolicy alignmentPolicy = + ScrollPositionAlignmentPolicy.explicit, + }) { + assert(index >= 0); + final pageKey = _pageGlobalKeys[index]; + Scrollable.ensureVisible(pageKey.currentContext!, + duration: duration, curve: curve, alignmentPolicy: alignmentPolicy); + } + Widget _showError(Object error) { if (widget.onError != null) { return widget.onError!(context, error); @@ -215,6 +239,7 @@ class PdfPreviewCustomState extends State }); }, child: PdfPreviewPage( + key: _pageGlobalKeys[index], pageData: pages[index], pdfPreviewPageDecoration: widget.pdfPreviewPageDecoration, pageMargin: widget.previewPageMargin, From 9599e41dcf4b337c87738c69be9797588d1974b7 Mon Sep 17 00:00:00 2001 From: Aleksei <130981115+MacDeveloper1@users.noreply.github.com> Date: Fri, 19 Jan 2024 17:23:57 +0100 Subject: [PATCH 06/14] Init pages keys after `pages` is populated --- printing/lib/src/preview/custom.dart | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/printing/lib/src/preview/custom.dart b/printing/lib/src/preview/custom.dart index 795a77a4..be09a48b 100644 --- a/printing/lib/src/preview/custom.dart +++ b/printing/lib/src/preview/custom.dart @@ -110,7 +110,7 @@ class PdfPreviewCustomState extends State with PdfPreviewRaster { final listView = GlobalKey(); - late List _pageGlobalKeys; + late final List _pageGlobalKeys; bool infoLoaded = false; @@ -174,15 +174,6 @@ class PdfPreviewCustomState extends State super.didChangeDependencies(); } - @override - void initState() { - super.initState(); - _pageGlobalKeys = List.generate( - pages.length, - (index) => GlobalKey(debugLabel: 'pdf-page-$index'), - ); - } - void scrollToPage( int index, { Duration duration = Duration.zero, @@ -221,9 +212,15 @@ class PdfPreviewCustomState extends State ); } + _pageGlobalKeys = List.generate( + pages.length, + (index) => GlobalKey(debugLabel: 'pdf-page-$index'), + ); + if (widget.pagesBuilder != null) { return widget.pagesBuilder!(context, pages); } + return ListView.builder( controller: scrollController, shrinkWrap: widget.shrinkWrap, From ef8bfa9f0e690adf9982e4e7e50b5c4cfb2b5b41 Mon Sep 17 00:00:00 2001 From: Aleksei <130981115+MacDeveloper1@users.noreply.github.com> Date: Fri, 19 Jan 2024 17:28:11 +0100 Subject: [PATCH 07/14] Fix finalizing --- printing/lib/src/preview/custom.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/printing/lib/src/preview/custom.dart b/printing/lib/src/preview/custom.dart index be09a48b..192f8066 100644 --- a/printing/lib/src/preview/custom.dart +++ b/printing/lib/src/preview/custom.dart @@ -110,7 +110,7 @@ class PdfPreviewCustomState extends State with PdfPreviewRaster { final listView = GlobalKey(); - late final List _pageGlobalKeys; + late List _pageGlobalKeys; bool infoLoaded = false; From ff7a059432a3dee9b03e388c3e5f44e02ea98266 Mon Sep 17 00:00:00 2001 From: Aleksei <130981115+MacDeveloper1@users.noreply.github.com> Date: Fri, 19 Jan 2024 17:40:14 +0100 Subject: [PATCH 08/14] Update `scrollToPage` --- printing/lib/src/preview/custom.dart | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/printing/lib/src/preview/custom.dart b/printing/lib/src/preview/custom.dart index 192f8066..3b1d7441 100644 --- a/printing/lib/src/preview/custom.dart +++ b/printing/lib/src/preview/custom.dart @@ -174,16 +174,17 @@ class PdfPreviewCustomState extends State super.didChangeDependencies(); } - void scrollToPage( + Future scrollToPage( int index, { - Duration duration = Duration.zero, + Duration duration = const Duration(milliseconds: 300), Curve curve = Curves.ease, ScrollPositionAlignmentPolicy alignmentPolicy = ScrollPositionAlignmentPolicy.explicit, }) { - assert(index >= 0); - final pageKey = _pageGlobalKeys[index]; - Scrollable.ensureVisible(pageKey.currentContext!, + assert(index >= 0, 'Index of page cannot be negative'); + final pageContext = _pageGlobalKeys[index].currentContext; + assert(pageContext != null, 'Context of GlobalKey cannot be null'); + return Scrollable.ensureVisible(pageContext!, duration: duration, curve: curve, alignmentPolicy: alignmentPolicy); } From 1c895b099a86881c9e27d6f667a66e1ef09c77dc Mon Sep 17 00:00:00 2001 From: Aleksei <130981115+MacDeveloper1@users.noreply.github.com> Date: Mon, 22 Jan 2024 08:18:36 +0100 Subject: [PATCH 09/14] Make `pageGlobalKeys` populated once --- printing/lib/src/preview/custom.dart | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/printing/lib/src/preview/custom.dart b/printing/lib/src/preview/custom.dart index 3b1d7441..68d5be20 100644 --- a/printing/lib/src/preview/custom.dart +++ b/printing/lib/src/preview/custom.dart @@ -110,7 +110,7 @@ class PdfPreviewCustomState extends State with PdfPreviewRaster { final listView = GlobalKey(); - late List _pageGlobalKeys; + List _pageGlobalKeys = []; bool infoLoaded = false; @@ -174,6 +174,7 @@ class PdfPreviewCustomState extends State super.didChangeDependencies(); } + /// Ensures that page with [index] is become visible. Future scrollToPage( int index, { Duration duration = const Duration(milliseconds: 300), @@ -188,6 +189,9 @@ class PdfPreviewCustomState extends State duration: duration, curve: curve, alignmentPolicy: alignmentPolicy); } + /// Returns the global key for page with [index]. + Key getPageKey(int index) => _pageGlobalKeys[index]; + Widget _showError(Object error) { if (widget.onError != null) { return widget.onError!(context, error); @@ -213,10 +217,12 @@ class PdfPreviewCustomState extends State ); } - _pageGlobalKeys = List.generate( - pages.length, - (index) => GlobalKey(debugLabel: 'pdf-page-$index'), - ); + if (_pageGlobalKeys.isEmpty) { + _pageGlobalKeys = List.generate( + pages.length, + (index) => GlobalKey(debugLabel: 'pdf-page-$index'), + ); + } if (widget.pagesBuilder != null) { return widget.pagesBuilder!(context, pages); @@ -237,7 +243,7 @@ class PdfPreviewCustomState extends State }); }, child: PdfPreviewPage( - key: _pageGlobalKeys[index], + key: getPageKey(index), pageData: pages[index], pdfPreviewPageDecoration: widget.pdfPreviewPageDecoration, pageMargin: widget.previewPageMargin, From 95de8782c232fe404f36f47da78565b5dc958cb8 Mon Sep 17 00:00:00 2001 From: Aleksei <130981115+MacDeveloper1@users.noreply.github.com> Date: Mon, 22 Jan 2024 08:46:31 +0100 Subject: [PATCH 10/14] Fill `pageGlobalKeys` oneach re-build --- printing/lib/src/preview/custom.dart | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/printing/lib/src/preview/custom.dart b/printing/lib/src/preview/custom.dart index 68d5be20..877bb81d 100644 --- a/printing/lib/src/preview/custom.dart +++ b/printing/lib/src/preview/custom.dart @@ -217,12 +217,10 @@ class PdfPreviewCustomState extends State ); } - if (_pageGlobalKeys.isEmpty) { - _pageGlobalKeys = List.generate( - pages.length, - (index) => GlobalKey(debugLabel: 'pdf-page-$index'), - ); - } + _pageGlobalKeys = List.generate( + pages.length, + (index) => GlobalKey(debugLabel: 'pdf-page-$index'), + ); if (widget.pagesBuilder != null) { return widget.pagesBuilder!(context, pages); From 750fd0fb66b68ad23d8e19b42ccae4ecfc7ee711 Mon Sep 17 00:00:00 2001 From: Aleksei <130981115+MacDeveloper1@users.noreply.github.com> Date: Mon, 22 Jan 2024 08:59:30 +0100 Subject: [PATCH 11/14] Make `pageGlobalKeys` predefined --- printing/lib/src/preview/custom.dart | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/printing/lib/src/preview/custom.dart b/printing/lib/src/preview/custom.dart index 877bb81d..6e19e9b6 100644 --- a/printing/lib/src/preview/custom.dart +++ b/printing/lib/src/preview/custom.dart @@ -110,7 +110,7 @@ class PdfPreviewCustomState extends State with PdfPreviewRaster { final listView = GlobalKey(); - List _pageGlobalKeys = []; + final _pageGlobalKeys = List.generate(100, (index) => GlobalKey()); bool infoLoaded = false; @@ -217,11 +217,6 @@ class PdfPreviewCustomState extends State ); } - _pageGlobalKeys = List.generate( - pages.length, - (index) => GlobalKey(debugLabel: 'pdf-page-$index'), - ); - if (widget.pagesBuilder != null) { return widget.pagesBuilder!(context, pages); } From 24c7f1ce7b5b9949416700c806d1ba1b7a6fcf8e Mon Sep 17 00:00:00 2001 From: Aleksei <130981115+MacDeveloper1@users.noreply.github.com> Date: Mon, 22 Jan 2024 09:44:46 +0100 Subject: [PATCH 12/14] Add `enableScrollToPage` --- printing/lib/src/preview/custom.dart | 80 ++++++++++++++++------- printing/lib/src/preview/pdf_preview.dart | 7 +- 2 files changed, 63 insertions(+), 24 deletions(-) diff --git a/printing/lib/src/preview/custom.dart b/printing/lib/src/preview/custom.dart index 6e19e9b6..afe1693b 100644 --- a/printing/lib/src/preview/custom.dart +++ b/printing/lib/src/preview/custom.dart @@ -50,6 +50,7 @@ class PdfPreviewCustom extends StatefulWidget { this.scrollPhysics, this.shrinkWrap = false, this.pagesBuilder, + this.enableScrollToPage = false, }) : super(key: key); /// Pdf paper page format @@ -102,6 +103,9 @@ class PdfPreviewCustom extends StatefulWidget { /// their own pages. final CustomPdfPagesBuilder? pagesBuilder; + /// Whether scroll to page functionality enabled. + final bool enableScrollToPage; + @override PdfPreviewCustomState createState() => PdfPreviewCustomState(); } @@ -110,7 +114,7 @@ class PdfPreviewCustomState extends State with PdfPreviewRaster { final listView = GlobalKey(); - final _pageGlobalKeys = List.generate(100, (index) => GlobalKey()); + late List _pageGlobalKeys; bool infoLoaded = false; @@ -217,32 +221,62 @@ class PdfPreviewCustomState extends State ); } + _pageGlobalKeys = List.generate(pages.length, (index) => GlobalKey()); + if (widget.pagesBuilder != null) { return widget.pagesBuilder!(context, pages); } - return ListView.builder( - controller: scrollController, - shrinkWrap: widget.shrinkWrap, - physics: widget.scrollPhysics, - padding: widget.padding, - itemCount: pages.length, - itemBuilder: (BuildContext context, int index) => GestureDetector( - onDoubleTap: () { - setState(() { - updatePosition = scrollController.position.pixels; - preview = index; - transformationController.value.setIdentity(); - }); - }, - child: PdfPreviewPage( - key: getPageKey(index), - pageData: pages[index], - pdfPreviewPageDecoration: widget.pdfPreviewPageDecoration, - pageMargin: widget.previewPageMargin, - ), - ), - ); + return widget.enableScrollToPage + ? Scrollbar( + controller: scrollController, + child: SingleChildScrollView( + controller: scrollController, + physics: widget.scrollPhysics, + child: Column( + children: List.generate( + pages.length, + (index) => GestureDetector( + onDoubleTap: () { + setState(() { + updatePosition = scrollController.position.pixels; + preview = index; + transformationController.value.setIdentity(); + }); + }, + child: PdfPreviewPage( + key: getPageKey(index), + pageData: pages[index], + pdfPreviewPageDecoration: widget.pdfPreviewPageDecoration, + pageMargin: widget.previewPageMargin, + ), + ), + ), + ), + ), + ) + : ListView.builder( + controller: scrollController, + shrinkWrap: widget.shrinkWrap, + physics: widget.scrollPhysics, + padding: widget.padding, + itemCount: pages.length, + itemBuilder: (BuildContext context, int index) => GestureDetector( + onDoubleTap: () { + setState(() { + updatePosition = scrollController.position.pixels; + preview = index; + transformationController.value.setIdentity(); + }); + }, + child: PdfPreviewPage( + key: getPageKey(index), + pageData: pages[index], + pdfPreviewPageDecoration: widget.pdfPreviewPageDecoration, + pageMargin: widget.previewPageMargin, + ), + ), + ); } Widget _zoomPreview() { diff --git a/printing/lib/src/preview/pdf_preview.dart b/printing/lib/src/preview/pdf_preview.dart index 6fc3b346..c322e64d 100644 --- a/printing/lib/src/preview/pdf_preview.dart +++ b/printing/lib/src/preview/pdf_preview.dart @@ -64,6 +64,7 @@ class PdfPreview extends StatefulWidget { this.onPageFormatChanged, this.dpi, this.actionBarTheme = const PdfActionBarTheme(), + this.enableScrollToPage = false, }) : _pagesBuilder = null, super(key: key); @@ -123,6 +124,7 @@ class PdfPreview extends StatefulWidget { this.dpi, this.actionBarTheme = const PdfActionBarTheme(), required CustomPdfPagesBuilder pagesBuilder, + this.enableScrollToPage = false, }) : _pagesBuilder = pagesBuilder, super(key: key); @@ -233,6 +235,9 @@ class PdfPreview extends StatefulWidget { /// their own pages. final CustomPdfPagesBuilder? _pagesBuilder; + /// Whether scroll to page functionality enabled. + final bool enableScrollToPage; + @override PdfPreviewState createState() => PdfPreviewState(); } @@ -302,7 +307,6 @@ class PdfPreviewState extends State { initialPageFormat: previewData.pageFormat, onComputeActualPageFormat: computeActualPageFormat, ); - setState(() {}); } super.didUpdateWidget(oldWidget); } @@ -406,6 +410,7 @@ class PdfPreviewState extends State { shouldRepaint: widget.shouldRepaint, pagesBuilder: widget._pagesBuilder, dpi: widget.dpi, + enableScrollToPage: widget.enableScrollToPage, ); }), ), From 8bfcced0490ba5ca8d5eb5b045637a723bda4271 Mon Sep 17 00:00:00 2001 From: Aleksei <130981115+MacDeveloper1@users.noreply.github.com> Date: Mon, 22 Jan 2024 10:24:39 +0100 Subject: [PATCH 13/14] Minor optimization --- printing/lib/src/preview/custom.dart | 55 ++++++++++++---------------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/printing/lib/src/preview/custom.dart b/printing/lib/src/preview/custom.dart index afe1693b..4ec78075 100644 --- a/printing/lib/src/preview/custom.dart +++ b/printing/lib/src/preview/custom.dart @@ -114,7 +114,7 @@ class PdfPreviewCustomState extends State with PdfPreviewRaster { final listView = GlobalKey(); - late List _pageGlobalKeys; + List _pageGlobalKeys = []; bool infoLoaded = false; @@ -221,36 +221,41 @@ class PdfPreviewCustomState extends State ); } - _pageGlobalKeys = List.generate(pages.length, (index) => GlobalKey()); + if (widget.enableScrollToPage) { + _pageGlobalKeys = List.generate(pages.length, (_) => GlobalKey()); + } if (widget.pagesBuilder != null) { return widget.pagesBuilder!(context, pages); } + Widget pageWidget(int index, {Key? key}) => GestureDetector( + onDoubleTap: () { + setState(() { + updatePosition = scrollController.position.pixels; + preview = index; + transformationController.value.setIdentity(); + }); + }, + child: PdfPreviewPage( + key: key, + pageData: pages[index], + pdfPreviewPageDecoration: widget.pdfPreviewPageDecoration, + pageMargin: widget.previewPageMargin, + ), + ); + return widget.enableScrollToPage ? Scrollbar( controller: scrollController, child: SingleChildScrollView( controller: scrollController, physics: widget.scrollPhysics, + padding: widget.padding, child: Column( children: List.generate( pages.length, - (index) => GestureDetector( - onDoubleTap: () { - setState(() { - updatePosition = scrollController.position.pixels; - preview = index; - transformationController.value.setIdentity(); - }); - }, - child: PdfPreviewPage( - key: getPageKey(index), - pageData: pages[index], - pdfPreviewPageDecoration: widget.pdfPreviewPageDecoration, - pageMargin: widget.previewPageMargin, - ), - ), + (index) => pageWidget(index, key: getPageKey(index)), ), ), ), @@ -261,21 +266,7 @@ class PdfPreviewCustomState extends State physics: widget.scrollPhysics, padding: widget.padding, itemCount: pages.length, - itemBuilder: (BuildContext context, int index) => GestureDetector( - onDoubleTap: () { - setState(() { - updatePosition = scrollController.position.pixels; - preview = index; - transformationController.value.setIdentity(); - }); - }, - child: PdfPreviewPage( - key: getPageKey(index), - pageData: pages[index], - pdfPreviewPageDecoration: widget.pdfPreviewPageDecoration, - pageMargin: widget.previewPageMargin, - ), - ), + itemBuilder: (BuildContext context, int index) => pageWidget(index), ); } From 50ea984043f26a77b9285e3c367c57cec1cdc5bd Mon Sep 17 00:00:00 2001 From: David PHAM-VAN Date: Sat, 27 Jan 2024 11:53:47 -0400 Subject: [PATCH 14/14] Update changelog --- printing/CHANGELOG.md | 3 ++- printing/pubspec.yaml | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/printing/CHANGELOG.md b/printing/CHANGELOG.md index badbf2f4..e8d2766c 100644 --- a/printing/CHANGELOG.md +++ b/printing/CHANGELOG.md @@ -1,8 +1,9 @@ # Changelog -## 5.11.2 +## 5.12.0 - Refactor html imports +- Implement PdfActionBarTheme for actions bar and add method scrollToPage [Aleksei] ## 5.11.1 diff --git a/printing/pubspec.yaml b/printing/pubspec.yaml index 115c777d..caa5877e 100644 --- a/printing/pubspec.yaml +++ b/printing/pubspec.yaml @@ -15,7 +15,7 @@ topics: - print - printing - report -version: 5.11.2 +version: 5.12.0 environment: sdk: ">=3.0.0 <4.0.0"