Skip to content

Commit 4ee8640

Browse files
committed
Add shrinkToFit option to shrink Html to its natural content width
1 parent 2039240 commit 4ee8640

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

lib/flutter_html.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ library flutter_html;
33
import 'package:flutter/material.dart';
44
import 'package:flutter_html/html_parser.dart';
55
import 'package:flutter_html/rich_text_parser.dart';
6+
67
import 'image_properties.dart';
78

89
class Html extends StatelessWidget {
@@ -25,6 +26,7 @@ class Html extends StatelessWidget {
2526
decoration: TextDecoration.underline,
2627
color: Colors.blueAccent,
2728
decorationColor: Colors.blueAccent),
29+
this.shrinkToFit = false,
2830
this.imageProperties,
2931
this.onImageTap,
3032
this.showImages = true,
@@ -40,6 +42,7 @@ class Html extends StatelessWidget {
4042
final bool useRichText;
4143
final ImageErrorListener onImageError;
4244
final TextStyle linkStyle;
45+
final bool shrinkToFit;
4346

4447
/// Properties for the Image widget that gets rendered by the rich text parser
4548
final ImageProperties imageProperties;
@@ -55,7 +58,7 @@ class Html extends StatelessWidget {
5558

5659
@override
5760
Widget build(BuildContext context) {
58-
final double width = MediaQuery.of(context).size.width;
61+
final double width = shrinkToFit ? null : MediaQuery.of(context).size.width;
5962

6063
return Container(
6164
padding: padding,
@@ -65,7 +68,7 @@ class Html extends StatelessWidget {
6568
style: defaultTextStyle ?? DefaultTextStyle.of(context).style,
6669
child: (useRichText)
6770
? HtmlRichTextParser(
68-
width: width,
71+
shrinkToFit: shrinkToFit,
6972
onLinkTap: onLinkTap,
7073
renderNewlines: renderNewlines,
7174
customEdgeInsets: customEdgeInsets,

lib/rich_text_parser.dart

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,11 @@ class BlockText extends StatelessWidget {
8484
final EdgeInsets padding;
8585
final EdgeInsets margin;
8686
final Decoration decoration;
87+
final bool shrinkToFit;
8788

8889
BlockText({
8990
@required this.child,
91+
@required this.shrinkToFit,
9092
this.padding,
9193
this.margin,
9294
this.decoration,
@@ -95,7 +97,7 @@ class BlockText extends StatelessWidget {
9597
@override
9698
Widget build(BuildContext context) {
9799
return Container(
98-
width: double.infinity,
100+
width: shrinkToFit ? null : double.infinity,
99101
padding: this.padding,
100102
margin: this.margin,
101103
decoration: this.decoration,
@@ -147,7 +149,7 @@ class ParseContext {
147149

148150
class HtmlRichTextParser extends StatelessWidget {
149151
HtmlRichTextParser({
150-
@required this.width,
152+
this.shrinkToFit,
151153
this.onLinkTap,
152154
this.renderNewlines = false,
153155
this.html,
@@ -167,7 +169,7 @@ class HtmlRichTextParser extends StatelessWidget {
167169

168170
final double indentSize = 10.0;
169171

170-
final double width;
172+
final bool shrinkToFit;
171173
final onLinkTap;
172174
final bool renderNewlines;
173175
final String html;
@@ -417,6 +419,7 @@ class HtmlRichTextParser extends StatelessWidget {
417419
));
418420
}
419421
BlockText blockText = BlockText(
422+
shrinkToFit: shrinkToFit,
420423
margin: EdgeInsets.only(
421424
top: 8.0,
422425
bottom: 8.0,
@@ -430,8 +433,10 @@ class HtmlRichTextParser extends StatelessWidget {
430433
);
431434
parseContext.rootWidgetList.add(blockText);
432435
} else {
433-
parseContext.rootWidgetList
434-
.add(BlockText(child: RichText(text: span)));
436+
parseContext.rootWidgetList.add(BlockText(
437+
child: RichText(text: span),
438+
shrinkToFit: shrinkToFit,
439+
));
435440
}
436441

437442
// this allows future items to be added as children of this item
@@ -591,6 +596,7 @@ class HtmlRichTextParser extends StatelessWidget {
591596
} else {
592597
// start a new block element for this link and its text
593598
BlockText blockElement = BlockText(
599+
shrinkToFit: shrinkToFit,
594600
margin: EdgeInsets.only(
595601
left: parseContext.indentLevel * indentSize, top: 10.0),
596602
child: RichText(text: span),
@@ -813,6 +819,7 @@ class HtmlRichTextParser extends StatelessWidget {
813819
}
814820
if (node.attributes['alt'] != null) {
815821
parseContext.rootWidgetList.add(BlockText(
822+
shrinkToFit: shrinkToFit,
816823
margin:
817824
EdgeInsets.symmetric(horizontal: 0.0, vertical: 10.0),
818825
padding: EdgeInsets.all(0.0),
@@ -836,6 +843,7 @@ class HtmlRichTextParser extends StatelessWidget {
836843
leadingChar = parseContext.listCount.toString() + '.';
837844
}
838845
BlockText blockText = BlockText(
846+
shrinkToFit: shrinkToFit,
839847
margin: EdgeInsets.only(
840848
left: parseContext.indentLevel * indentSize, top: 3.0),
841849
child: RichText(
@@ -907,6 +915,7 @@ class HtmlRichTextParser extends StatelessWidget {
907915
));
908916
}
909917
BlockText blockText = BlockText(
918+
shrinkToFit: shrinkToFit,
910919
margin: node.localName != 'body'
911920
? _customEdgeInsets ??
912921
EdgeInsets.only(

0 commit comments

Comments
 (0)