349 lines
15 KiB
Dart
349 lines
15 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:flutter_widget_from_html/flutter_widget_from_html.dart';
|
|
import 'package:manager_api_new/api.dart';
|
|
import 'package:mymuseum_visitapp/Components/loading_common.dart';
|
|
import 'package:mymuseum_visitapp/Helpers/translationHelper.dart';
|
|
import 'package:mymuseum_visitapp/Models/visitContext.dart';
|
|
import 'package:mymuseum_visitapp/Screens/Sections/PDF/pdf_filter.dart';
|
|
import 'package:mymuseum_visitapp/app_context.dart';
|
|
import 'package:mymuseum_visitapp/constants.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
|
|
class PDFPage extends StatefulWidget {
|
|
final PdfDTO section;
|
|
const PDFPage({super.key, required this.section});
|
|
|
|
@override
|
|
_PDFPage createState() => _PDFPage();
|
|
}
|
|
|
|
class _PDFPage extends State<PDFPage> {
|
|
PdfDTO pdfDTO = PdfDTO();
|
|
String remotePDFpath = "";
|
|
final Completer<PDFViewController> _controller = Completer<PDFViewController>();
|
|
int? pages = 0;
|
|
int? currentPage = 0;
|
|
bool isReady = false;
|
|
String errorMessage = '';
|
|
late ValueNotifier<OrderedTranslationAndResourceDTO?> selectedPdf = ValueNotifier<OrderedTranslationAndResourceDTO?>(pdfDTO.pdfs!.first);
|
|
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);*/
|
|
pdfDTO = widget.section;
|
|
pdfDTO.pdfs!.sort((a, b) => a.order!.compareTo(b.order!));
|
|
|
|
super.initState();
|
|
|
|
/*createFileOfPdfUrl(pdfDTO.source_!).then((f) {
|
|
setState(() {
|
|
remotePDFpath = f.path;
|
|
print("paaath");
|
|
print(remotePDFpath);
|
|
});
|
|
});*/
|
|
}
|
|
|
|
Future<File?> createFileOfPdfUrl(VisitAppContext visitAppContext, OrderedTranslationAndResourceDTO pdfFileDTO) async {
|
|
Completer<File?> completer = Completer();
|
|
|
|
if(pdfFileDTO.translationAndResourceDTOs!.firstWhere((pfat) => pfat.language == visitAppContext.language).resourceId == null) {
|
|
completer.complete(null);
|
|
} else {
|
|
var file = await _checkIfLocalResourceExists(visitAppContext, pdfFileDTO.translationAndResourceDTOs!.firstWhere((pfat) => pfat.language == visitAppContext.language).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 = pdfFileDTO.translationAndResourceDTOs!.firstWhere((pfat) => pfat.language == visitAppContext.language).resource!.url!;
|
|
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);
|
|
VisitAppContext visitAppContext = appContext.getContext();
|
|
|
|
var primaryColor = visitAppContext.configuration != null ? visitAppContext.configuration!.primaryColor != null ? new Color(int.parse(visitAppContext.configuration!.primaryColor!.split('(0x')[1].split(')')[0], radix: 16)) : kSecondColor : kSecondColor;
|
|
|
|
pdfDTO.pdfs!.sort((a, b) => a.order!.compareTo(b.order!));
|
|
Size size = MediaQuery.of(context).size;
|
|
|
|
var title = TranslationHelper.get(widget.section.title, appContext.getContext());
|
|
String cleanedTitle = title.replaceAll('\n', ' ').replaceAll('<br>', ' ');
|
|
|
|
return Stack(
|
|
children: [
|
|
Container(
|
|
height: size.height * 0.28,
|
|
decoration: BoxDecoration(
|
|
boxShadow: const [
|
|
BoxShadow(
|
|
color: kMainGrey,
|
|
spreadRadius: 0.5,
|
|
blurRadius: 5,
|
|
offset: Offset(0, 1), // changes position of shadow
|
|
),
|
|
],
|
|
gradient: const LinearGradient(
|
|
begin: Alignment.centerRight,
|
|
end: Alignment.centerLeft,
|
|
colors: [
|
|
/*Color(0xFFDD79C2),
|
|
Color(0xFFB65FBE),
|
|
Color(0xFF9146BA),
|
|
Color(0xFF7633B8),
|
|
Color(0xFF6528B6),
|
|
Color(0xFF6025B6)*/
|
|
kMainColor0, //Color(0xFFf6b3c4)
|
|
kMainColor1,
|
|
kMainColor2,
|
|
|
|
],
|
|
),
|
|
image: widget.section.imageSource != null ? DecorationImage(
|
|
fit: BoxFit.cover,
|
|
opacity: 0.65,
|
|
image: NetworkImage(
|
|
widget.section.imageSource!,
|
|
),
|
|
): null,
|
|
),
|
|
),
|
|
Column(
|
|
children: <Widget>[
|
|
SizedBox(
|
|
height: size.height * 0.11,
|
|
width: size.width,
|
|
child: Stack(
|
|
fit: StackFit.expand,
|
|
children: [
|
|
Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.only(top: 22.0),
|
|
child: SizedBox(
|
|
width: size.width *0.7,
|
|
child: HtmlWidget(
|
|
cleanedTitle,
|
|
textStyle: const TextStyle(color: Colors.white, fontFamily: 'Roboto', fontSize: 20),
|
|
customStylesBuilder: (element)
|
|
{
|
|
return {'text-align': 'center', 'font-family': "Roboto", '-webkit-line-clamp': "2"};
|
|
},
|
|
),
|
|
),
|
|
),
|
|
),
|
|
Positioned(
|
|
top: 35,
|
|
left: 10,
|
|
child: SizedBox(
|
|
width: 50,
|
|
height: 50,
|
|
child: InkWell(
|
|
onTap: () {
|
|
Navigator.of(context).pop();
|
|
},
|
|
child: Container(
|
|
decoration: const BoxDecoration(
|
|
color: kMainColor,
|
|
shape: BoxShape.circle,
|
|
),
|
|
child: const Icon(Icons.arrow_back, size: 23, color: Colors.white)
|
|
),
|
|
)
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
Expanded(child: Container(
|
|
margin: const EdgeInsets.only(top: 0),
|
|
decoration: const BoxDecoration(
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: kMainGrey,
|
|
spreadRadius: 0.5,
|
|
blurRadius: 2,
|
|
offset: Offset(0, 1), // changes position of shadow
|
|
),
|
|
],
|
|
color: kBackgroundColor,
|
|
borderRadius: BorderRadius.only(
|
|
topLeft: Radius.circular(30),
|
|
topRight: Radius.circular(30),
|
|
),
|
|
),
|
|
child: ClipRRect(
|
|
borderRadius: const BorderRadius.only(
|
|
topLeft: Radius.circular(30),
|
|
topRight: Radius.circular(30),
|
|
),
|
|
child: pdfDTO.pdfs != null && pdfDTO.pdfs!.isNotEmpty ?
|
|
Center(
|
|
child: Stack(
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: ValueListenableBuilder<OrderedTranslationAndResourceDTO?>(
|
|
valueListenable: selectedPdf,
|
|
builder: (context, value, _) {
|
|
return FutureBuilder(
|
|
future: createFileOfPdfUrl(visitAppContext, value!),
|
|
builder: (context, AsyncSnapshot<dynamic> snapshot) {
|
|
print("snapshot.data");
|
|
print(snapshot.data);
|
|
if (snapshot.connectionState == ConnectionState.done) {
|
|
if(snapshot.data == null) {
|
|
return Center(child: Text("Aucun fichier à afficher"));
|
|
} else {
|
|
return Stack(
|
|
children: [
|
|
PDFView(
|
|
filePath: snapshot.data.path,
|
|
enableSwipe: true,
|
|
fitEachPage: true,
|
|
swipeHorizontal: true,
|
|
autoSpacing: false,
|
|
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,
|
|
left: 20,
|
|
child: ValueListenableBuilder<Map<String, int>>(
|
|
valueListenable: currentState,
|
|
builder: (context, value, _) {
|
|
return Container(
|
|
decoration: BoxDecoration(
|
|
borderRadius: BorderRadius.all(Radius.circular(visitAppContext.configuration!.roundedValue?.toDouble() ?? 20.0)),
|
|
color: primaryColor
|
|
),
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(6.0),
|
|
child: Text("${value["page"]!+1}/${value["total"]!}",
|
|
style: const TextStyle(color: Colors.white, fontSize: 20)),
|
|
)
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
} else {
|
|
return Center(
|
|
child: Container(
|
|
child: LoadingCommon()
|
|
)
|
|
);
|
|
}
|
|
}
|
|
);
|
|
}
|
|
),
|
|
),
|
|
pdfDTO.pdfs!.length > 1 ? Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: PdfFilter(
|
|
pdfsList: pdfDTO.pdfs!,
|
|
onPDFSelected: (selectedOrder) {
|
|
selectedPdf.value = pdfDTO.pdfs!.firstWhere((pdf) => pdf.order == selectedOrder);
|
|
}),
|
|
): const SizedBox(),
|
|
],
|
|
),
|
|
) :
|
|
Center(child: Text("Aucun pdf à afficher", style: TextStyle(fontSize: kNoneInfoOrIncorrect))))
|
|
)
|
|
)
|
|
],
|
|
)
|
|
],
|
|
);
|
|
|
|
/**/
|
|
}
|
|
} //_webView
|
|
|
|
Future<File?> _checkIfLocalResourceExists(VisitAppContext visitAppContext, String resourceId) async {
|
|
try {
|
|
Directory? appDocumentsDirectory = Platform.isIOS ? await getApplicationDocumentsDirectory() : await getDownloadsDirectory();
|
|
String localPath = appDocumentsDirectory!.path;
|
|
Directory configurationDirectory = Directory('$localPath/${visitAppContext.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;
|
|
} |