Skip to content

Commit 196321c

Browse files
#6366 Improved table layout.
1 parent 2e97b67 commit 196321c

File tree

2 files changed

+45
-35
lines changed

2 files changed

+45
-35
lines changed

lib/src/builtins/flutter_html_table.dart

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import 'package:flutter_layout_grid/flutter_layout_grid.dart';
1313
/// Currently, nested tables are not supported.
1414
///
1515
///
16-
final RegExp _regExp = RegExp(r'[\w/\(\)]+');
17-
1816
class TableHtmlExtension extends HtmlExtension {
1917
const TableHtmlExtension();
2018

@@ -268,7 +266,6 @@ Widget _layoutCells(TableElement table, Map<StyledElement, InlineSpan> parsedCel
268266
));
269267
}
270268

271-
272269
List<double> _getColWidths(List<StyledElement> children) {
273270
final widths = <double>[];
274271
for (final child in children) {
@@ -294,45 +291,58 @@ List<double> _getColWidths(List<StyledElement> children) {
294291
List<double> _getColWidthsFromRow(TableRowLayoutElement row) {
295292
List<double> widths = [];
296293
for (final cell in row.children) {
297-
double minWidth = 0;
298294
if (cell is TableCellElement) {
299-
// Get entire text
300-
StringBuffer text = StringBuffer();
295+
WidthInfo info = WidthInfo();
301296
for (final child in cell.children) {
302-
text.write(_getText(child));
303-
}
304-
305-
final words = _regExp.allMatches(text.toString()).map((match) => match.group(0)!).toList();
306-
for (final word in words) {
307-
double wordWidth = TextPainter.computeWidth(
308-
text: TextSpan(
309-
text: word,
310-
style: TextStyle(
311-
fontSize: cell.style.fontSize?.value ?? 16,
312-
fontFamily: cell.style.fontFamily,
313-
fontWeight: cell.style.fontWeight,
314-
fontStyle: cell.style.fontStyle,
315-
)),
316-
textDirection: TextDirection.ltr,
317-
);
318-
if (wordWidth > minWidth) {
319-
minWidth = wordWidth;
320-
}
297+
_getCellInfo(child, info);
321298
}
299+
double minWidth = info.requiredWidth + 32;
300+
widths.add(minWidth);
322301
}
323-
minWidth += 32;
324-
widths.add(minWidth);
325302
}
326303
return widths;
327304
}
328305

329-
String _getText(StyledElement element) {
330-
if (element is TextContentElement) return element.text ?? '';
331-
StringBuffer buffer = StringBuffer();
332-
for (final child in element.children) {
333-
buffer.write(_getText(child));
306+
void _getCellInfo(StyledElement element, WidthInfo info) {
307+
if (element is TextContentElement) {
308+
final regexp = RegExp(r'\s+|(?=[()/\-—])|(?<=[()/\-—])');
309+
final text = element.text;
310+
if (text == null || text.isEmpty) return;
311+
final words = text.split(regexp).where((word) => word.isNotEmpty).toList();
312+
for (final word in words) {
313+
double wordWidth = TextPainter.computeWidth(
314+
text: TextSpan(
315+
text: word,
316+
style: TextStyle(
317+
fontSize: element.style.fontSize?.value ?? 16,
318+
fontFamily: element.style.fontFamily,
319+
fontWeight: element.style.fontWeight,
320+
fontStyle: element.style.fontStyle,
321+
)),
322+
textDirection: TextDirection.ltr,
323+
);
324+
if (info.join && !regexp.hasMatch(word[0])) {
325+
info.width += wordWidth;
326+
} else {
327+
info.width = wordWidth;
328+
}
329+
if (info.width > info.requiredWidth) {
330+
info.requiredWidth = info.width;
331+
}
332+
info.join = !regexp.hasMatch(word[word.length - 1]);
333+
}
334+
info.join = !regexp.hasMatch(text[text.length - 1]);
335+
} else {
336+
for (final child in element.children) {
337+
_getCellInfo(child, info);
338+
}
334339
}
335-
return buffer.toString();
340+
}
341+
342+
class WidthInfo {
343+
double width = 0;
344+
double requiredWidth = 0;
345+
bool join = false;
336346
}
337347

338348
Alignment _getCellAlignment(TableCellElement cell, TextDirection alignment) {

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_html
22
description: A Flutter widget for rendering static HTML and CSS as Flutter widgets.
3-
version: 3.0.0-beta5803
3+
version: 3.0.0-beta6366
44
homepage: https://github.com/Sub6Resources/flutter_html
55

66
environment:
@@ -39,4 +39,4 @@ topics:
3939
- css
4040
- widgets
4141
- layout
42-
- flutter_html
42+
- flutter-html

0 commit comments

Comments
 (0)