Skip to content

Fix inline rendering of (text) links #499

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

Merged
merged 4 commits into from
Jan 18, 2021
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
5 changes: 3 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ const htmlData = """
</p>
<h3>Image support:</h3>
<p>
<img alt='Google' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png' />
<a href='https://google.com'><img alt='Google' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png' /></a>
<img alt='Google' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png' /><br />
<a href='https://google.com'>A linked image: <img alt='Google' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png' /></a>
<img alt='Alt Text of an intentionally broken image' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30d' />
</p>
<h3>Video support:</h3>
<video controls>
Expand Down
56 changes: 34 additions & 22 deletions lib/html_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:math';

import 'package:csslib/parser.dart' as cssparser;
import 'package:csslib/visitor.dart' as css;
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_html/src/css_parser.dart';
Expand Down Expand Up @@ -335,28 +336,39 @@ class HtmlParser extends StatelessWidget {
);
}
} else if (tree is InteractableElement) {
return WidgetSpan(
child: RawGestureDetector(
gestures: {
MultipleTapGestureRecognizer: GestureRecognizerFactoryWithHandlers<
MultipleTapGestureRecognizer>(
() => MultipleTapGestureRecognizer(),
(instance) {
instance..onTap = () => onLinkTap?.call(tree.href);
},
),
},
child: StyledText(
textSpan: TextSpan(
style: newContext.style.generateTextStyle(),
children: tree.children
.map((tree) => parseTree(newContext, tree))
.toList() ??
[],
),
style: newContext.style,
),
),
return TextSpan(
children: tree.children
.map((tree) => parseTree(newContext, tree))
.map((childSpan) {
if (childSpan is TextSpan) {
return TextSpan(
text: childSpan.text,
children: childSpan.children,
style: (childSpan.style ?? TextStyle())
.merge(newContext.style.generateTextStyle()),
semanticsLabel: childSpan.semanticsLabel,
recognizer: TapGestureRecognizer()
..onTap = () => onLinkTap?.call(tree.href),
);
} else {
return WidgetSpan(
child: RawGestureDetector(
gestures: {
MultipleTapGestureRecognizer:
GestureRecognizerFactoryWithHandlers<
MultipleTapGestureRecognizer>(
() => MultipleTapGestureRecognizer(),
(instance) {
instance..onTap = () => onLinkTap?.call(tree.href);
},
),
},
child: (childSpan as WidgetSpan).child,
),
);
}
}).toList() ??
[],
);
} else if (tree is LayoutElement) {
return WidgetSpan(
Expand Down