Skip to content

Commit 9327fe9

Browse files
authored
Merge pull request #530 from tneotia/bugfix/shrink-wrap
Fix shrink wrap not applying correctly
2 parents 1c51d62 + 14a9461 commit 9327fe9

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

README.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ A Flutter widget for rendering HTML and CSS as Flutter widgets.
8686
- [SVG](#svg)
8787

8888
- [Table](#table)
89+
90+
- [Notes](#notes)
8991

9092
- [Migration Guide](#migration-guides)
9193

@@ -102,12 +104,13 @@ Add the following to your `pubspec.yaml` file:
102104
| | | | | | | | | | | |
103105
|------------|-----------|-------|-------------|---------|---------|-------|------|--------|--------|--------|
104106
|`a` | `abbr` | `acronym`| `address` | `article`| `aside` | `audio`| `b` | `bdi` | `bdo` | `big` |
105-
|`blockquote`| `body` | `br` | `caption` | `cite` | `code` | `data`| `dd` | `del` | `dfn` | `div` |
106-
|`dl` | `dt` | `em` | `figcaption`| `figure`| `footer`| `h1` | `h2` | `h3` | `h4` | `h5` |
107-
|`h6` | `header` | `hr` | `i` | `iframe`| `img` | `ins` | `kbd`| `li` | `main` | `mark` |
108-
|`nav` | `noscript`|`ol` | `p` | `pre` | `q` | `rp` | `rt` | `ruby` | `s` |`samp` |
109-
|`section` | `small` | `span`| `strike` | `strong`| `sub` | `sup` | `svg`| `table`| `tbody`| `td` |
110-
| `template` | `tfoot` | `th` | `thead` |`time` | `tr` | `tt` | `u` | `ul` | `var` | `video`|
107+
|`blockquote`| `body` | `br` | `caption` | `cite` | `code` | `data`| `dd` | `del` | `details` | `dfn` |
108+
| `div` | `dl` | `dt` | `em` | `figcaption`| `figure`| `footer`| `h1` | `h2` | `h3` | `h4` |
109+
| `h5` |`h6` | `header` | `hr` | `i` | `iframe`| `img` | `ins` | `kbd`| `li` | `main` |
110+
| `mark` | `nav` | `noscript`|`ol` | `p` | `pre` | `q` | `rp` | `rt` | `ruby` | `s` |
111+
| `samp` | `section` | `small` | `span`| `strike` | `strong`| `sub` | `sup` | `summary` | `svg`| `table`|
112+
| `tbody` | `td` | `template` | `tfoot` | `th` | `thead` |`time` | `tr` | `tt` | `u` | `ul` |
113+
| `var` | `video` | | | | | | | | | |
111114

112115

113116

@@ -646,6 +649,24 @@ This package renders table elements using the [`flutter_layout_grid`](https://pu
646649

647650
When rendering table elements, the package tries to calculate the best fit for each element and size its cell accordingly. `Rowspan`s and `colspan`s are considered in this process, so cells that span across multiple rows and columns are rendered as expected. Heights are determined intrinsically to maintain an optimal aspect ratio for the cell.
648651

652+
## Notes
653+
654+
1. If you'd like to use this widget inside of a `Row()`, make sure to set `shrinkWrap: true` and place your widget inside expanded:
655+
656+
```dart
657+
Widget row = Row(
658+
children: [
659+
Expanded(
660+
child: Html(
661+
shrinkWrap: true,
662+
//other params
663+
)
664+
),
665+
//whatever other widgets
666+
]
667+
);
668+
```
669+
649670
## Migration Guides
650671
- For Version 1.0 - [Guide](https://github.com/Sub6Resources/flutter_html/wiki/1.0.0-Migration-Guide)
651672

lib/html_parser.dart

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ class HtmlParser extends StatelessWidget {
6868
RenderContext(
6969
buildContext: context,
7070
parser: this,
71-
style: Style.fromTextStyle(Theme.of(context).textTheme.bodyText2),
7271
),
7372
cleanedTree,
7473
);
@@ -81,6 +80,11 @@ class HtmlParser extends StatelessWidget {
8180
textSpan: parsedTree,
8281
style: cleanedTree.style,
8382
textScaleFactor: MediaQuery.of(context).textScaleFactor,
83+
renderContext: RenderContext(
84+
buildContext: context,
85+
parser: this,
86+
style: Style.fromTextStyle(Theme.of(context).textTheme.bodyText2),
87+
),
8488
);
8589
}
8690

@@ -322,6 +326,7 @@ class HtmlParser extends StatelessWidget {
322326
style: newContext.style.generateTextStyle(),
323327
),
324328
style: newContext.style,
329+
renderContext: context,
325330
),
326331
)
327332
],
@@ -412,6 +417,7 @@ class HtmlParser extends StatelessWidget {
412417
[],
413418
),
414419
style: newContext.style,
420+
renderContext: context,
415421
),
416422
),
417423
);
@@ -760,6 +766,7 @@ class ContainerSpan extends StatelessWidget {
760766
children: children,
761767
),
762768
style: newContext.style,
769+
renderContext: newContext,
763770
),
764771
);
765772
}
@@ -769,20 +776,19 @@ class StyledText extends StatelessWidget {
769776
final InlineSpan textSpan;
770777
final Style style;
771778
final double textScaleFactor;
779+
final RenderContext renderContext;
772780

773781
const StyledText({
774782
this.textSpan,
775783
this.style,
776784
this.textScaleFactor = 1.0,
785+
this.renderContext,
777786
});
778787

779788
@override
780789
Widget build(BuildContext context) {
781790
return SizedBox(
782-
width:
783-
style.display == Display.BLOCK || style.display == Display.LIST_ITEM
784-
? double.infinity
785-
: null,
791+
width: calculateWidth(style.display, renderContext),
786792
child: Text.rich(
787793
textSpan,
788794
style: style.generateTextStyle(),
@@ -792,4 +798,14 @@ class StyledText extends StatelessWidget {
792798
),
793799
);
794800
}
801+
802+
double calculateWidth(Display display, RenderContext context) {
803+
if ((display == Display.BLOCK || display == Display.LIST_ITEM) && !renderContext.parser.shrinkWrap) {
804+
return double.infinity;
805+
}
806+
if (renderContext.parser.shrinkWrap) {
807+
return MediaQuery.of(context.buildContext).size.width;
808+
}
809+
return null;
810+
}
795811
}

lib/src/layout_element.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class TableLayoutElement extends LayoutElement {
119119
child: StyledText(
120120
textSpan: context.parser.parseTree(context, child),
121121
style: child.style,
122+
renderContext: context,
122123
),
123124
),
124125
),
@@ -284,6 +285,7 @@ class DetailsContentElement extends LayoutElement {
284285
children: [firstChild] ?? [],
285286
),
286287
style: style,
288+
renderContext: context,
287289
) : Text("Details"),
288290
children: [
289291
StyledText(
@@ -292,6 +294,7 @@ class DetailsContentElement extends LayoutElement {
292294
children: getChildren(childrenList, context, elementList?.isNotEmpty == true && elementList?.first?.localName == "summary" ? firstChild : null)
293295
),
294296
style: style,
297+
renderContext: context,
295298
),
296299
]
297300
);

0 commit comments

Comments
 (0)