@@ -4,6 +4,8 @@ import 'package:flutter/rendering.dart';
44import 'package:flutter/widgets.dart' ;
55import 'package:logging/logging.dart' ;
66
7+ import 'css_sizing.dart' show CssSizingHint;
8+
79/// A TABLE widget.
810class HtmlTable extends MultiChildRenderObjectWidget {
911 /// The table border sides.
@@ -19,12 +21,6 @@ class HtmlTable extends MultiChildRenderObjectWidget {
1921 /// Default: `0.0` .
2022 final double borderSpacing;
2123
22- /// If non-null, overwrites the incoming [BoxConstraints.maxWidth] .
23- final double ? maxWidth;
24-
25- /// If non-null, overwrites the incoming [BoxConstraints.minWidth] .
26- final double ? minWidth;
27-
2824 /// Determines the order to lay children out horizontally.
2925 ///
3026 /// Default: [TextDirection.ltr] .
@@ -36,21 +32,22 @@ class HtmlTable extends MultiChildRenderObjectWidget {
3632 this .borderCollapse = false ,
3733 this .borderSpacing = 0.0 ,
3834 required super .children,
39- this .maxWidth,
40- this .minWidth,
4135 this .textDirection = TextDirection .ltr,
4236 super .key,
4337 });
4438
4539 @override
46- RenderObject createRenderObject (BuildContext context) => _TableRenderObject (
47- border,
48- textDirection,
49- borderCollapse: borderCollapse,
50- borderSpacing: borderSpacing,
51- maxWidth: maxWidth,
52- minWidth: minWidth,
53- );
40+ RenderObject createRenderObject (BuildContext context) {
41+ final hint = context.dependOnInheritedWidgetOfExactType <CssSizingHint >();
42+ return _TableRenderObject (
43+ border,
44+ textDirection,
45+ borderCollapse: borderCollapse,
46+ borderSpacing: borderSpacing,
47+ maxWidth: hint? .maxWidth,
48+ minWidth: hint? .minWidth,
49+ );
50+ }
5451
5552 @override
5653 void debugFillProperties (DiagnosticPropertiesBuilder properties) {
@@ -66,8 +63,6 @@ class HtmlTable extends MultiChildRenderObjectWidget {
6663 );
6764 properties
6865 .add (DoubleProperty ('borderSpacing' , borderSpacing, defaultValue: 0.0 ));
69- properties.add (DoubleProperty ('maxWidth' , maxWidth, defaultValue: null ));
70- properties.add (DoubleProperty ('minWidth' , minWidth, defaultValue: null ));
7166 properties.add (
7267 DiagnosticsProperty (
7368 'textDirection' ,
@@ -79,12 +74,13 @@ class HtmlTable extends MultiChildRenderObjectWidget {
7974
8075 @override
8176 void updateRenderObject (BuildContext context, RenderObject renderObject) {
77+ final hint = context.dependOnInheritedWidgetOfExactType <CssSizingHint >();
8278 (renderObject as _TableRenderObject )
8379 ..setBorder (border)
8480 ..setBorderCollapse (borderCollapse)
8581 ..setBorderSpacing (borderSpacing)
86- ..setMaxWidth (maxWidth)
87- ..setMinWidth (minWidth)
82+ ..setMaxWidth (hint ? . maxWidth)
83+ ..setMinWidth (hint ? . minWidth)
8884 ..setTextDirection (textDirection);
8985 }
9086}
@@ -333,36 +329,38 @@ class _TableRenderLayouter {
333329 child = data.nextSibling;
334330 }
335331
332+ final columnGapsSum = (columnCount + 1 ) * tro.columnGap;
333+ final gapsAndPaddings = tro.paddingLeft + columnGapsSum + tro.paddingRight;
334+
335+ double ? availableWidth;
336336 final maxWidth = tro._maxWidth ?? constraints.maxWidth;
337+ if (maxWidth.isFinite && maxWidth > 0 ) {
338+ availableWidth = maxWidth - gapsAndPaddings;
339+ }
337340
338- var wc = const BoxConstraints ();
339- final troMinWidth = tro._minWidth;
340- if (troMinWidth != null && troMinWidth > 0 ) {
341- // only apply width constrains if a specific min value has been set
342- // this will be used mostly in percentage calculations
343- wc = BoxConstraints (maxWidth: maxWidth, minWidth: troMinWidth);
341+ double ? requiredWidth;
342+ final minWidth = tro._minWidth ?? constraints.minWidth;
343+ if (minWidth.isFinite && minWidth > 0 ) {
344+ requiredWidth = minWidth - gapsAndPaddings;
344345 }
345346
346347 return _TableDataStep1 (
348+ availableWidth: availableWidth,
347349 cells: cells,
348350 children: children,
349351 columnCount: columnCount,
350- remainingMaxWidth:
351- _TableDataStep1 ._calculateRemainingWidth (tro, columnCount, maxWidth),
352- remainingMinWidth: _TableDataStep1 ._calculateRemainingWidth (
353- tro, columnCount, wc.minWidth),
352+ requiredWidth: requiredWidth,
354353 rowCount: rowCount,
355- widthConstraints: wc,
356354 );
357355 }
358356
359357 _TableDataStep2 step2NaiveColumnWidths (_TableDataStep1 step1) {
360358 final cells = step1.cells;
361359 final naiveColumnWidths = List .filled (step1.columnCount, .0 );
362360
363- final remainingMinWidth = step1.remainingMinWidth ;
364- if (remainingMinWidth != null && step1.columnCount > 0 ) {
365- final cellMinWidth = remainingMinWidth / step1.columnCount;
361+ final requiredWidth = step1.requiredWidth ;
362+ if (requiredWidth != null && step1.columnCount > 0 ) {
363+ final cellMinWidth = requiredWidth / step1.columnCount;
366364 for (var i = 0 ; i < cells.length; i++ ) {
367365 final data = cells[i];
368366 naiveColumnWidths.setMaxColumnWidths (tro, data, cellMinWidth);
@@ -379,9 +377,9 @@ class _TableRenderLayouter {
379377
380378 _TableDataStep3 step3MinIntrinsicWidth (_TableDataStep2 step2) {
381379 final step1 = step2.step1;
380+ final availableWidth = step1.availableWidth;
382381 final cells = step1.cells;
383382 final children = step1.children;
384- final remainingMaxWidth = step1.remainingMaxWidth;
385383
386384 final cellSizes = List <Size ?>.filled (children.length, null );
387385 final childMinWidths = List <double ?>.filled (children.length, null );
@@ -393,7 +391,7 @@ class _TableRenderLayouter {
393391 // it only considers min value when the columns don't fit
394392 var columnWidths = maxColumnWidths;
395393 if (columnWidths.zeros.isEmpty &&
396- (remainingMaxWidth == null || columnWidths.sum <= remainingMaxWidth )) {
394+ (availableWidth == null || columnWidths.sum <= availableWidth )) {
397395 return _TableDataStep3 (
398396 step2,
399397 columnWidths: columnWidths,
@@ -411,10 +409,10 @@ class _TableRenderLayouter {
411409 if (cellSizes[i] == null ) {
412410 // side effect
413411 // layout cells for the initial width
414- final layoutSize = layouter (child, step1.widthConstraints );
412+ final layoutSize = layouter (child, constraints );
415413 cellSizes[i] = layoutSize;
416414 maxColumnWidths.setMaxColumnWidths (tro, data, layoutSize.width);
417- logger.fine ('[3] Got child#$i $layoutSize @${ step1 . widthConstraints } ' );
415+ logger.fine ('[3] Got child#$i $layoutSize @$constraints ' );
418416 shouldLoop = true ;
419417 }
420418
@@ -446,9 +444,9 @@ class _TableRenderLayouter {
446444 }
447445 }
448446
449- if (remainingMaxWidth != null ) {
447+ if (availableWidth != null ) {
450448 columnWidths = redistributeValues (
451- available: remainingMaxWidth ,
449+ available: availableWidth ,
452450 maxValues: maxColumnWidths,
453451 minValues: minColumnWidths,
454452 );
@@ -470,20 +468,20 @@ class _TableRenderLayouter {
470468 required List <double > minColumnWidths,
471469 }) {
472470 final step1 = step2.step1;
473- final remainingMaxWidth = step1.remainingMaxWidth ;
471+ final availableWidth = step1.availableWidth ;
474472
475473 final widthSum = columnWidths.sumRange (data);
476474 final maxWidthSum = maxColumnWidths.sumRange (data);
477475 if (widthSum >= maxWidthSum) {
478476 // cell has been allocated more than its requested value
479477 // skip measuring if not absolutely needed because it's expensive
480478
481- if (remainingMaxWidth == null ) {
482- // unbounded width constraints
479+ if (availableWidth == null ) {
480+ // unlimited available space
483481 return null ;
484482 }
485483
486- if (columnWidths.sum <= remainingMaxWidth ) {
484+ if (columnWidths.sum <= availableWidth ) {
487485 // current widths are good enough
488486 return null ;
489487 }
@@ -658,34 +656,21 @@ class _TableRenderLayouter {
658656
659657@immutable
660658class _TableDataStep1 {
659+ final double ? availableWidth;
661660 final List <_TableCellData > cells;
662661 final List <RenderBox > children;
663662 final int columnCount;
664- final double ? remainingMaxWidth;
665- final double ? remainingMinWidth;
663+ final double ? requiredWidth;
666664 final int rowCount;
667- final BoxConstraints widthConstraints;
668665
669666 const _TableDataStep1 ({
667+ required this .availableWidth,
670668 required this .cells,
671669 required this .children,
672670 required this .columnCount,
673- required this .remainingMaxWidth,
674- required this .remainingMinWidth,
671+ required this .requiredWidth,
675672 required this .rowCount,
676- required this .widthConstraints,
677673 });
678-
679- static double _calculateRemainingWidth (
680- _TableRenderObject tro, int columnCount, double width) {
681- if (width.isZero) {
682- return 0 ;
683- }
684-
685- final columnGapsSum = (columnCount + 1 ) * tro.columnGap;
686- final gapsAndPaddings = tro.paddingLeft + columnGapsSum + tro.paddingRight;
687- return width - gapsAndPaddings;
688- }
689674}
690675
691676@immutable
0 commit comments