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

Add support for custom text alignment #112

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions lib/flutter_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Html extends StatelessWidget {
this.customRender,
this.customEdgeInsets,
this.customTextStyle,
this.customTextAlign,
this.blockSpacing = 14.0,
this.useRichText = true,
this.onImageError,
Expand Down Expand Up @@ -49,6 +50,7 @@ class Html extends StatelessWidget {
final CustomRender customRender;
final CustomEdgeInsets customEdgeInsets;
final CustomTextStyle customTextStyle;
final CustomTextAlign customTextAlign;

@override
Widget build(BuildContext context) {
Expand All @@ -67,6 +69,7 @@ class Html extends StatelessWidget {
renderNewlines: renderNewlines,
customEdgeInsets: customEdgeInsets,
customTextStyle: customTextStyle,
customTextAlign: customTextAlign,
html: data,
onImageError: onImageError,
linkStyle: linkStyle,
Expand Down
10 changes: 8 additions & 2 deletions lib/html_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ typedef CustomTextStyle = TextStyle Function(
dom.Node node,
TextStyle baseStyle,
);
typedef CustomTextAlign = TextAlign Function(dom.Element elem);
typedef CustomEdgeInsets = EdgeInsets Function(dom.Node node);
typedef OnLinkTap = void Function(String url);
typedef OnImageTap = void Function();
Expand Down Expand Up @@ -150,8 +151,9 @@ class HtmlRichTextParser extends StatelessWidget {
this.onLinkTap,
this.renderNewlines = false,
this.html,
this.customTextStyle,
this.customEdgeInsets,
this.customTextStyle,
this.customTextAlign,
this.onImageError,
this.linkStyle = const TextStyle(
decoration: TextDecoration.underline,
Expand All @@ -169,8 +171,9 @@ class HtmlRichTextParser extends StatelessWidget {
final onLinkTap;
final bool renderNewlines;
final String html;
final CustomTextStyle customTextStyle;
final CustomEdgeInsets customEdgeInsets;
final CustomTextStyle customTextStyle;
final CustomTextAlign customTextAlign;
final ImageErrorListener onImageError;
final TextStyle linkStyle;
final ImageProperties imageProperties;
Expand Down Expand Up @@ -696,6 +699,9 @@ class HtmlRichTextParser extends StatelessWidget {
// so if we have a block element, reset the parentElement to null
parseContext.parentElement = null;
TextAlign textAlign = TextAlign.left;
if (customTextAlign != null) {
textAlign = customTextAlign(node) ?? textAlign;
}

EdgeInsets _customEdgeInsets;
if (customEdgeInsets != null) {
Expand Down