Skip to content

Commit

Permalink
fixup! fixup! TF-2764 Handle printing PDF attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
dab246 committed May 16, 2024
1 parent 540a9c9 commit 8601efe
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:core/utils/app_logger.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:pdf_render/pdf_render_widgets.dart';
import 'package:tmail_ui_user/main/localizations/app_localizations.dart';

Expand All @@ -23,36 +22,28 @@ class PaginationPDFViewer extends StatefulWidget {
}

class _PaginationPDFViewerState extends State<PaginationPDFViewer> {
static const double _maxZoomLevelDefault = 3.0;
static const double _maxZoomLevelDefault = 4.0;
static const double _minZoomLevelDefault = 1.0;

final TextEditingController _editingController = TextEditingController();
final ValueNotifier<ZoomState> _zoomInPageNotifier = ValueNotifier<ZoomState>(ZoomState.activate);
final ValueNotifier<ZoomState> _zoomOutPageNotifier = ValueNotifier<ZoomState>(ZoomState.inactivate);
final ValueNotifier<int> _pageCurrentNotifier = ValueNotifier<int>(1);

int _currentPage = 1;
int _totalPage = 1;
double _zoomLevel = 1.0;
bool _isPageJumping = false;

@override
void initState() {
_currentPage = 1;
_zoomLevel = 1.0;
_totalPage = widget.pdfViewerController?.pageCount ?? 1;
_editingController.text = '$_currentPage';
_pageCurrentNotifier.value = 1;
widget.pdfViewerController?.addListener(_pageChanged);
super.initState();
}

@override
void dispose() {
_currentPage = 1;
_zoomLevel = 1.0;
_totalPage = 1;
_isPageJumping = false;
widget.pdfViewerController?.removeListener(_pageChanged);
_editingController.dispose();
_pageCurrentNotifier.dispose();
_zoomInPageNotifier.dispose();
_zoomOutPageNotifier.dispose();
super.dispose();
Expand Down Expand Up @@ -80,35 +71,17 @@ class _PaginationPDFViewerState extends State<PaginationPDFViewer> {
),
),
const SizedBox(width: 12),
Material(
type: MaterialType.transparency,
child: Container(
width: 40,
height: 30,
margin: const EdgeInsets.only(top: 8),
child: TextField(
controller: _editingController,
keyboardType: TextInputType.number,
textAlign: TextAlign.center,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],
ValueListenableBuilder(
valueListenable: _pageCurrentNotifier,
builder: (_, value, __) {
return Text(
'$value',
style: Theme.of(context).textTheme.labelSmall?.copyWith(
color: Colors.white,
fontSize: 14
),
onSubmitted: _onSubmitTextInputFieldListener,
decoration: InputDecoration(
border: InputBorder.none,
filled: true,
isDense: true,
fillColor: Colors.black.withOpacity(0.9),
contentPadding: const EdgeInsets.symmetric(vertical: 5)
),
cursorColor: Colors.white,
),
),
),
);
}),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Text(
Expand All @@ -120,7 +93,7 @@ class _PaginationPDFViewerState extends State<PaginationPDFViewer> {
),
),
Text(
'$_totalPage',
'${widget.pdfViewerController?.pageCount}',
style: Theme.of(context).textTheme.labelSmall?.copyWith(
color: Colors.white,
fontSize: 14
Expand Down Expand Up @@ -186,45 +159,9 @@ class _PaginationPDFViewerState extends State<PaginationPDFViewer> {
}
}

void _onSubmitTextInputFieldListener(String text) {
try {
final page = int.tryParse(text) ?? 1;
if (page > 1 && page <= _totalPage) {
_currentPage = page;
} else {
_currentPage = 1;
}
_editingController.text = '$_currentPage';
_jumpToPage(_currentPage);
} catch (e) {
logError('_PaginationPDFViewerState::_onSubmitTextInputFieldListener: Exception = $e');
_currentPage = 1;
_editingController.text = '$_currentPage';
_jumpToPage(_currentPage);
}
}

void _pageChanged({String? property}) async {
if (_isPageJumping) {
_isPageJumping = false;
return;
}
log('_PaginationPDFViewerState::_pageChanged');
if (widget.pdfViewerController != null
&& _currentPage != widget.pdfViewerController!.currentPageNumber) {
await Future<dynamic>.delayed(const Duration(milliseconds: 100), () {
_currentPage = widget.pdfViewerController!.currentPageNumber;
_editingController.text = '$_currentPage';
});
}
}

void _jumpToPage(int page) {
_isPageJumping = true;
widget.pdfViewerController?.goToPage(
pageNumber: page,
duration: Duration.zero
);
void _pageChanged({String? property}) {
_pageCurrentNotifier.value = widget.pdfViewerController?.currentPageNumber ?? 1;
_updateZoomState();
}

void _zoomInPage() {
Expand All @@ -238,7 +175,11 @@ class _PaginationPDFViewerState extends State<PaginationPDFViewer> {
} else if (_zoomLevel >= 2.0 && _zoomLevel < 2.50) {
_zoomLevel = 2.5;
} else if (_zoomLevel >= 2.5 && _zoomLevel < 3.0) {
_zoomLevel = 3;
_zoomLevel = 3.0;
} else if (_zoomLevel >= 3.0 && _zoomLevel < 3.5) {
_zoomLevel = 3.5;
} else if (_zoomLevel >= 3.5 && _zoomLevel < 4.0) {
_zoomLevel = 4.0;
}
_updateZoomState();
widget.pdfViewerController?.setZoomRatio(zoomRatio: _zoomLevel);
Expand All @@ -257,20 +198,26 @@ class _PaginationPDFViewerState extends State<PaginationPDFViewer> {
_zoomLevel = 2.0;
} else if (_zoomLevel > 2.5 && _zoomLevel <= 3.0) {
_zoomLevel = 2.5;
} else if (_zoomLevel > 3.0 && _zoomLevel <= 3.5) {
_zoomLevel = 3.0;
} else if (_zoomLevel > 3.5 && _zoomLevel <= 4.0) {
_zoomLevel = 3.5;
}
_updateZoomState();
widget.pdfViewerController?.setZoomRatio(zoomRatio: _zoomLevel);
}
}

void _updateZoomState() {
if (_zoomLevel <= _maxZoomLevelDefault && _zoomLevel > _minZoomLevelDefault) {
final zoomLevel = widget.pdfViewerController?.zoomRatio ?? 1.0;
log('_PaginationPDFViewerState::_updateZoomState:zoomLevel = $zoomLevel');
if (zoomLevel <= _maxZoomLevelDefault && zoomLevel > _minZoomLevelDefault) {
_zoomOutPageNotifier.value = ZoomState.activate;
} else {
_zoomOutPageNotifier.value = ZoomState.inactivate;
}

if (_zoomLevel < _maxZoomLevelDefault && _zoomLevel >= _minZoomLevelDefault) {
if (zoomLevel < _maxZoomLevelDefault && zoomLevel >= _minZoomLevelDefault) {
_zoomInPageNotifier.value = ZoomState.activate;
} else {
_zoomInPageNotifier.value = ZoomState.inactivate;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:math';
import 'dart:typed_data';

import 'package:core/presentation/extensions/color_extension.dart';
Expand Down Expand Up @@ -132,11 +133,27 @@ class _PDFViewerState extends State<PDFViewer> {
},
params: PdfViewerParams(
panAxis: PanAxis.vertical,
padding: 52,
scrollByMouseWheel: 0.5,
onViewerControllerInitialized: (controller) {
_pdfViewerController = controller;
_pdfControllerInitialStateNotifier.value = true;
},
layoutPages: (viewSize, pages) {
List<Rect> rect = [];
final viewWidth = viewSize.width;
final viewHeight = viewSize.height;
final maxHeight = pages.fold<double>(0.0, (maxHeight, page) => max(maxHeight, page.height));
final ratio = viewHeight / maxHeight;
var top = 0.0;
double padding = 16.0;
for (var page in pages) {
final width = page.width * ratio;
final height = page.height * ratio;
final left = viewWidth > viewHeight ? (viewWidth / 2) - (width / 2) : 0.0;
rect.add(Rect.fromLTWH(left, top, width, height));
top += height + padding;
}
return rect;
}
),
);
Expand Down

0 comments on commit 8601efe

Please sign in to comment.