1
1
import 'package:flutter/material.dart' ;
2
2
import 'package:flutter_html/html_parser.dart' ;
3
+ import 'package:flutter_html/src/html_elements.dart' ;
3
4
import 'package:flutter_html/src/styled_element.dart' ;
4
5
import 'package:flutter_html/style.dart' ;
5
6
import 'package:html/dom.dart' as dom;
@@ -19,47 +20,154 @@ abstract class LayoutElement extends StyledElement {
19
20
20
21
class TableLayoutElement extends LayoutElement {
21
22
TableLayoutElement ({
23
+ String name,
24
+ Style style,
25
+ @required List <StyledElement > children,
26
+ dom.Element node,
27
+ }) : super (name: name, style: style, children: children, node: node);
28
+
29
+ @override
30
+ Widget toWidget (RenderContext context) {
31
+
32
+ final colWidths = children.where ((c) => c.name == "colgroup" ).map ((group) {
33
+ return group.children.where ((c) => c.name == "col" ).map ((c) {
34
+ final widthStr = c.attributes["width" ] ?? "" ;
35
+ if (widthStr.endsWith ("%" )) {
36
+ final width = double .tryParse (widthStr.substring (0 , widthStr.length - 1 )) * 0.01 ;
37
+ return FractionColumnWidth (width);
38
+ } else {
39
+ final width = double .tryParse (widthStr);
40
+ return FixedColumnWidth (width);
41
+ }
42
+ });
43
+ }).expand ((i) => i).toList ().asMap ();
44
+
45
+ return Container (
46
+ decoration: BoxDecoration (
47
+ color: style.backgroundColor
48
+ ),
49
+ child: Table (
50
+ columnWidths: colWidths,
51
+ children: children.map ((c) {
52
+ if (c is TableSectionLayoutElement ) {
53
+ return c.toTableRows (context);
54
+ }
55
+ return null ;
56
+ })
57
+ .where ((t) {
58
+ return t != null ;
59
+ })
60
+ .toList ()
61
+ .expand ((i) => i)
62
+ .toList (),
63
+ ));
64
+ }
65
+ }
66
+
67
+ class TableSectionLayoutElement extends LayoutElement {
68
+ TableSectionLayoutElement ({
69
+ String name,
22
70
@required List <StyledElement > children,
23
- }) : super (children: children);
71
+ }) : super (name : name, children: children);
24
72
25
73
@override
26
74
Widget toWidget (RenderContext context) {
27
- return Table (
28
- // children: children.where((e) => e.name == 'tr').map(),
29
- );
75
+ return Container (child: Text ("TABLE SECTION" ));
76
+ }
77
+
78
+ List <TableRow > toTableRows (RenderContext context) {
79
+ return children.map ((c) {
80
+ if (c is TableRowLayoutElement ) {
81
+ return c.toTableRow (context);
82
+ }
83
+ return null ;
84
+ }).where ((t) {
85
+ return t != null ;
86
+ }).toList ();
30
87
}
31
88
}
32
89
33
90
class TableRowLayoutElement extends LayoutElement {
34
91
TableRowLayoutElement ({
92
+ String name,
35
93
@required List <StyledElement > children,
36
- }) : super (children: children);
94
+ dom.Element node,
95
+ }) : super (name: name, children: children, node: node);
37
96
38
97
@override
39
98
Widget toWidget (RenderContext context) {
40
99
return Container (child: Text ("TABLE ROW" ));
41
100
}
42
101
43
102
TableRow toTableRow (RenderContext context) {
44
-
103
+ return TableRow (
104
+ decoration: BoxDecoration (
105
+ border: style.border,
106
+ ),
107
+ children: children.map ((c) {
108
+ if (c is StyledElement && c.name == 'td' || c.name == 'th' ) {
109
+ return TableCell (
110
+ child: Container (
111
+ padding: c.style.padding,
112
+ decoration: BoxDecoration (
113
+ color: c.style.backgroundColor
114
+ ),
115
+ child: RichText (text: context.parser.parseTree (context, c))));
116
+ }
117
+ return null ;
118
+ }).where ((c) => c != null ).toList ());
45
119
}
46
120
}
47
121
48
- LayoutElement parseLayoutElement (dom.Element element, List <StyledElement > children) {
122
+ class TableStyleElement extends StyledElement {
123
+ TableStyleElement ({
124
+ String name,
125
+ List <StyledElement > children,
126
+ Style style,
127
+ dom.Element node,
128
+ }) : super (name: name, children: children, style: style, node: node);
129
+ }
130
+
131
+ TableStyleElement parseTableDefinitionElement (
132
+ dom.Element element, List <StyledElement > children) {
133
+ switch (element.localName) {
134
+ case "colgroup" :
135
+ case "col" :
136
+ return TableStyleElement (
137
+ name: element.localName,
138
+ children: children,
139
+ node: element
140
+ );
141
+ default :
142
+ return TableStyleElement ();
143
+ }
144
+ }
145
+ LayoutElement parseLayoutElement (
146
+ dom.Element element, List <StyledElement > children) {
49
147
switch (element.localName) {
50
148
case "table" :
51
149
return TableLayoutElement (
150
+ name: element.localName,
151
+ children: children,
152
+ node: element,
153
+ );
154
+ break ;
155
+ case "thead" :
156
+ case "tbody" :
157
+ case "tfoot" :
158
+ return TableSectionLayoutElement (
159
+ name: element.localName,
52
160
children: children,
53
161
);
54
162
break ;
55
163
case "tr" :
56
- return TableLayoutElement (
164
+ return TableRowLayoutElement (
165
+ name: element.localName,
57
166
children: children,
167
+ node: element
58
168
);
59
169
break ;
60
170
default :
61
- return TableLayoutElement (
62
- children: children
63
- );
171
+ return TableLayoutElement (children: children);
64
172
}
65
173
}
0 commit comments