mirror of
https://bitbucket.org/FransoletThomas/tablet-app.git
synced 2025-12-06 16:41:19 +00:00
147 lines
4.6 KiB
Dart
147 lines
4.6 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 = '';
|
|
ValueNotifier<Map<String, int>> currentState = ValueNotifier<Map<String, int>>({'page': 0, 'total': 0});
|
|
|
|
@override
|
|
void initState() {
|
|
print(widget.section!.data);
|
|
pdfDTO = PdfDTO.fromJson(jsonDecode(widget.section!.data!))!;
|
|
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 = 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.resourceUrl != null && pdfDTO.resourceUrl!.length > 0 ?
|
|
FutureBuilder(
|
|
future: createFileOfPdfUrl(pdfDTO.resourceUrl!),
|
|
builder: (context, AsyncSnapshot<dynamic> snapshot) {
|
|
print("snapshot.data");
|
|
print(snapshot.data);
|
|
if (snapshot.connectionState == ConnectionState.done) {
|
|
return Stack(
|
|
children: [
|
|
PDFView(
|
|
filePath: snapshot.data.path,
|
|
enableSwipe: true,
|
|
swipeHorizontal: true,
|
|
autoSpacing: true,
|
|
pageFling: true,
|
|
fitPolicy: FitPolicy.HEIGHT,
|
|
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) {
|
|
currentPage = page;
|
|
pages = total;
|
|
currentState.value = {'page': page!, 'total': total!};
|
|
|
|
print('page change: $page/$total');
|
|
},
|
|
),
|
|
Positioned(
|
|
bottom: 20,
|
|
right: 20,
|
|
child: ValueListenableBuilder<Map<String, int>>(
|
|
valueListenable: currentState,
|
|
builder: (context, value, _) {
|
|
return Container(
|
|
//color: Colors.blueAccent,
|
|
child: Text("${value["page"]!+1}/${value["total"]!}",
|
|
style: TextStyle(color: Colors.black, fontSize: 30))
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
);
|
|
} else {
|
|
return Center(
|
|
child: Container(
|
|
child: LoadingCommon()
|
|
)
|
|
);
|
|
}
|
|
}
|
|
) :
|
|
Center(child: Text("Le PDF ne peut pas être affiché, il n'existe pas"));
|
|
} //_webView |