Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
dart_pdf
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
David PHAM-VAN
2020-04-27 18:04:50 -0400
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
bab846c7a8cb354ed7e44f9518d59047879836dc
bab846c7
1 parent
4b99a6f2
Improve Table.fromTextArray()
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
192 additions
and
34 deletions
pdf/CHANGELOG.md
pdf/lib/widgets/table.dart
pdf/pubspec.yaml
pdf/test/widget_table_test.dart
pdf/test/widget_test.dart
test/golden/widgets-table.pdf
test/golden/widgets-theme.pdf
test/golden/widgets.pdf
pdf/CHANGELOG.md
View file @
bab846c
# Changelog
## 1.8.0
-
Improve Table.fromTextArray()
## 1.7.1
-
Fix justified text softWrap issue
...
...
pdf/lib/widgets/table.dart
View file @
bab846c
...
...
@@ -187,48 +187,156 @@ class FractionColumnWidth extends TableColumnWidth {
}
}
typedef
OnCellFormat
=
String
Function
(
int
index
,
dynamic
data
);
/// A widget that uses the table layout algorithm for its children.
class
Table
extends
Widget
implements
SpanningWidget
{
Table
(
{
this
.
children
=
const
<
TableRow
>[],
this
.
border
,
this
.
defaultVerticalAlignment
=
TableCellVerticalAlignment
.
top
,
this
.
columnWidths
,
this
.
defaultColumnWidth
=
const
IntrinsicColumnWidth
(),
this
.
tableWidth
=
TableWidth
.
max
})
:
assert
(
children
!=
null
),
Table
({
this
.
children
=
const
<
TableRow
>[],
this
.
border
,
this
.
defaultVerticalAlignment
=
TableCellVerticalAlignment
.
top
,
this
.
columnWidths
,
this
.
defaultColumnWidth
=
const
IntrinsicColumnWidth
(),
this
.
tableWidth
=
TableWidth
.
max
,
})
:
assert
(
children
!=
null
),
assert
(
defaultColumnWidth
!=
null
),
assert
(
defaultVerticalAlignment
!=
null
),
super
();
factory
Table
.
fromTextArray
({
@required
Context
context
,
@required
List
<
List
<
String
>>
data
,
EdgeInsets
margin
=
const
EdgeInsets
.
all
(
5
),
Context
context
,
@required
List
<
List
<
dynamic
>>
data
,
@deprecated
EdgeInsets
margin
,
EdgeInsets
cellPadding
=
const
EdgeInsets
.
all
(
5
),
double
cellHeight
=
0
,
Alignment
cellAlignment
=
Alignment
.
topLeft
,
Map
<
int
,
Alignment
>
cellAlignments
,
TextStyle
cellStyle
,
TextStyle
oddCellStyle
,
OnCellFormat
cellFormat
,
int
headerCount
=
1
,
List
<
dynamic
>
headers
,
EdgeInsets
headerPadding
,
double
headerHeight
,
Alignment
headerAlignment
=
Alignment
.
center
,
Map
<
int
,
Alignment
>
headerAlignments
,
TextStyle
headerStyle
,
OnCellFormat
headerFormat
,
TableBorder
border
=
const
TableBorder
(),
Map
<
int
,
TableColumnWidth
>
columnWidths
,
IntrinsicColumnWidth
defaultColumnWidth
=
const
IntrinsicColumnWidth
(),
TableWidth
tableWidth
=
TableWidth
.
max
,
BoxDecoration
headerDecoration
,
BoxDecoration
rowDecoration
,
BoxDecoration
oddRowDecoration
,
})
{
assert
(
data
!=
null
);
assert
(
headerCount
!=
null
&&
headerCount
>=
0
);
assert
(
cellHeight
!=
null
);
if
(
margin
!=
null
)
{
cellPadding
=
margin
;
}
if
(
context
!=
null
)
{
final
ThemeData
theme
=
Theme
.
of
(
context
);
headerStyle
??=
theme
.
tableHeader
;
cellStyle
??=
theme
.
tableCell
;
}
headerPadding
??=
cellPadding
;
headerHeight
??=
cellHeight
;
oddRowDecoration
??=
rowDecoration
;
oddCellStyle
??=
cellStyle
;
cellAlignments
??=
const
<
int
,
Alignment
>{};
headerAlignments
??=
cellAlignments
;
final
List
<
TableRow
>
rows
=
<
TableRow
>[];
for
(
List
<
String
>
row
in
data
)
{
int
rowNum
=
0
;
if
(
headers
!=
null
)
{
final
List
<
Widget
>
tableRow
=
<
Widget
>[];
if
(
row
==
data
.
first
)
{
for
(
String
cell
in
row
)
{
tableRow
.
add
(
Container
(
alignment:
Alignment
.
center
,
margin:
margin
,
child:
Text
(
cell
,
style:
Theme
.
of
(
context
).
tableHeader
)));
for
(
final
dynamic
cell
in
headers
)
{
tableRow
.
add
(
Container
(
alignment:
headerAlignments
[
tableRow
.
length
]
??
headerAlignment
,
padding:
headerPadding
,
constraints:
BoxConstraints
(
minHeight:
headerHeight
),
child:
Text
(
headerFormat
==
null
?
cell
.
toString
()
:
headerFormat
(
tableRow
.
length
,
cell
),
style:
headerStyle
,
),
),
);
}
rows
.
add
(
TableRow
(
children:
tableRow
,
repeat:
true
,
decoration:
headerDecoration
,
));
rowNum
++;
}
for
(
final
List
<
dynamic
>
row
in
data
)
{
final
List
<
Widget
>
tableRow
=
<
Widget
>[];
final
bool
isOdd
=
(
rowNum
-
headerCount
)
%
2
!=
0
;
if
(
rowNum
<
headerCount
)
{
for
(
final
dynamic
cell
in
row
)
{
tableRow
.
add
(
Container
(
alignment:
headerAlignments
[
tableRow
.
length
]
??
headerAlignment
,
padding:
headerPadding
,
constraints:
BoxConstraints
(
minHeight:
headerHeight
),
child:
Text
(
headerFormat
==
null
?
cell
.
toString
()
:
headerFormat
(
tableRow
.
length
,
cell
),
style:
headerStyle
,
),
),
);
}
}
else
{
for
(
String
cell
in
row
)
{
tableRow
.
add
(
Container
(
margin:
margin
,
child:
Text
(
cell
,
style:
Theme
.
of
(
context
).
tableCell
)));
for
(
final
dynamic
cell
in
row
)
{
tableRow
.
add
(
Container
(
alignment:
cellAlignments
[
tableRow
.
length
]
??
cellAlignment
,
padding:
cellPadding
,
constraints:
BoxConstraints
(
minHeight:
cellHeight
),
child:
Text
(
cellFormat
==
null
?
cell
.
toString
()
:
cellFormat
(
tableRow
.
length
,
cell
),
style:
isOdd
?
oddCellStyle
:
cellStyle
,
),
),
);
}
}
rows
.
add
(
TableRow
(
children:
tableRow
,
repeat:
row
==
data
.
first
));
BoxDecoration
decoration
=
isOdd
?
oddRowDecoration
:
rowDecoration
;
if
(
rowNum
<
headerCount
)
{
decoration
=
headerDecoration
;
}
rows
.
add
(
TableRow
(
children:
tableRow
,
repeat:
rowNum
<
headerCount
,
decoration:
decoration
,
));
rowNum
++;
}
return
Table
(
border:
const
TableBorder
(),
tableWidth:
TableWidth
.
max
,
children:
rows
);
border:
border
,
tableWidth:
tableWidth
,
children:
rows
,
columnWidths:
columnWidths
,
defaultColumnWidth:
defaultColumnWidth
,
);
}
@override
...
...
pdf/pubspec.yaml
View file @
bab846c
...
...
@@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl
homepage
:
https://github.com/DavBfr/dart_pdf/tree/master/pdf
repository
:
https://github.com/DavBfr/dart_pdf
issue_tracker
:
https://github.com/DavBfr/dart_pdf/issues
version
:
1.
7.1
version
:
1.
8.0
environment
:
sdk
:
"
>=2.3.0
<3.0.0"
...
...
pdf/test/widget_table_test.dart
View file @
bab846c
...
...
@@ -186,6 +186,52 @@ void main() {
);
});
test
(
'Table fromTextArray'
,
()
{
pdf
.
addPage
(
Page
(
build:
(
Context
context
)
=>
Table
.
fromTextArray
(
context:
context
,
tableWidth:
TableWidth
.
min
,
data:
<
List
<
dynamic
>>[
<
dynamic
>[
'One'
,
'Two'
,
'Three'
],
<
dynamic
>[
1
,
2
,
3
],
<
dynamic
>[
4
,
5
,
6
],
],
),
));
});
test
(
'Table fromTextArray with formatting'
,
()
{
pdf
.
addPage
(
Page
(
build:
(
Context
context
)
=>
Table
.
fromTextArray
(
border:
null
,
cellAlignment:
Alignment
.
center
,
headerDecoration:
const
BoxDecoration
(
borderRadius:
2
,
color:
PdfColors
.
indigo
,
),
headerHeight:
25
,
cellHeight:
40
,
headerStyle:
TextStyle
(
color:
PdfColors
.
white
,
fontWeight:
FontWeight
.
bold
,
),
rowDecoration:
const
BoxDecoration
(
border:
BoxBorder
(
bottom:
true
,
color:
PdfColors
.
indigo
,
width:
.
5
,
),
),
headers:
<
dynamic
>[
'One'
,
'Two'
,
'Three'
],
data:
<
List
<
dynamic
>>[
<
dynamic
>[
1
,
2
,
3
],
<
dynamic
>[
4
,
5
,
6
],
<
dynamic
>[
7
,
8
,
9
],
],
),
));
});
tearDownAll
(()
{
final
File
file
=
File
(
'widgets-table.pdf'
);
file
.
writeAsBytesSync
(
pdf
.
save
());
...
...
pdf/test/widget_test.dart
View file @
bab846c
...
...
@@ -164,7 +164,7 @@ void main() {
build:
(
Context
context
)
=>
<
Widget
>[
Table
.
fromTextArray
(
context:
context
,
margin
:
const
EdgeInsets
.
all
(
3
),
cellPadding
:
const
EdgeInsets
.
all
(
3
),
data:
<
List
<
String
>>[
<
String
>[
'Company'
,
'Contact'
,
'Country'
],
<
String
>[
'Alfreds Futterkiste'
,
'Maria Anders'
,
'Germany'
],
...
...
test/golden/widgets-table.pdf
View file @
bab846c
No preview for this file type
test/golden/widgets-theme.pdf
View file @
bab846c
No preview for this file type
test/golden/widgets.pdf
View file @
bab846c
No preview for this file type
Please
register
or
login
to post a comment