mirror of
https://bitbucket.org/FransoletThomas/tablet-app.git
synced 2025-12-06 16:41:19 +00:00
185 lines
6.5 KiB
Dart
185 lines
6.5 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:provider/provider.dart';
|
|
import 'package:tablet_app/Components/loading_common.dart';
|
|
import 'package:tablet_app/Models/tabletContext.dart';
|
|
import 'package:tablet_app/app_context.dart';
|
|
import 'package:tablet_app/constants.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': 1});
|
|
|
|
@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(TabletAppContext tabletAppContext, PdfDTO pdfDTO) async {
|
|
Completer<File> completer = Completer();
|
|
|
|
var file = await _checkIfLocalResourceExists(tabletAppContext, pdfDTO.resourceId!);
|
|
|
|
if(file == null) {
|
|
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 = pdfDTO.resourceUrl!;
|
|
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!');
|
|
}
|
|
} else {
|
|
print("FOUND FILE PDF");
|
|
completer.complete(file);
|
|
}
|
|
|
|
return completer.future;
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
//_webView = null;
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final appContext = Provider.of<AppContext>(context);
|
|
TabletAppContext tabletAppContext = appContext.getContext();
|
|
|
|
return pdfDTO.resourceUrl != null && pdfDTO.resourceUrl!.length > 0 ?
|
|
Padding(
|
|
padding: const EdgeInsets.all(12.5),
|
|
child: FutureBuilder(
|
|
future: createFileOfPdfUrl(tabletAppContext, pdfDTO),
|
|
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", style: TextStyle(fontSize: kNoneInfoOrIncorrect)));
|
|
}
|
|
} //_webView
|
|
|
|
Future<File?> _checkIfLocalResourceExists(TabletAppContext tabletAppContext, String resourceId) async {
|
|
try {
|
|
Directory? appDocumentsDirectory = Platform.isIOS ? await getApplicationDocumentsDirectory() : await getDownloadsDirectory();
|
|
String localPath = appDocumentsDirectory!.path;
|
|
Directory configurationDirectory = Directory('$localPath/${tabletAppContext.configuration!.id}');
|
|
List<FileSystemEntity> fileList = configurationDirectory.listSync();
|
|
|
|
if(fileList.any((fileL) => fileL.uri.pathSegments.last.contains(resourceId))) {
|
|
File file = File(fileList.firstWhere((fileL) => fileL.uri.pathSegments.last.contains(resourceId)).path);
|
|
return file;
|
|
}
|
|
} catch (e) {
|
|
print("ERROR _checkIfLocalResourceExists PDF");
|
|
print(e);
|
|
}
|
|
|
|
return null;
|
|
} |