60 lines
1.5 KiB
Dart
60 lines
1.5 KiB
Dart
|
|
import 'package:flutter/material.dart';
|
||
|
|
import 'package:pdfx/pdfx.dart';
|
||
|
|
import 'package:internet_file/internet_file.dart'; // flutter pub add internet_file
|
||
|
|
|
||
|
|
class PdfViewerWidget extends StatefulWidget {
|
||
|
|
final String url;
|
||
|
|
|
||
|
|
const PdfViewerWidget({super.key, required this.url});
|
||
|
|
|
||
|
|
@override
|
||
|
|
State<PdfViewerWidget> createState() => _PdfViewerWidgetState();
|
||
|
|
}
|
||
|
|
|
||
|
|
class _PdfViewerWidgetState extends State<PdfViewerWidget> {
|
||
|
|
late PdfControllerPinch _pdfController;
|
||
|
|
bool _isLoading = true;
|
||
|
|
|
||
|
|
@override
|
||
|
|
void initState() {
|
||
|
|
super.initState();
|
||
|
|
_initPdf();
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<void> _initPdf() async {
|
||
|
|
// Scarica il file in memoria in modo fluido
|
||
|
|
final pdfData = await InternetFile.get(widget.url);
|
||
|
|
_pdfController = PdfControllerPinch(
|
||
|
|
document: PdfDocument.openData(pdfData),
|
||
|
|
);
|
||
|
|
if (mounted) setState(() => _isLoading = false);
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
void dispose() {
|
||
|
|
_pdfController.dispose();
|
||
|
|
super.dispose();
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
Widget build(BuildContext context) {
|
||
|
|
if (_isLoading) {
|
||
|
|
return const Center(child: CircularProgressIndicator());
|
||
|
|
}
|
||
|
|
return Scaffold(
|
||
|
|
// Usiamo Scaffold dentro il Dialog per avere l'AppBar e poter chiudere
|
||
|
|
appBar: AppBar(
|
||
|
|
title: const Text("Visualizzatore PDF"),
|
||
|
|
leading: IconButton(
|
||
|
|
icon: const Icon(Icons.close),
|
||
|
|
onPressed: () => Navigator.pop(context),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
body: PdfViewPinch(
|
||
|
|
controller: _pdfController,
|
||
|
|
// pdfx gestisce nativamente il pinch to zoom!
|
||
|
|
),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|