Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New option: shrinkToFit #148

Merged
merged 5 commits into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class _MyHomePageState extends State<MyHomePage> {
<br />
Second nested div<br />
<figure>
<img src="https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark.png" />
<img src="https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png" />
<figcaption>Available on GitHub</figcaption>
</figure>
</div>
Expand Down
7 changes: 5 additions & 2 deletions lib/flutter_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ library flutter_html;
import 'package:flutter/material.dart';
import 'package:flutter_html/html_parser.dart';
import 'package:flutter_html/rich_text_parser.dart';

import 'image_properties.dart';

class Html extends StatelessWidget {
Expand All @@ -25,6 +26,7 @@ class Html extends StatelessWidget {
decoration: TextDecoration.underline,
color: Colors.blueAccent,
decorationColor: Colors.blueAccent),
this.shrinkToFit = false,
this.imageProperties,
this.onImageTap,
this.showImages = true,
Expand All @@ -40,6 +42,7 @@ class Html extends StatelessWidget {
final bool useRichText;
final ImageErrorListener onImageError;
final TextStyle linkStyle;
final bool shrinkToFit;

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

@override
Widget build(BuildContext context) {
final double width = MediaQuery.of(context).size.width;
final double width = shrinkToFit ? null : MediaQuery.of(context).size.width;

return Container(
padding: padding,
Expand All @@ -65,7 +68,7 @@ class Html extends StatelessWidget {
style: defaultTextStyle ?? DefaultTextStyle.of(context).style,
child: (useRichText)
? HtmlRichTextParser(
width: width,
shrinkToFit: shrinkToFit,
onLinkTap: onLinkTap,
renderNewlines: renderNewlines,
customEdgeInsets: customEdgeInsets,
Expand Down
4 changes: 1 addition & 3 deletions lib/html_parser.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import 'dart:convert';
import 'package:flutter_html/rich_text_parser.dart';

import 'image_properties.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/rich_text_parser.dart';
import 'package:html/dom.dart' as dom;
import 'package:html/parser.dart' as parser;

Expand Down
38 changes: 20 additions & 18 deletions lib/rich_text_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,31 +83,25 @@ class BlockText extends StatelessWidget {
final RichText child;
final EdgeInsets padding;
final EdgeInsets margin;
final String leadingChar;
final Decoration decoration;
final bool shrinkToFit;

BlockText({
@required this.child,
@required this.shrinkToFit,
this.padding,
this.margin,
this.leadingChar = '',
this.decoration,
});

@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
width: shrinkToFit ? null : double.infinity,
padding: this.padding,
margin: this.margin,
decoration: this.decoration,
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
leadingChar.isNotEmpty ? Text(leadingChar) : Container(),
Expanded(child: child),
],
),
child: child,
);
}
}
Expand Down Expand Up @@ -155,7 +149,7 @@ class ParseContext {

class HtmlRichTextParser extends StatelessWidget {
HtmlRichTextParser({
@required this.width,
this.shrinkToFit,
this.onLinkTap,
this.renderNewlines = false,
this.html,
Expand All @@ -175,7 +169,7 @@ class HtmlRichTextParser extends StatelessWidget {

final double indentSize = 10.0;

final double width;
final bool shrinkToFit;
final onLinkTap;
final bool renderNewlines;
final String html;
Expand Down Expand Up @@ -425,6 +419,7 @@ class HtmlRichTextParser extends StatelessWidget {
));
}
BlockText blockText = BlockText(
shrinkToFit: shrinkToFit,
margin: EdgeInsets.only(
top: 8.0,
bottom: 8.0,
Expand All @@ -438,8 +433,10 @@ class HtmlRichTextParser extends StatelessWidget {
);
parseContext.rootWidgetList.add(blockText);
} else {
parseContext.rootWidgetList
.add(BlockText(child: RichText(text: span)));
parseContext.rootWidgetList.add(BlockText(
child: RichText(text: span),
shrinkToFit: shrinkToFit,
));
}

// this allows future items to be added as children of this item
Expand Down Expand Up @@ -599,6 +596,7 @@ class HtmlRichTextParser extends StatelessWidget {
} else {
// start a new block element for this link and its text
BlockText blockElement = BlockText(
shrinkToFit: shrinkToFit,
margin: EdgeInsets.only(
left: parseContext.indentLevel * indentSize, top: 10.0),
child: RichText(text: span),
Expand Down Expand Up @@ -821,6 +819,7 @@ class HtmlRichTextParser extends StatelessWidget {
}
if (node.attributes['alt'] != null) {
parseContext.rootWidgetList.add(BlockText(
shrinkToFit: shrinkToFit,
margin:
EdgeInsets.symmetric(horizontal: 0.0, vertical: 10.0),
padding: EdgeInsets.all(0.0),
Expand All @@ -844,16 +843,18 @@ class HtmlRichTextParser extends StatelessWidget {
leadingChar = parseContext.listCount.toString() + '.';
}
BlockText blockText = BlockText(
shrinkToFit: shrinkToFit,
margin: EdgeInsets.only(
left: parseContext.indentLevel * indentSize, top: 3.0),
child: RichText(
text: TextSpan(
text: '',
style: nextContext.childStyle,
children: <TextSpan>[],
text: '$leadingChar ',
style: DefaultTextStyle.of(buildContext).style,
children: <TextSpan>[
TextSpan(text: '', style: nextContext.childStyle)
],
),
),
leadingChar: '$leadingChar ',
);
parseContext.rootWidgetList.add(blockText);
nextContext.parentElement = blockText.child.text;
Expand Down Expand Up @@ -914,6 +915,7 @@ class HtmlRichTextParser extends StatelessWidget {
));
}
BlockText blockText = BlockText(
shrinkToFit: shrinkToFit,
margin: node.localName != 'body'
? _customEdgeInsets ??
EdgeInsets.only(
Expand Down