Skip to content

Commit

Permalink
Merge branch 'MacDeveloper1-feature/bar'
Browse files Browse the repository at this point in the history
  • Loading branch information
DavBfr committed Jan 27, 2024
2 parents df3167c + 50ea984 commit ea5ecca
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 28 deletions.
3 changes: 2 additions & 1 deletion printing/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
1 change: 1 addition & 0 deletions printing/lib/printing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
105 changes: 105 additions & 0 deletions printing/lib/src/preview/action_bar_theme.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
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.0,
this.actionSpacing = 0.0,
this.alignment = WrapAlignment.spaceAround,
this.runAlignment = WrapAlignment.center,
this.crossAxisAlignment = WrapCrossAlignment.center,
});

final Color? backgroundColor;
final Color? iconColor;
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
/// new values.
PdfActionBarTheme copyWith({
Color? backgroundColor,
Color? iconColor,
double? height,
TextStyle? textStyle,
double? elevation,
double? actionSpacing,
WrapAlignment? alignment,
WrapAlignment? runAlignment,
WrapCrossAlignment? crossAxisAlignment,
}) {
return PdfActionBarTheme(
backgroundColor: backgroundColor ?? this.backgroundColor,
iconColor: iconColor ?? this.iconColor,
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,
);
}

@override
int get hashCode => Object.hashAll([
backgroundColor,
iconColor,
height,
textStyle,
elevation,
actionSpacing,
alignment,
runAlignment,
crossAxisAlignment,
]);

@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.actionSpacing == actionSpacing &&
other.alignment == alignment &&
other.runAlignment == runAlignment &&
other.crossAxisAlignment == crossAxisAlignment;
}

@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', textStyle));
properties.add(DoubleProperty('elevation', elevation));
properties.add(DoubleProperty('actionSpacing', actionSpacing));
properties.add(DiagnosticsProperty<WrapAlignment>('alignment', alignment,
defaultValue: WrapAlignment.spaceAround));
properties.add(DiagnosticsProperty<WrapAlignment>(
'runAlignment', runAlignment,
defaultValue: WrapAlignment.center));
properties.add(DiagnosticsProperty<WrapCrossAlignment>(
'crossAxisAlignment', crossAxisAlignment,
defaultValue: WrapCrossAlignment.center));
}
}
89 changes: 68 additions & 21 deletions printing/lib/src/preview/custom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand All @@ -110,6 +114,8 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom>
with PdfPreviewRaster {
final listView = GlobalKey();

List<GlobalKey> _pageGlobalKeys = <GlobalKey>[];

bool infoLoaded = false;

int? preview;
Expand Down Expand Up @@ -172,6 +178,24 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom>
super.didChangeDependencies();
}

/// Ensures that page with [index] is become visible.
Future<void> scrollToPage(
int index, {
Duration duration = const Duration(milliseconds: 300),
Curve curve = Curves.ease,
ScrollPositionAlignmentPolicy alignmentPolicy =
ScrollPositionAlignmentPolicy.explicit,
}) {
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);
}

/// 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);
Expand All @@ -197,30 +221,53 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom>
);
}

if (widget.enableScrollToPage) {
_pageGlobalKeys = List.generate(pages.length, (_) => 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(
pageData: pages[index],
pdfPreviewPageDecoration: widget.pdfPreviewPageDecoration,
pageMargin: widget.previewPageMargin,
),
),
);

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) => pageWidget(index, key: getPageKey(index)),
),
),
),
)
: ListView.builder(
controller: scrollController,
shrinkWrap: widget.shrinkWrap,
physics: widget.scrollPhysics,
padding: widget.padding,
itemCount: pages.length,
itemBuilder: (BuildContext context, int index) => pageWidget(index),
);
}

Widget _zoomPreview() {
Expand Down
28 changes: 23 additions & 5 deletions printing/lib/src/preview/pdf_preview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -62,6 +63,8 @@ class PdfPreview extends StatefulWidget {
this.loadingWidget,
this.onPageFormatChanged,
this.dpi,
this.actionBarTheme = const PdfActionBarTheme(),
this.enableScrollToPage = false,
}) : _pagesBuilder = null,
super(key: key);

Expand Down Expand Up @@ -119,7 +122,9 @@ class PdfPreview extends StatefulWidget {
this.loadingWidget,
this.onPageFormatChanged,
this.dpi,
this.actionBarTheme = const PdfActionBarTheme(),
required CustomPdfPagesBuilder pagesBuilder,
this.enableScrollToPage = false,
}) : _pagesBuilder = pagesBuilder,
super(key: key);

Expand Down Expand Up @@ -223,10 +228,16 @@ 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;

/// Whether scroll to page functionality enabled.
final bool enableScrollToPage;

@override
PdfPreviewState createState() => PdfPreviewState();
}
Expand Down Expand Up @@ -296,7 +307,6 @@ class PdfPreviewState extends State<PdfPreview> {
initialPageFormat: previewData.pageFormat,
onComputeActualPageFormat: computeActualPageFormat,
);
setState(() {});
}
super.didUpdateWidget(oldWidget);
}
Expand Down Expand Up @@ -400,22 +410,30 @@ class PdfPreviewState extends State<PdfPreview> {
shouldRepaint: widget.shouldRepaint,
pagesBuilder: widget._pagesBuilder,
dpi: widget.dpi,
enableScrollToPage: widget.enableScrollToPage,
);
}),
),
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,
spacing: widget.actionBarTheme.actionSpacing,
alignment: widget.actionBarTheme.alignment,
runAlignment: widget.actionBarTheme.runAlignment,
crossAxisAlignment:
widget.actionBarTheme.crossAxisAlignment,
children: actions,
),
),
Expand Down
2 changes: 1 addition & 1 deletion printing/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ topics:
- print
- printing
- report
version: 5.11.2
version: 5.12.0

environment:
sdk: ">=3.0.0 <4.0.0"
Expand Down

0 comments on commit ea5ecca

Please sign in to comment.