getImageBlock method
Future<Widget?>
getImageBlock(
- String line, [
- Alignment? alignment
])
override
Implementation
@override
Future<pw.Widget?> getImageBlock(String line, [pw.Alignment? alignment]) async {
final RegExpMatch? match = Constant.IMAGE_PATTERN.firstMatch(line);
double? width = null;
double? height = null;
if (match != null) {
final String objectFit = match.group(2)!;
if (match.group(6) != null) width = double.tryParse(match.group(7)!);
if (match.group(8) != null) width = double.tryParse(match.group(9)!);
final String path = match.group(10)!;
late final File? file;
if (Constant.IMAGE_FROM_NETWORK_URL.hasMatch(path)) {
final String url = path;
final String pathStorage = '${(await getApplicationCacheDirectory()).path}/image (${Random.secure().nextInt(99999) + 50})';
try {
file = File(pathStorage);
await Dio().download(url, pathStorage);
} on DioException catch (e) {
final Map<String, dynamic> mapError = <String, dynamic>{
'error': e.error,
'message': e.message,
'request_options': e.requestOptions,
'response': e.response,
'stacktrace': e.stackTrace,
'type': e.type.name,
};
debugPrint('${e.message}\n\n${jsonEncode(mapError)}');
return null;
}
}
file = File(path);
if (!(await file.exists())) {
//if not exist the image will create a warning
return null;
}
//calculate exceded height using page format params
if (height != null && height >= pageHeight) height = pageHeight;
//calculate exceded width using page format params
if (width != null && width >= pageWidth) width = pageWidth;
//calculating object fit type instead use object fit literal enum (it causes out of memory errors)
if (objectFit.equals('cover') || objectFit.equals('fill')) {
return pw.Wrap(
children: <pw.Widget>[
pw.Container(
alignment: alignment,
child: pw.Image(
pw.MemoryImage((await file.readAsBytes())),
dpi: 230,
fit: pw.BoxFit.fitWidth,
height: pageHeight,
width: pageWidth,
),
),
],
);
}
if (objectFit.equals('fill-all')) {
return pw.RichText(
softWrap: true,
overflow: pw.TextOverflow.span,
text: pw.WidgetSpan(
child: pw.Container(
alignment: alignment,
child: pw.Image(
pw.MemoryImage((await file.readAsBytes())),
dpi: 230,
fit: pw.BoxFit.contain,
height: pageWidth,
width: pageWidth,
),
),
),
);
}
if (objectFit.equals('fitWidth')) {
return pw.RichText(
softWrap: true,
overflow: pw.TextOverflow.span,
text: pw.WidgetSpan(
child: pw.Container(
alignment: alignment,
child: pw.Image(
pw.MemoryImage((await file.readAsBytes())),
dpi: 230,
fit: pw.BoxFit.fitWidth,
height: height,
),
),
),
);
}
if (objectFit.equals('fitHeight')) {
return pw.RichText(
softWrap: true,
overflow: pw.TextOverflow.span,
text: pw.WidgetSpan(
child: pw.Container(
alignment: alignment,
child: pw.Image(
pw.MemoryImage((await file.readAsBytes())),
dpi: 230,
fit: pw.BoxFit.fitHeight,
width: width,
),
),
),
);
}
return pw.RichText(
softWrap: true,
overflow: pw.TextOverflow.span,
text: pw.WidgetSpan(
child: pw.Container(
alignment: alignment,
child: pw.Image(
pw.MemoryImage((await file.readAsBytes())),
dpi: 230,
fit: objectFit.resolvePdfFit,
height: height,
width: width,
),
),
),
);
}
return null;
}