Skip to content

Commit d715173

Browse files
committed
Fix inline rendering of (text) links
If non-text elements are used inside the <a> we still need widget spans and if they are long you can still have clunky wrapping, but I'm not even sure that is a bug per se. Fixes #388, #488 and #495
1 parent f9638ad commit d715173

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

lib/html_parser.dart

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:math';
33

44
import 'package:csslib/parser.dart' as cssparser;
55
import 'package:csslib/visitor.dart' as css;
6+
import 'package:flutter/gestures.dart';
67
import 'package:flutter/material.dart';
78
import 'package:flutter_html/flutter_html.dart';
89
import 'package:flutter_html/src/css_parser.dart';
@@ -329,28 +330,42 @@ class HtmlParser extends StatelessWidget {
329330
);
330331
}
331332
} else if (tree is InteractableElement) {
332-
return WidgetSpan(
333-
child: RawGestureDetector(
334-
gestures: {
335-
MultipleTapGestureRecognizer: GestureRecognizerFactoryWithHandlers<
336-
MultipleTapGestureRecognizer>(
337-
() => MultipleTapGestureRecognizer(),
338-
(instance) {
339-
instance..onTap = () => onLinkTap?.call(tree.href);
340-
},
341-
),
342-
},
343-
child: StyledText(
344-
textSpan: TextSpan(
345-
style: newContext.style.generateTextStyle(),
346-
children: tree.children
347-
.map((tree) => parseTree(newContext, tree))
348-
.toList() ??
349-
[],
350-
),
351-
style: newContext.style,
352-
),
353-
),
333+
return TextSpan(
334+
style: newContext.style.generateTextStyle(),
335+
children: tree.children
336+
.map((tree) => parseTree(newContext, tree))
337+
.map((childSpan) {
338+
if (childSpan is TextSpan) {
339+
return TextSpan(
340+
text: childSpan.text,
341+
children: childSpan.children,
342+
style: childSpan.style,
343+
semanticsLabel: childSpan.semanticsLabel,
344+
recognizer: TapGestureRecognizer()
345+
..onTap = () => onLinkTap?.call(tree.href),
346+
);
347+
} else {
348+
return WidgetSpan(
349+
child: RawGestureDetector(
350+
gestures: {
351+
MultipleTapGestureRecognizer:
352+
GestureRecognizerFactoryWithHandlers<
353+
MultipleTapGestureRecognizer>(
354+
() => MultipleTapGestureRecognizer(),
355+
(instance) {
356+
instance..onTap = () => onLinkTap?.call(tree.href);
357+
},
358+
),
359+
},
360+
child: StyledText(
361+
style: newContext.style,
362+
textSpan: childSpan,
363+
),
364+
),
365+
);
366+
}
367+
}).toList() ??
368+
[],
354369
);
355370
} else if (tree is LayoutElement) {
356371
return WidgetSpan(

0 commit comments

Comments
 (0)