Skip to content

Commit dd012c1

Browse files
#6334 Improved table layout.
1 parent c677d20 commit dd012c1

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

lib/src/builtins/flutter_html_table.dart

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,12 @@ class TableHtmlExtension extends HtmlExtension {
178178
style: context.styledElement!.style,
179179
child: LayoutBuilder(
180180
builder: (ctx, constraints) {
181-
final width = MediaQuery.sizeOf(ctx).width;
181+
double width;
182+
if (constraints.hasBoundedWidth) {
183+
width = constraints.maxWidth;
184+
} else {
185+
width = MediaQuery.sizeOf(ctx).width - 32;
186+
}
182187
return _layoutCells(
183188
context.styledElement as TableElement,
184189
context.builtChildrenMap!,
@@ -217,7 +222,6 @@ List<TableCellElement> _getCellDescendants(List<StyledElement> children) {
217222
}
218223

219224
Widget _layoutCells(TableElement table, Map<StyledElement, InlineSpan> parsedCells, ExtensionContext context, double width) {
220-
width -= 32;
221225
double requiredWidth = 0;
222226
for (final minWidth in table.minWidths) {
223227
requiredWidth += minWidth;
@@ -259,8 +263,10 @@ Widget _layoutCells(TableElement table, Map<StyledElement, InlineSpan> parsedCel
259263
...row.children.whereType<TableCellElement>().map((cell) => cell.rowspan - 1),
260264
];
261265
// Ignore width set in CSS, there is only one proper layout...
262-
row.children.whereType<TableCellElement>().forEach((cell)=> cell.style.width = null);
266+
row.children.whereType<TableCellElement>().forEach((cell) => cell.style.width = null);
263267
}
268+
double borderWidth = rows.first.children.whereType<TableCellElement>().first.style.border?.left.width ?? 0;
269+
double borderAdjustment = borderWidth * columnMax / (columnMax + 1);
264270

265271
// Place the cells in the rows/columns
266272
final cells = <GridPlacement>[];
@@ -286,15 +292,12 @@ Widget _layoutCells(TableElement table, Map<StyledElement, InlineSpan> parsedCel
286292
child: CssBoxWidget(
287293
style: child.style.merge(row.style),
288294
child: Builder(builder: (context) {
289-
final alignment =
290-
child.style.direction ?? Directionality.of(context);
295+
final alignment = child.style.direction ?? Directionality.of(context);
291296
return SizedBox.expand(
292297
child: Container(
293298
alignment: _getCellAlignment(child, alignment),
294299
child: CssBoxWidget.withInlineSpanChildren(
295-
children: [
296-
parsedCells[child] ?? const TextSpan(text: "error")
297-
],
300+
children: [parsedCells[child] ?? const TextSpan(text: "error")],
298301
style: Style(),
299302
),
300303
),
@@ -315,8 +318,7 @@ Widget _layoutCells(TableElement table, Map<StyledElement, InlineSpan> parsedCel
315318
}
316319

317320
// Create column tracks (insofar there were no colgroups that already defined them)
318-
List<TrackSize> finalColumnSizes = List.generate(cellWidths.length, (index)=>FixedTrackSize(cellWidths[index]));
319-
// finalColumnSizes += List.generate(max(0, columnMax - finalColumnSizes.length), (_) => const IntrinsicContentTrackSize());
321+
List<TrackSize> finalColumnSizes = List.generate(cellWidths.length, (index) => FixedTrackSize(cellWidths[index] - borderAdjustment));
320322

321323
if (finalColumnSizes.isEmpty || rowSizes.isEmpty) {
322324
// No actual cells to show
@@ -326,7 +328,6 @@ Widget _layoutCells(TableElement table, Map<StyledElement, InlineSpan> parsedCel
326328
return SingleChildScrollView(
327329
scrollDirection: Axis.horizontal,
328330
child: SizedBox(
329-
width: width,
330331
child: LayoutGrid(
331332
gridFit: GridFit.loose,
332333
columnSizes: finalColumnSizes,

0 commit comments

Comments
 (0)