Skip to content

Instantly share code, notes, and snippets.

@riccardopirani
Created July 13, 2020 06:55
Show Gist options
  • Save riccardopirani/0691bce45b2afc6bd6366b50423236d0 to your computer and use it in GitHub Desktop.
Save riccardopirani/0691bce45b2afc6bd6366b50423236d0 to your computer and use it in GitHub Desktop.
flutter pdf viewer
import 'package:advance_pdf_viewer/advance_pdf_viewer.dart';
//Funzione che si occupa di visualizzare la stampa
Future<void> stampa(String path) async {
final pdf = await rootBundle.load(path);
await Printing.layoutPdf(onLayout: (_) => pdf.buffer.asUint8List());
}
//Funzione che che permette di tornare alla pagina di generazione del rapporto
void apriGenerazioneRapportino(BuildContext context, Rapporto rc) {
final gr = new RapportoPage(rc);
//Navigator.pop(context);
Navigator.push(context, MaterialPageRoute(builder: (context) => gr));
}
//Funzione che si occupa di inviare il documento
Future<void> invioDocumento(
String path, Rapporto rp, Utente u, BuildContext context) async {
//Dichiaro la variabile che contiene la pagina di home
final page = HomeViewScreen();
//Funzione che esegue la conversione di un filepdf in una stringa base64
String base64file = await Supporto.castDocumentToBase64(path);
//Funzione che si occupa dell'invio del rapportino
bool ris = await Rapporto.invioFile(base64file);
//Timer per ritardare invio rapportino
Timer(Duration(seconds: 1), () {
print("Timer di 1 secondo concluos");
});
//Se il rapportino è stato inviato correttamente viene visualizzato un messaggio di conferma
if (ris == true) {
Alert(
context: context,
type: AlertType.success,
title: "Rapportino Inviato",
desc: "Il rappportino è stato inviato con succeso al cliente",
buttons: [
DialogButton(
child: Text(
"Conferma",
style: TextStyle(color: Colors.white, fontSize: 20),
),
onPressed: () => {
Navigator.push(
context, MaterialPageRoute(builder: (context) => page)),
})
],
).show();
}
//Se il rapportino non viene inviato correttamente viene visualizzato un messaggio di errore
else {
Alert(
context: context,
title: "Errore",
desc: "Invio rapportino non riuscito")
.show();
}
}
//Classe che si occupa di mostrare il PDF a video
class PdfPreviewScreen extends StatefulWidget {
//Path del pdf
PdfPreviewScreen({this.file, this.path, this.rp, this.u});
PDFDocument file;
final String path;
final Rapporto rp;
final Utente u;
@override
_PdfPreviewScreenState createState() => _PdfPreviewScreenState();
}
class _PdfPreviewScreenState extends State<PdfPreviewScreen> {
bool _isLoading = true;
bool myInterceptor(bool stopDefaultButtonEvent) {
print("Bottone back disabilitato!"); // Do some stuff.
return true;
}
@override
void initState() {
BackButtonInterceptor.add(myInterceptor);
loadDocument();
}
loadDocument() async {
widget.file = await PDFDocument.fromAsset(widget.path);
setState(() => _isLoading = false);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Rapportino'),
leading: new IconButton(
icon: new Icon(Icons.arrow_back),
onPressed: () => apriGenerazioneRapportino(context, widget.rp),
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.send),
onPressed: () =>
{invioDocumento(widget.path, widget.rp, widget.u, context)},
),
IconButton(
icon: Icon(Icons.print),
onPressed: () => {stampa(widget.path)},
)
],
),
body: Center(
child: _isLoading
? Center(child: CircularProgressIndicator())
: PDFViewer(
document: widget.file,
zoomSteps: 1,
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment