implemented making a pdf from DetailPage.dart and croping image to 1:1 format

This commit is contained in:
2023-05-11 16:57:17 +02:00
parent 7c48542920
commit 423aae59b1
4 changed files with 166 additions and 3 deletions
+26 -1
View File
@@ -1,8 +1,10 @@
import 'dart:io';
import 'dart:math';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:image/image.dart' as img;
class AddScreen extends StatefulWidget {
@@ -35,6 +37,27 @@ class _AddScreenState extends State<AddScreen> {
return fileSize <= maxSizeInBytes;
}
Future<File> _cropImage(File imageFile) async {
final originalImage = img.decodeImage(await imageFile.readAsBytes());
if (originalImage != null) {
int originSize = min(originalImage.width, originalImage.height);
final croppedImage = img.copyCrop(
originalImage,
x: (originalImage.width - originSize)~/2,
y: (originalImage.height - originSize)~/2,
width: originSize,
height: originSize
);
final newPath = imageFile.path.substring(0, imageFile.path.lastIndexOf('/'));
final newFile = File('$newPath/cropped.jpg');
await newFile.writeAsBytes(img.encodePng(croppedImage));
return newFile;
} else {
throw Exception('Unable to decode image file.');
}
}
Future<void> _getImage(ImageSource source) async {
final pickedFile = await ImagePicker().pickImage(source: source);
@@ -42,8 +65,9 @@ class _AddScreenState extends State<AddScreen> {
final tempFile = File(pickedFile.path);
final bool isValid = await isFileSizeValid(tempFile, 10);
if (isValid) {
File croppedFile = await _cropImage(tempFile); // Bild zuschneiden
setState(() {
_imageFile = tempFile;
_imageFile = croppedFile;
});
} else {
ScaffoldMessenger.of(context).showSnackBar(
@@ -53,6 +77,7 @@ class _AddScreenState extends State<AddScreen> {
}
}
Future<void> addNewElementToFirestore(String title, String description, File imageFile, {required Function(bool) onComplete}) async {
String collectionName;
if (_selectedIndex == 0) {
+56 -1
View File
@@ -1,5 +1,9 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'dart:typed_data';
import 'package:pdf/widgets.dart' as pdfWidgets;
import 'package:printing/printing.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:cached_network_image/cached_network_image.dart';
class DetailPage extends StatelessWidget {
final String title;
@@ -15,11 +19,62 @@ class DetailPage extends StatelessWidget {
required this.imageUrl,
}) : super(key: key);
Future<void> _printPdf() async {
final pdf = pdfWidgets.Document();
final image = await _loadNetworkImage(imageUrl);
pdf.addPage(
pdfWidgets.MultiPage(
build: (context) => [
pdfWidgets.Center(
child: pdfWidgets.SizedBox(
width: double.infinity, // Die Breite auf die maximal mögliche Breite setzen
child: pdfWidgets.AspectRatio(
aspectRatio: 1, // Sie können das Seitenverhältnis entsprechend Ihrem Bild anpassen
child: pdfWidgets.Image(image),
),
),
),
pdfWidgets.Text(
title,
style: pdfWidgets.TextStyle(
fontSize: 20,
fontWeight: pdfWidgets.FontWeight.bold // Titel fett
)
),
pdfWidgets.Text(datum, style: const pdfWidgets.TextStyle(fontSize: 14, )),
pdfWidgets.Paragraph(text: description, style: const pdfWidgets.TextStyle(fontSize: 12)),
],
),
);
await Printing.sharePdf(bytes: await pdf.save(), filename: '$title.pdf');
}
Future<pdfWidgets.ImageProvider> _loadNetworkImage(String url) async {
final response = await http.get(Uri.parse(url));
if (response.statusCode != 200) {
throw Exception('Failed to load network image.');
}
final Uint8List bytes = response.bodyBytes;
final imageProvider = pdfWidgets.MemoryImage(bytes);
return imageProvider;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
actions: <Widget>[
IconButton(
icon: const Icon(Icons.picture_as_pdf),
onPressed: _printPdf,
),
],
),
body: SingleChildScrollView(
child: Column(