356 lines
12 KiB
Dart
356 lines
12 KiB
Dart
import 'dart:typed_data';
|
|
import 'package:file_selector/file_selector.dart' as fs;
|
|
import 'package:flutter/foundation.dart' show kIsWeb;
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:pdf_signature/l10n/app_localizations.dart';
|
|
import 'package:printing/printing.dart' as printing;
|
|
|
|
import '../../../../data/services/providers.dart';
|
|
import '../view_model/view_model.dart';
|
|
import 'draw_canvas.dart';
|
|
import 'pdf_toolbar.dart';
|
|
import 'pdf_page_area.dart';
|
|
import 'adjustments_panel.dart';
|
|
import '../../preferences/widgets/settings_screen.dart';
|
|
|
|
class PdfSignatureHomePage extends ConsumerStatefulWidget {
|
|
const PdfSignatureHomePage({super.key});
|
|
|
|
@override
|
|
ConsumerState<PdfSignatureHomePage> createState() =>
|
|
_PdfSignatureHomePageState();
|
|
}
|
|
|
|
class _PdfSignatureHomePageState extends ConsumerState<PdfSignatureHomePage> {
|
|
static const Size _pageSize = SignatureController.pageSize;
|
|
|
|
// Exposed for tests to trigger the invalid-file SnackBar without UI.
|
|
@visibleForTesting
|
|
void debugShowInvalidSignatureSnackBar() {
|
|
ref.read(signatureProvider.notifier).setInvalidSelected(context);
|
|
}
|
|
|
|
Future<void> _pickPdf() async {
|
|
final typeGroup = const fs.XTypeGroup(label: 'PDF', extensions: ['pdf']);
|
|
final file = await fs.openFile(acceptedTypeGroups: [typeGroup]);
|
|
if (file != null) {
|
|
Uint8List? bytes;
|
|
try {
|
|
bytes = await file.readAsBytes();
|
|
} catch (_) {
|
|
bytes = null;
|
|
}
|
|
ref.read(pdfProvider.notifier).openPicked(path: file.path, bytes: bytes);
|
|
ref.read(signatureProvider.notifier).resetForNewPage();
|
|
}
|
|
}
|
|
|
|
void _jumpToPage(int page) {
|
|
ref.read(pdfProvider.notifier).jumpTo(page);
|
|
}
|
|
|
|
// mark-for-signing removed; no toggle needed
|
|
|
|
Future<void> _loadSignatureFromFile() async {
|
|
final typeGroup = const fs.XTypeGroup(
|
|
label: 'Image',
|
|
extensions: ['png', 'jpg', 'jpeg', 'webp'],
|
|
);
|
|
final file = await fs.openFile(acceptedTypeGroups: [typeGroup]);
|
|
if (file == null) return;
|
|
final bytes = await file.readAsBytes();
|
|
final sig = ref.read(signatureProvider.notifier);
|
|
sig.setImageBytes(bytes);
|
|
// When a signature is added, set the current page as signed.
|
|
final p = ref.read(pdfProvider);
|
|
if (p.loaded) {
|
|
ref.read(pdfProvider.notifier).setSignedPage(p.currentPage);
|
|
}
|
|
}
|
|
|
|
void _createNewSignature() {
|
|
// Create a movable signature (draft) that won't be exported until confirmed
|
|
final sig = ref.read(signatureProvider.notifier);
|
|
if (ref.read(pdfProvider).loaded) {
|
|
sig.placeDefaultRect();
|
|
ref
|
|
.read(pdfProvider.notifier)
|
|
.setSignedPage(ref.read(pdfProvider).currentPage);
|
|
// Hint: how to confirm/delete via context menu
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(
|
|
AppLocalizations.of(
|
|
context,
|
|
).longPressOrRightClickTheSignatureToConfirmOrDelete,
|
|
),
|
|
duration: Duration(seconds: 3),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
void _confirmSignature() {
|
|
// Confirm: make current signature immutable and eligible for export by placing it
|
|
ref.read(signatureProvider.notifier).confirmCurrentSignature(ref);
|
|
}
|
|
|
|
void _onDragSignature(Offset delta) {
|
|
ref.read(signatureProvider.notifier).drag(delta);
|
|
}
|
|
|
|
void _onResizeSignature(Offset delta) {
|
|
ref.read(signatureProvider.notifier).resize(delta);
|
|
}
|
|
|
|
void _onSelectPlaced(int? index) {
|
|
ref.read(pdfProvider.notifier).selectPlacement(index);
|
|
}
|
|
|
|
Future<void> _openDrawCanvas() async {
|
|
final result = await showModalBottomSheet<Uint8List>(
|
|
context: context,
|
|
isScrollControlled: true,
|
|
enableDrag: false,
|
|
builder: (_) => const DrawCanvas(),
|
|
);
|
|
if (result != null && result.isNotEmpty) {
|
|
// Use the drawn image as signature content
|
|
ref.read(signatureProvider.notifier).setImageBytes(result);
|
|
// Mark current page as signed when a signature is created
|
|
final p = ref.read(pdfProvider);
|
|
if (p.loaded) {
|
|
ref.read(pdfProvider.notifier).setSignedPage(p.currentPage);
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> _saveSignedPdf() async {
|
|
// Set exporting state to show loading overlay and block interactions
|
|
ref.read(exportingProvider.notifier).state = true;
|
|
try {
|
|
final pdf = ref.read(pdfProvider);
|
|
final sig = ref.read(signatureProvider);
|
|
// Cache messenger before any awaits to avoid using BuildContext across async gaps.
|
|
final messenger = ScaffoldMessenger.of(context);
|
|
if (!pdf.loaded || sig.rect == null) {
|
|
messenger.showSnackBar(
|
|
SnackBar(
|
|
content: Text(AppLocalizations.of(context).nothingToSaveYet),
|
|
), // guard per use-case
|
|
);
|
|
return;
|
|
}
|
|
final exporter = ref.read(exportServiceProvider);
|
|
final targetDpi = ref.read(exportDpiProvider);
|
|
final useMock = ref.read(useMockViewerProvider);
|
|
bool ok = false;
|
|
String? savedPath;
|
|
if (kIsWeb) {
|
|
// Web: prefer using picked bytes; share via Printing
|
|
Uint8List? src = pdf.pickedPdfBytes;
|
|
if (src == null) {
|
|
ok = false;
|
|
} else {
|
|
final processed = ref.read(processedSignatureImageProvider);
|
|
final bytes = await exporter.exportSignedPdfFromBytes(
|
|
srcBytes: src,
|
|
signedPage: pdf.signedPage,
|
|
signatureRectUi: sig.rect,
|
|
uiPageSize: SignatureController.pageSize,
|
|
signatureImageBytes: processed ?? sig.imageBytes,
|
|
placementsByPage: pdf.placementsByPage,
|
|
targetDpi: targetDpi,
|
|
);
|
|
if (bytes != null) {
|
|
try {
|
|
await printing.Printing.sharePdf(
|
|
bytes: bytes,
|
|
filename: 'signed.pdf',
|
|
);
|
|
ok = true;
|
|
} catch (_) {
|
|
ok = false;
|
|
}
|
|
} else {
|
|
ok = false;
|
|
}
|
|
}
|
|
} else {
|
|
// Desktop/mobile: choose between bytes or file-based export
|
|
final pick = ref.read(savePathPickerProvider);
|
|
final path = await pick();
|
|
if (path == null || path.trim().isEmpty) return;
|
|
final fullPath = _ensurePdfExtension(path.trim());
|
|
savedPath = fullPath;
|
|
if (pdf.pickedPdfBytes != null) {
|
|
final processed = ref.read(processedSignatureImageProvider);
|
|
final out = await exporter.exportSignedPdfFromBytes(
|
|
srcBytes: pdf.pickedPdfBytes!,
|
|
signedPage: pdf.signedPage,
|
|
signatureRectUi: sig.rect,
|
|
uiPageSize: SignatureController.pageSize,
|
|
signatureImageBytes: processed ?? sig.imageBytes,
|
|
placementsByPage: pdf.placementsByPage,
|
|
targetDpi: targetDpi,
|
|
);
|
|
if (useMock) {
|
|
// In mock mode for tests, simulate success without file IO
|
|
ok = out != null;
|
|
} else if (out != null) {
|
|
ok = await exporter.saveBytesToFile(
|
|
bytes: out,
|
|
outputPath: fullPath,
|
|
);
|
|
} else {
|
|
ok = false;
|
|
}
|
|
} else if (pdf.pickedPdfPath != null) {
|
|
if (useMock) {
|
|
// Simulate success in mock
|
|
ok = true;
|
|
} else {
|
|
final processed = ref.read(processedSignatureImageProvider);
|
|
ok = await exporter.exportSignedPdfFromFile(
|
|
inputPath: pdf.pickedPdfPath!,
|
|
outputPath: fullPath,
|
|
signedPage: pdf.signedPage,
|
|
signatureRectUi: sig.rect,
|
|
uiPageSize: SignatureController.pageSize,
|
|
signatureImageBytes: processed ?? sig.imageBytes,
|
|
placementsByPage: pdf.placementsByPage,
|
|
targetDpi: targetDpi,
|
|
);
|
|
}
|
|
} else {
|
|
ok = false;
|
|
}
|
|
}
|
|
if (!kIsWeb) {
|
|
// Desktop/mobile: we had a concrete path
|
|
if (ok) {
|
|
messenger.showSnackBar(
|
|
SnackBar(
|
|
content: Text(
|
|
AppLocalizations.of(context).savedWithPath(savedPath ?? ''),
|
|
),
|
|
),
|
|
);
|
|
} else {
|
|
messenger.showSnackBar(
|
|
SnackBar(
|
|
content: Text(AppLocalizations.of(context).failedToSavePdf),
|
|
),
|
|
);
|
|
}
|
|
} else {
|
|
// Web: indicate whether we triggered a download dialog
|
|
if (ok) {
|
|
messenger.showSnackBar(
|
|
SnackBar(
|
|
content: Text(AppLocalizations.of(context).downloadStarted),
|
|
),
|
|
);
|
|
} else {
|
|
messenger.showSnackBar(
|
|
SnackBar(
|
|
content: Text(AppLocalizations.of(context).failedToGeneratePdf),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
} finally {
|
|
// Clear exporting state when finished or on error
|
|
ref.read(exportingProvider.notifier).state = false;
|
|
}
|
|
}
|
|
|
|
String _ensurePdfExtension(String name) {
|
|
if (!name.toLowerCase().endsWith('.pdf')) return '$name.pdf';
|
|
return name;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final isExporting = ref.watch(exportingProvider);
|
|
final l = AppLocalizations.of(context);
|
|
return Scaffold(
|
|
appBar: AppBar(title: Text(l.appTitle)),
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(12),
|
|
child: Stack(
|
|
children: [
|
|
Column(
|
|
children: [
|
|
PdfToolbar(
|
|
disabled: isExporting,
|
|
onOpenSettings: () {
|
|
Navigator.of(context).push(
|
|
MaterialPageRoute(builder: (_) => const SettingsScreen()),
|
|
);
|
|
},
|
|
onPickPdf: _pickPdf,
|
|
onJumpToPage: _jumpToPage,
|
|
onSave: _saveSignedPdf,
|
|
onLoadSignatureFromFile: _loadSignatureFromFile,
|
|
onCreateSignature: _createNewSignature,
|
|
onOpenDrawCanvas: _openDrawCanvas,
|
|
),
|
|
const SizedBox(height: 8),
|
|
Expanded(
|
|
child: AbsorbPointer(
|
|
absorbing: isExporting,
|
|
child: PdfPageArea(
|
|
pageSize: _pageSize,
|
|
onDragSignature: _onDragSignature,
|
|
onResizeSignature: _onResizeSignature,
|
|
onConfirmSignature: _confirmSignature,
|
|
onClearActiveOverlay:
|
|
() =>
|
|
ref
|
|
.read(signatureProvider.notifier)
|
|
.clearActiveOverlay(),
|
|
onSelectPlaced: _onSelectPlaced,
|
|
),
|
|
),
|
|
),
|
|
Consumer(
|
|
builder: (context, ref, _) {
|
|
final sig = ref.watch(signatureProvider);
|
|
return sig.rect != null
|
|
? AbsorbPointer(
|
|
absorbing: isExporting,
|
|
child: AdjustmentsPanel(sig: sig),
|
|
)
|
|
: const SizedBox.shrink();
|
|
},
|
|
),
|
|
],
|
|
),
|
|
if (isExporting)
|
|
Positioned.fill(
|
|
child: Container(
|
|
color: Colors.black45,
|
|
child: Center(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
CircularProgressIndicator(),
|
|
SizedBox(height: 12),
|
|
Text(
|
|
l.exportingPleaseWait,
|
|
style: const TextStyle(color: Colors.white),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|