mirror of
https://bitbucket.org/FransoletThomas/tablet-app.git
synced 2025-12-06 16:41:19 +00:00
125 lines
3.7 KiB
Dart
125 lines
3.7 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
//import 'dart:html';
|
|
import 'dart:ui' as ui;
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_pdfview/flutter_pdfview.dart';
|
|
import 'package:manager_api/api.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:tablet_app/Components/loading_common.dart';
|
|
|
|
|
|
class PDFViewWidget extends StatefulWidget {
|
|
final SectionDTO? section;
|
|
PDFViewWidget({this.section});
|
|
|
|
@override
|
|
_PDFViewWidget createState() => _PDFViewWidget();
|
|
}
|
|
|
|
class _PDFViewWidget extends State<PDFViewWidget> {
|
|
PdfDTO pdfDTO = PdfDTO();
|
|
String remotePDFpath = "";
|
|
final Completer<PDFViewController> _controller = Completer<PDFViewController>();
|
|
int? pages = 0;
|
|
int? currentPage = 0;
|
|
bool isReady = false;
|
|
String errorMessage = '';
|
|
|
|
@override
|
|
void initState() {
|
|
print(widget.section!.data);
|
|
pdfDTO = PdfDTO.fromJson(jsonDecode(widget.section!.data!))!;
|
|
pdfDTO.source_ = "todo";
|
|
print(pdfDTO);
|
|
super.initState();
|
|
|
|
/*createFileOfPdfUrl(pdfDTO.source_!).then((f) {
|
|
setState(() {
|
|
remotePDFpath = f.path;
|
|
print("paaath");
|
|
print(remotePDFpath);
|
|
});
|
|
});*/
|
|
}
|
|
|
|
Future<File> createFileOfPdfUrl(String source_) async {
|
|
Completer<File> completer = Completer();
|
|
print("Start download file from internet!");
|
|
try {
|
|
// "https://berlin2017.droidcon.cod.newthinking.net/sites/global.droidcon.cod.newthinking.net/files/media/documents/Flutter%20-%2060FPS%20UI%20of%20the%20future%20%20-%20DroidconDE%2017.pdf";
|
|
// final url = "https://pdfkit.org/docs/guide.pdf";
|
|
final url = "http://www.pdf995.com/samples/pdf.pdf";
|
|
// TODO replace by source_
|
|
final filename = url.substring(url.lastIndexOf("/") + 1);
|
|
var request = await HttpClient().getUrl(Uri.parse(url));
|
|
var response = await request.close();
|
|
var bytes = await consolidateHttpClientResponseBytes(response);
|
|
var dir = await getApplicationDocumentsDirectory();
|
|
print("Download files");
|
|
print("${dir.path}/$filename");
|
|
File file = File("${dir.path}/$filename");
|
|
|
|
await file.writeAsBytes(bytes, flush: true);
|
|
completer.complete(file);
|
|
} catch (e) {
|
|
throw Exception('Error parsing asset file!');
|
|
}
|
|
|
|
return completer.future;
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
//_webView = null;
|
|
super.dispose();
|
|
}
|
|
|
|
|
|
@override
|
|
Widget build(BuildContext context) => pdfDTO.source_ != null && pdfDTO.source_!.length > 0 ?
|
|
FutureBuilder(
|
|
future: createFileOfPdfUrl(""),
|
|
builder: (context, AsyncSnapshot<dynamic> snapshot) {
|
|
print("snapshot.data");
|
|
print(snapshot.data);
|
|
if (snapshot.connectionState == ConnectionState.done) {
|
|
return PDFView(
|
|
filePath: snapshot.data.path,
|
|
enableSwipe: true,
|
|
swipeHorizontal: true,
|
|
autoSpacing: true,
|
|
pageFling: true,
|
|
onRender: (_pages) {
|
|
//setState(() {
|
|
pages = _pages;
|
|
isReady = true;
|
|
//});
|
|
},
|
|
onError: (error) {
|
|
print(error.toString());
|
|
},
|
|
onPageError: (page, error) {
|
|
print('$page: ${error.toString()}');
|
|
},
|
|
onViewCreated: (PDFViewController pdfViewController) {
|
|
//_controller.complete(pdfViewController);
|
|
},
|
|
onPageChanged: (int? page, int? total) {
|
|
print('page change: $page/$total');
|
|
},
|
|
);
|
|
} else {
|
|
return Center(
|
|
child: Container(
|
|
child: LoadingCommon()
|
|
)
|
|
);
|
|
}
|
|
}
|
|
) :
|
|
Center(child: Text("Le PDF ne peut pas être affiché, il n'existe pas"));
|
|
} //_webView |