implemented making a pdf from DetailPage.dart and croping image to 1:1 format
This commit is contained in:
+56
-1
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user