116 lines
3.3 KiB
Dart
116 lines
3.3 KiB
Dart
import 'dart:io' as io;
|
|
|
|
//import 'package:advance_pdf_viewer/advance_pdf_viewer.dart';
|
|
import 'package:gepam/funcoes_gerais.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:share/share.dart';
|
|
|
|
class PDFViewerScreenController extends GetxController {
|
|
var loading = true.obs;
|
|
var loadPDF = false.obs;
|
|
var visibleRefresh = false.obs;
|
|
var error = false.obs;
|
|
//PDFDocument doc = PDFDocument();
|
|
io.File? filePDF;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
this._loadPDF();
|
|
}
|
|
|
|
Future<void> _loadPDF() async {
|
|
var file = await this._createFileOfPdfUrl();
|
|
// if(file != null && await file.exists()){
|
|
// //io.File fpdf = new io.File('${file.path}');
|
|
// this.doc = await PDFDocument.fromFile(file);
|
|
// //this.doc = await PDFDocument.fromFile(file);
|
|
// //this.visibleRefresh.value = true;
|
|
// }
|
|
// else{
|
|
// if(await FuncoesGerais.checkInternetConnection() == false){
|
|
// this.error.value = true;
|
|
// }
|
|
// else{
|
|
// this.doc = await PDFDocument.fromURL(FuncoesGerais.urlPDF);
|
|
// }
|
|
|
|
// }
|
|
|
|
if (file == null || await file.exists() == false) {
|
|
this.error.value = true;
|
|
}
|
|
|
|
this.loading.value = false;
|
|
}
|
|
|
|
Future<void> refreshPDF() async {
|
|
this.loading.value = true;
|
|
try {
|
|
await this.filePDF!.delete();
|
|
this._loadPDF();
|
|
} catch (e) {
|
|
Get.rawSnackbar(message: 'Não possível atualizar o PDF. Tente novamente');
|
|
this.loading.value = false;
|
|
}
|
|
}
|
|
|
|
Future<void> sharePDF() async {
|
|
//Share.shareFiles(['${directory.path}/image.jpg'], text: 'Great picture');
|
|
this.loadPDF.value = true;
|
|
var file = await this._createFileOfPdfUrl();
|
|
try {
|
|
Share.shareFiles([file!.path], text: 'Gepam card')
|
|
.then((value) => this.loadPDF.value = false);
|
|
} catch (e) {
|
|
this.loadPDF.value = false;
|
|
Get.rawSnackbar(message: 'Não foi possível compartilhar o PDF');
|
|
}
|
|
}
|
|
|
|
Future<io.File?> _createFileOfPdfUrl() async {
|
|
final url = FuncoesGerais.urlPDF;
|
|
io.File? file;
|
|
//f.File file2;
|
|
final filename = url.substring(url.lastIndexOf("/") + 1);
|
|
String dir = (await getApplicationDocumentsDirectory()).path;
|
|
file = io.File('$dir/$filename');
|
|
if (!await file.exists()) {
|
|
if (await FuncoesGerais.checkInternetConnection()) {
|
|
var request = await io.HttpClient().getUrl(Uri.parse(url));
|
|
var response = await request.close();
|
|
var bytes = await consolidateHttpClientResponseBytes(response);
|
|
await file.writeAsBytes(bytes);
|
|
} else {
|
|
file = null;
|
|
}
|
|
}
|
|
this.filePDF = file;
|
|
if (this.filePDF != null)
|
|
this.visibleRefresh.value = await this.filePDF!.exists();
|
|
|
|
return file;
|
|
}
|
|
|
|
Widget buildError() {
|
|
return Container(
|
|
width: double.maxFinite,
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: [
|
|
Text('Não foi possível carregar o PDF'),
|
|
SizedBox(
|
|
height: 10.0,
|
|
),
|
|
ElevatedButton(onPressed: this.refreshPDF, child: Text('Atualizar'))
|
|
//RaisedButton(onPressed: this.refreshPDF,child: Text('Atualizar'),)
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|