PDF QRCode download + code clean

This commit is contained in:
Fransolet Thomas 2022-06-23 17:48:02 +02:00
parent 11766b2fac
commit 98e066c712
15 changed files with 201 additions and 17 deletions

Binary file not shown.

View File

@ -0,0 +1,73 @@
import 'package:flutter/services.dart';
import 'package:managerapi/api.dart';
import 'dart:convert';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'dart:html' as html;
class PDFHelper {
static downloadPDF(List<SectionDTO> sections) async {
var sectionsArticle = sections.where((section) => section.type == SectionType.article);
if(sectionsArticle.length > 0) {
final font = await rootBundle.load("fonts/OpenSans-Medium.ttf");
final ttf = pw.Font.ttf(font);
final pdf = pw.Document();
pdf.addPage(pw.MultiPage(
pageFormat: PdfPageFormat.a4,
build: (pw.Context context) {
return <pw.Widget> [
for(var section in sectionsArticle)
pw.Center(
child:
pw.Column(
children: [
pw.Text(section.label, style: pw.TextStyle(font: ttf, fontSize: 30)),
pw.Padding(padding: const pw.EdgeInsets.only(top: 20)),
pw.BarcodeWidget(
data: section.id,
width: 150,
height: 150,
barcode: pw.Barcode.qrCode(),
drawText: false,
),
pw.Padding(padding: const pw.EdgeInsets.only(bottom: 30)),
]
)
)
];
}));
/*pdf.addPage(pw.Page(
pageFormat: PdfPageFormat.a4,
build: (pw.Context context) {
return
pw.Center(
child: pw.Column(
children: [
pw.Text(section.label, style: pw.TextStyle(font: ttf, fontSize: 30)),
pw.Padding(padding: const pw.EdgeInsets.only(top: 10)),
pw.BarcodeWidget(
data: section.id,
width: 150,
height: 150,
barcode: pw.Barcode.qrCode(),
drawText: false,
),
],
)
);
}));*/
var test = await pdf.save();
final content = base64Encode(test);
final fileName = sectionsArticle.length > 1 ? 'QRCodes' : sectionsArticle.first.label;
final anchor = html.AnchorElement(
href: "data:application/octet-stream;charset=utf-16le;base64,$content")
..setAttribute("download", '$fileName.pdf')
..click();
}
}
}

View File

@ -99,7 +99,7 @@ class _ArticleConfigState extends State<ArticleConfig> {
isChecked: articleDTO.isContentTop,
onChanged: (value) {
setState(() {
print(value);
//print(value);
articleDTO.isContentTop = value;
widget.onChanged(jsonEncode(articleDTO).toString());
});

View File

@ -0,0 +1,47 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:manager_app/Helpers/PDFHelper.dart';
import 'package:manager_app/app_context.dart';
import 'package:manager_app/constants.dart';
import 'package:managerapi/api.dart';
import 'package:provider/provider.dart';
class DownloadPDF extends StatefulWidget {
final List<SectionDTO> sections;
const DownloadPDF({
Key key,
this.sections,
}) : super(key: key);
@override
_DownloadPDFState createState() => _DownloadPDFState();
}
class _DownloadPDFState extends State<DownloadPDF> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
final appContext = Provider.of<AppContext>(context);
Size size = MediaQuery.of(context).size;
return
InkWell(
onTap: () async {
try {
PDFHelper.downloadPDF(widget.sections);
} catch(e) {
print(e);
}
},
child: Padding(
padding: const EdgeInsets.only(left: 5.0),
child: Icon(Icons.cloud_download, color: kPrimaryColor)
),
);
}
}

View File

@ -89,9 +89,7 @@ class _QuizzConfigState extends State<QuizzConfig> {
if (result != null)
{
setState(() {
print("BAAAD");
quizzDTO.badLevel = result;
print(quizzDTO.badLevel);
widget.onChanged(jsonEncode(quizzDTO).toString());
});
}
@ -216,7 +214,6 @@ class _QuizzConfigState extends State<QuizzConfig> {
right: 10,
child: InkWell(
onTap: () async {
print("new question");
QuestionDTO result = await showNewOrUpdateQuestionQuizz(null, appContext, context, "Question");
if (result != null)
{

View File

@ -17,6 +17,7 @@ import 'package:provider/provider.dart';
import 'package:intl/intl.dart';
import 'SubSection/Article/article_config.dart';
import 'SubSection/Article/download_pdf.dart';
import 'SubSection/Map/map_config.dart';
import 'SubSection/Menu/menu_config.dart';
import 'SubSection/Quizz/quizz_config.dart';
@ -97,6 +98,8 @@ class _SectionDetailScreenState extends State<SectionDetailScreen> {
),
),
Text(sectionDTO != null ? sectionDTO.label : "", style: TextStyle(fontSize: 30, fontWeight: FontWeight.w400)),
if(sectionDTO.type == SectionType.article)
DownloadPDF(sections: [sectionDTO]),
],
),
Padding(
@ -260,7 +263,6 @@ class _SectionDetailScreenState extends State<SectionDetailScreen> {
textColor: Colors.white,
fontSize: 15,
press: () {
print(sectionDTO);
delete(sectionDTO, appContext);
},
),
@ -351,7 +353,7 @@ class _SectionDetailScreenState extends State<SectionDetailScreen> {
initialValue: sectionDTO.data,
onChanged: (String data) {
print("Received info in parent");
print(data);
//print(data);
sectionDTO.data = data;
},
);
@ -360,17 +362,16 @@ class _SectionDetailScreenState extends State<SectionDetailScreen> {
initialValue: sectionDTO.data,
onChanged: (String data) {
print("Received info in parent - quizz");
print(data);
//print(data);
sectionDTO.data = data;
},
);
case SectionType.article:
print(sectionDTO.data);
return ArticleConfig(
initialValue: sectionDTO.data,
onChanged: (String data) {
print("Received info in parent - article");
print(data);
//print(data);
sectionDTO.data = data;
save(false, sectionDTO, appContext);
},
@ -381,6 +382,6 @@ class _SectionDetailScreenState extends State<SectionDetailScreen> {
Future<SectionDTO> getSection(String sectionId, Client client) async {
SectionDTO section = await client.sectionApi.sectionGetDetail(sectionId);
print(section);
//print(section);
return section;
}

View File

@ -14,6 +14,7 @@ import 'package:manager_app/Components/multi_select_container.dart';
import 'package:manager_app/Components/rounded_button.dart';
import 'package:manager_app/Components/string_input_container.dart';
import 'package:manager_app/Helpers/FileHelper.dart';
import 'package:manager_app/Helpers/PDFHelper.dart';
import 'package:manager_app/Models/managerContext.dart';
import 'package:manager_app/Screens/Configurations/section_reorderList.dart';
import 'package:manager_app/app_context.dart';
@ -223,6 +224,17 @@ class _ConfigurationDetailScreenState extends State<ConfigurationDetailScreen> {
configurationDTO.isMobile = value;
},
),
if(configurationDTO.isMobile)
RoundedButton(
text: "Télécharger les QRCodes",
icon: Icons.download_outlined,
color: Colors.grey,
textColor: Colors.white,
fontSize: 15,
press: () {
PDFHelper.downloadPDF(sections);
},
)
],
)
],

View File

@ -137,7 +137,7 @@ Future<List<ConfigurationDTO>> getConfigurations(dynamic appContext) async {
List<ConfigurationDTO> configurations = await appContext.getContext().clientAPI.configurationApi.configurationGet();
print("number of configurations " + configurations.length.toString());
configurations.forEach((element) {
print(element);
//print(element);
});
return configurations;
}

View File

@ -226,7 +226,7 @@ Future<List<DeviceDTO>> getDevices(dynamic appContext) async {
List<DeviceDTO> devices = await appContext.getContext().clientAPI.deviceApi.deviceGet();
print("number of devices " + devices.length.toString());
devices.forEach((element) {
print(element);
//print(element);
});
return devices;
}

View File

@ -32,7 +32,7 @@ class _BodyState extends State<Body> {
MenuSection configurations = new MenuSection(name: "Configurations", type: "configurations", order: 1);
MenuSection resources = new MenuSection(name: "Ressources", type: "resources", order: 2);
Menu menu = new Menu(title: "Manager");
Menu menu = new Menu(title: "MyMuseum");
@override
Widget build(BuildContext context) {

View File

@ -34,7 +34,7 @@ class _ResourceBodyGridState extends State<ResourceBodyGrid> {
@override
void initState() {
resourcesToShow = widget.resources;
filterType = [resource_types[0], resource_types[1], resource_types[2]]; // resource_types[3]
filterType = [resource_types[0], resource_types[1]];//, resource_types[2]]; // resource_types[3]
selectedTypes = resource_types;
super.initState();
}

View File

@ -16,7 +16,7 @@ const kSuccess = Color(0xFF8bc34a);
const List<String> section_types = ["Map", "Slider", "Video", "Web", "Menu", "Quizz", "Article"];
const List<String> map_types = ["none", "normal", "satellite", "terrain", "hybrid"];
const List<String> languages = ["FR", "NL", "EN", "DE"];
const List<String> resource_types = ["image", "image url", "video url"]; // "video",
const List<String> resource_types = ["image", "image url"]; // "video url" , "video",
/*
const kTextStyle = TextStyle(

View File

@ -49,7 +49,7 @@ class _MyAppState extends State<MyApp> {
child: MaterialApp(
scrollBehavior: MyCustomScrollBehavior(),
debugShowCheckedModeBanner: false,
title: 'Manager App',
title: 'MyMuseum App',
initialRoute: widget.initialRoute,
/*supportedLocales: [
const Locale('en', 'US'),

View File

@ -1,6 +1,13 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
archive:
dependency: transitive
description:
name: archive
url: "https://pub.dartlang.org"
source: hosted
version: "3.3.0"
args:
dependency: transitive
description:
@ -29,6 +36,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
barcode:
dependency: transitive
description:
name: barcode
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
boolean_selector:
dependency: transitive
description:
@ -184,6 +198,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.4"
image:
dependency: transitive
description:
name: image
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.3"
intl:
dependency: transitive
description:
@ -261,6 +282,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
path_parsing:
dependency: transitive
description:
name: path_parsing
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.1"
pdf:
dependency: "direct main"
description:
name: pdf
url: "https://pub.dartlang.org"
source: hosted
version: "3.6.4"
pedantic:
dependency: transitive
description:
@ -268,6 +303,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.11.1"
petitparser:
dependency: transitive
description:
name: petitparser
url: "https://pub.dartlang.org"
source: hosted
version: "4.4.0"
plugin_platform_interface:
dependency: transitive
description:
@ -413,6 +455,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.2"
xml:
dependency: transitive
description:
name: xml
url: "https://pub.dartlang.org"
source: hosted
version: "5.3.1"
sdks:
dart: ">=2.15.0 <3.0.0"
flutter: ">=2.10.0"

View File

@ -41,7 +41,7 @@ dependencies:
#path_provider: ^2.0.2
encrypt: ^5.0.0
qr_flutter: ^4.0.0
pdf: ^3.8.1
pdf: ^3.6.0
#msix: ^2.1.3
#window_size:
# git:
@ -92,6 +92,11 @@ msix_config:
architecture: x64
capabilities: 'internetClient'
fonts:
- family: OpenSans
fonts:
- asset: fonts/OpenSans-Medium.ttf
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.