Skip to content

Commit

Permalink
Merge branch 'MacDeveloper1-feature/zoom'
Browse files Browse the repository at this point in the history
  • Loading branch information
DavBfr committed Jan 27, 2024
2 parents ea5ecca + 403dfcd commit 2d2b21a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions printing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Refactor html imports
- Implement PdfActionBarTheme for actions bar and add method scrollToPage [Aleksei]
- Update cursors in zoom mode for web [Aleksei]

## 5.11.1

Expand Down
35 changes: 34 additions & 1 deletion printing/lib/src/preview/custom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:pdf/pdf.dart';

Expand Down Expand Up @@ -51,6 +52,7 @@ class PdfPreviewCustom extends StatefulWidget {
this.shrinkWrap = false,
this.pagesBuilder,
this.enableScrollToPage = false,
this.onZoomChanged,
}) : super(key: key);

/// Pdf paper page format
Expand Down Expand Up @@ -106,6 +108,9 @@ class PdfPreviewCustom extends StatefulWidget {
/// Whether scroll to page functionality enabled.
final bool enableScrollToPage;

/// The zoom mode has changed
final ValueChanged<bool>? onZoomChanged;

@override
PdfPreviewCustomState createState() => PdfPreviewCustomState();
}
Expand All @@ -130,13 +135,16 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom>

Timer? previewUpdate;

MouseCursor _mouseCursor = MouseCursor.defer;

static const _errorMessage = 'Unable to display the document';

@override
double? get forcedDpi => widget.dpi;

@override
void dispose() {
transformationController.dispose();
previewUpdate?.cancel();
super.dispose();
}
Expand All @@ -155,6 +163,7 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom>
preview = null;
updatePosition = null;
raster();
_zoomChanged();
}
super.didUpdateWidget(oldWidget);
}
Expand Down Expand Up @@ -235,7 +244,9 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom>
updatePosition = scrollController.position.pixels;
preview = index;
transformationController.value.setIdentity();
_updateCursor(SystemMouseCursors.grab);
});
_zoomChanged();
},
child: PdfPreviewPage(
key: key,
Expand Down Expand Up @@ -271,15 +282,23 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom>
}

Widget _zoomPreview() {
return GestureDetector(
final zoomPreview = GestureDetector(
onDoubleTap: () {
setState(() {
preview = null;
_updateCursor(MouseCursor.defer);
});
_zoomChanged();
},
onLongPressCancel:
kIsWeb ? () => _updateCursor(SystemMouseCursors.grab) : null,
onLongPressDown:
kIsWeb ? (_) => _updateCursor(SystemMouseCursors.grabbing) : null,
child: InteractiveViewer(
transformationController: transformationController,
maxScale: 5,
onInteractionEnd:
kIsWeb ? (_) => _updateCursor(SystemMouseCursors.grab) : null,
child: Center(
child: PdfPreviewPage(
pageData: pages[preview!],
Expand All @@ -289,6 +308,20 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom>
),
),
);
return MouseRegion(
cursor: _mouseCursor,
child: zoomPreview,
);
}

void _zoomChanged() => widget.onZoomChanged?.call(preview != null);

void _updateCursor(MouseCursor mouseCursor) {
if (mouseCursor != _mouseCursor) {
setState(() {
_mouseCursor = mouseCursor;
});
}
}

@override
Expand Down
6 changes: 6 additions & 0 deletions printing/lib/src/preview/pdf_preview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class PdfPreview extends StatefulWidget {
this.dpi,
this.actionBarTheme = const PdfActionBarTheme(),
this.enableScrollToPage = false,
this.onZoomChanged,
}) : _pagesBuilder = null,
super(key: key);

Expand Down Expand Up @@ -125,6 +126,7 @@ class PdfPreview extends StatefulWidget {
this.actionBarTheme = const PdfActionBarTheme(),
required CustomPdfPagesBuilder pagesBuilder,
this.enableScrollToPage = false,
this.onZoomChanged,
}) : _pagesBuilder = pagesBuilder,
super(key: key);

Expand Down Expand Up @@ -238,6 +240,9 @@ class PdfPreview extends StatefulWidget {
/// Whether scroll to page functionality enabled.
final bool enableScrollToPage;

/// The zoom mode has changed
final ValueChanged<bool>? onZoomChanged;

@override
PdfPreviewState createState() => PdfPreviewState();
}
Expand Down Expand Up @@ -411,6 +416,7 @@ class PdfPreviewState extends State<PdfPreview> {
pagesBuilder: widget._pagesBuilder,
dpi: widget.dpi,
enableScrollToPage: widget.enableScrollToPage,
onZoomChanged: widget.onZoomChanged,
);
}),
),
Expand Down

0 comments on commit 2d2b21a

Please sign in to comment.