Skip to content

Commit 5be653d

Browse files
committed
Add shrinkWrap property, fix small link-image issue and style alignment issue
1 parent 135316f commit 5be653d

File tree

5 files changed

+23
-10
lines changed

5 files changed

+23
-10
lines changed

example/lib/main.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ class MyApp extends StatelessWidget {
1313
return new MaterialApp(
1414
title: 'Flutter Demo',
1515
theme: new ThemeData(
16-
primarySwatch: Colors.blue,
17-
brightness: Brightness.dark,
16+
primarySwatch: Colors.deepPurple,
1817
),
1918
home: new MyHomePage(title: 'flutter_html Example'),
2019
);
@@ -124,6 +123,7 @@ class _MyHomePageState extends State<MyHomePage> {
124123
@override
125124
Widget build(BuildContext context) {
126125
return new Scaffold(
126+
appBar: AppBar(title: Text('flutter_html Example')),
127127
body: SafeArea(
128128
child: Row(
129129
children: <Widget>[

lib/flutter_html.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Html extends StatelessWidget {
2727
decoration: TextDecoration.underline,
2828
color: Colors.blueAccent,
2929
decorationColor: Colors.blueAccent),
30-
this.shrinkToFit = false,
30+
this.shrinkWrap = false,
3131
@deprecated this.imageProperties,
3232
this.onImageTap,
3333
@deprecated this.showImages = true,
@@ -46,7 +46,7 @@ class Html extends StatelessWidget {
4646
final bool useRichText;
4747
final ImageErrorListener onImageError;
4848
final TextStyle linkStyle;
49-
final bool shrinkToFit;
49+
final bool shrinkWrap;
5050

5151
/// Properties for the Image widget that gets rendered by the rich text parser
5252
final ImageProperties imageProperties;
@@ -67,7 +67,7 @@ class Html extends StatelessWidget {
6767

6868
@override
6969
Widget build(BuildContext context) {
70-
final double width = shrinkToFit ? null : MediaQuery.of(context).size.width;
70+
final double width = shrinkWrap ? null : MediaQuery.of(context).size.width;
7171

7272
if (useRichText) {
7373
return Container(
@@ -77,7 +77,7 @@ class Html extends StatelessWidget {
7777
child: DefaultTextStyle.merge(
7878
style: defaultTextStyle ?? Theme.of(context).textTheme.body1,
7979
child: HtmlRichTextParser(
80-
shrinkToFit: shrinkToFit,
80+
shrinkToFit: shrinkWrap,
8181
onLinkTap: onLinkTap,
8282
renderNewlines: renderNewlines,
8383
customEdgeInsets: customEdgeInsets,
@@ -103,6 +103,7 @@ class Html extends StatelessWidget {
103103
onLinkTap: onLinkTap,
104104
onImageTap: onImageTap,
105105
onImageError: onImageError,
106+
shrinkWrap: shrinkWrap,
106107
style: style,
107108
customRender: customRender,
108109
blacklistedElements: blacklistedElements,

lib/html_parser.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class HtmlParser extends StatelessWidget {
2222
final OnTap onLinkTap;
2323
final OnTap onImageTap;
2424
final ImageErrorListener onImageError;
25+
final bool shrinkWrap;
2526

2627
final Map<String, Style> style;
2728
final Map<String, CustomRender> customRender;
@@ -33,6 +34,7 @@ class HtmlParser extends StatelessWidget {
3334
this.onLinkTap,
3435
this.onImageTap,
3536
this.onImageError,
37+
this.shrinkWrap,
3638
this.style,
3739
this.customRender,
3840
this.blacklistedElements,
@@ -209,11 +211,13 @@ class HtmlParser extends StatelessWidget {
209211
child: ContainerSpan(
210212
newContext: newContext,
211213
style: tree.style,
214+
shrinkWrap: context.parser.shrinkWrap,
212215
child: customRender[tree.name].call(
213216
newContext,
214217
ContainerSpan(
215218
newContext: newContext,
216219
style: tree.style,
220+
shrinkWrap: context.parser.shrinkWrap,
217221
children: tree.children?.map((tree) => parseTree(newContext, tree))?.toList() ?? [],
218222
),
219223
tree.attributes,
@@ -228,6 +232,7 @@ class HtmlParser extends StatelessWidget {
228232
child: ContainerSpan(
229233
newContext: newContext,
230234
style: tree.style,
235+
shrinkWrap: context.parser.shrinkWrap,
231236
children: tree.children?.map((tree) => parseTree(newContext, tree))?.toList() ?? [],
232237
),
233238
);
@@ -236,6 +241,7 @@ class HtmlParser extends StatelessWidget {
236241
child: ContainerSpan(
237242
newContext: newContext,
238243
style: tree.style,
244+
shrinkWrap: context.parser.shrinkWrap,
239245
child: Stack(
240246
children: <Widget>[
241247
PositionedDirectional(
@@ -599,12 +605,14 @@ class ContainerSpan extends StatelessWidget {
599605
final List<InlineSpan> children;
600606
final Style style;
601607
final RenderContext newContext;
608+
final bool shrinkWrap;
602609

603610
ContainerSpan({
604611
this.child,
605612
this.children,
606613
this.style,
607614
this.newContext,
615+
this.shrinkWrap = false,
608616
});
609617

610618
@override
@@ -618,7 +626,7 @@ class ContainerSpan extends StatelessWidget {
618626
width: style?.width,
619627
padding: style?.padding,
620628
margin: style?.margin,
621-
alignment: style?.alignment,
629+
alignment: shrinkWrap? null: style?.alignment,
622630
child: child ??
623631
RichText(
624632
text: TextSpan(

lib/src/utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Context<T> {
99
// This class is a workaround so that both an image
1010
// and a link can detect taps at the same time.
1111
class MultipleTapGestureRecognizer extends TapGestureRecognizer {
12-
bool _ready;
12+
bool _ready = false;
1313

1414
@override
1515
void addAllowedPointer(PointerDownEvent event) {

lib/style.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,13 @@ class Style {
8080
this.after,
8181
this.textDirection,
8282
this.border,
83-
this.alignment = Alignment.centerLeft, //TODO
83+
this.alignment,
8484
this.markerContent,
85-
});
85+
}) {
86+
if (this.alignment == null && (display == Display.BLOCK || display == Display.LIST_ITEM)) {
87+
this.alignment = Alignment.centerLeft;
88+
}
89+
}
8690

8791
//TODO: all attributes of TextStyle likely have a CSS attribute and should be supported.
8892
TextStyle generateTextStyle() {

0 commit comments

Comments
 (0)