PDF library for Flutter not showing long text - flutter

I am using the pdf library in my Flutter project (https://pub.dev/packages/pdf).
This is part of the class that generates a PDF document
static Widget buildTablaDiario(List<dynamic> listaDiarioActual){
return ListView.builder(
itemCount: listaDiarioActual.length,
itemBuilder: (pw.Context context, index){
DiarioModelo diario = listaDiarioActual[index];
DateTime tempDateI =
new DateFormat("yyyy-MM-dd HH:mm:ss")
.parse(diario.fecha_diario);
String date1 = DateFormat("dd-MM-yyyy HH:mm")
.format(tempDateI);
String hora = DateFormat("HH:mm")
.format(tempDateI);
return pw.Row(
children: [
pw.Text("${hora}"),
pw.SizedBox(width: 10),
pw.Text(diario.descripcion,maxLines: 10)
]
);
}
);
}
My issue is at line
pw.Text(diario.descripcion,maxLines: 10)
The output from the PDF document is not taking into account the lenght of the text if it is longer than one line. The text ends at the end of the line and a new line is not created.
What can I do to show the text correctly if its lenght is greater than one line?

Related

How to get string arraylist from string by filtering single single word from single string in flutter?

"phrase": "educated washroom stinging breeze regroup earphone threaten epidemic hazy imbecile fritter bony"
So, phrase is my string.
I want to get phrase string in form of string arraylist
stringList = ['educated','washroom','stinging',....]
you can do it by using split method of string like this
const string = 'Hello world!';
final splitted = string.split(' ');
print(splitted); // [Hello, world!];
you can read more details about split method from the official documentation.
Edit:
The solution for the problem you typed in the comment.
ListView.builder(
itemCount: stringList.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(stringList[index]),
);
},
),

How to make flutter listview show millions of items

I'm tring to write a log viewer application with flutter.
I've learn that ListView.builder could handle larget number, even infinite number of items.
for example:
const mockLines = [
'this is a short line that will display in one line',
'this is a long line that will display in multiple lines, let us see how long it will be ?',
];
Widget _buildLogView() {
return ListView.builder(
itemCount: 1000,
itemBuilder: (context, index) {
final line = mockLines[index % mockLines.length];
return Text('$index: $line');
}
);
}
which works good, text got wrapped when it's too long.
But scrolling got very slow, and memory got blow when I set itemCount to 1000000.
Tried itemExtent:
Widget _buildLogView() {
return ListView.builder(
itemCount: 1000000,
itemExtent: 20
itemBuilder: (context, index) {
final line = mockLines[index % mockLines.length];
return Text('$index: $line');
}
);
}
Problem solved.
However, text won't wrap anymore, it just got truncated.
It seems that there's no way for me to create a listview can handle millions of lines when keeping the 'wrap' behavior.
Am I miss something, or just a limitation of flutter?
===============
A release build, draging scrollbar to 1/3 with itemCount = 1000000
memory keep crimbing up (would finally eat all)
same on windows:

Flutter PDF generated document margins

I am creating my first PDF document in Flutter using the pdf package.
Here you have a partial screenshot from the pdf part that I need to change:
You may see 6 rows, each of them has a time and a string.
Here you have the code for that screenshot:
static Widget buildTablaDiario(List<dynamic> listaDiarioActual){
return ListView.builder(
itemCount: listaDiarioActual.length,
itemBuilder: (pw.Context context, index){
DiarioModelo diario = listaDiarioActual[index];
DateTime tempDateI =
new DateFormat("yyyy-MM-dd hh:mm:ss")
.parse(diario.fecha_diario);
String date1 = DateFormat("dd-MM-yyyy HH:mm")
.format(tempDateI);
String hora = DateFormat("HH:mm")
.format(tempDateI);
return Row(
children: [
pw.Text("${hora}"),
pw.SizedBox(width: 10),
pw.Text(diario.descripcion,overflow: TextOverflow.clip)
]
);
}
);
}
I would like to set a right margin in order to avoid the text lines to end at the right edge from the document.
I know it's late, but in case anyone is still looking for an answer...
inside pw.Page or pw.MultiPage there is a margin attribute where you can add paddings
pdf.addPage(
pw.MultiPage(
pageFormat: PdfPageFormat.a4,
**margin: const pw.EdgeInsets.only(top: 100),**
orientation: pw.PageOrientation.portrait,...
Another approach is the following:
Before pdf.save() run the following to add your custom margins
for (int i = 1; i < pdf.document.pdfPageList.pages.length; i++) {
pdf.document.pdfPageList.pages
.elementAt(i)
.pageFormat
.copyWith(marginLeft: 50, marginTop: 100, marginRight: 50, marginBottom: 50);
}

Flutter: Display grid of images on PDF

I'm using flutter with the pdf plugin to generate a PDF document where I need to display a grid of images. The images are stored in a List<File>.
Here's what I've tried. Keep in mind that all these widgets are from the pdf package and not from material.
GridView(
crossAxisCount: 5,
childAspectRatio: 1,
children: door.images.map((image) {
return Container(
child: Image(MemoryImage(?????), fit: BoxFit.cover),
);
}).toList(),
),
Note: door.images is the List<File>
How do I convert the File image (from the List<File>) to the Uint8List bytes needed for the MemoryImage widgets?
Convert your File to imagebytes.
final List<int> _imageBytes = await image.readAsBytes();
And then convert the imagebytes to your Uint8List.
final String _uint8List = base64Encode(_imageBytes);

How to solve the error "Error parsing asset file" in advanced_pdf_viewer in flutter?

I am new to flutter and developing a custom app(just for testing). I have bunch of pdfs in docs folder. So to view respective pdf, I passed an id to the pdf viewer to load the pdf file from button. When I type the pdf name (static) in loaddocument() , it works fine.
FYI - all pdfs are starting with name of law and have integer value in end. for example law1.pdf.
here is the function code.
LoadPdf
import 'package:advance_pdf_viewer/advance_pdf_viewer.dart';
loadDocument() async {
String id= widget.lawid;
String filename= 'law$id';
print(filename);
document = await PDFDocument.fromAsset('docs/law/$filename.pdf');
setState(() => isLoading = false);
}
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Center(
child: isLoading
? Center(child: CircularProgressIndicator())
: PDFViewer(
document: document,
zoomSteps: 1,
scrollDirection: Axis.vertical,
)),
));
}
I got the error when I pass the id from Button. The error is Error parsing asset file. I am using advance_pdf_viewer package. Please suggest me too if you have any other pdf viewer package. This package doesn't support continuous scroll (it renders one pager per scroll).
Your help would be appreciated.