Skip to content

Commit f869088

Browse files
committed
content [nfc]: Track recognizer on inline tree, apply to text spans
At this stage the recognizer is always null and so this doesn't do anything. But it provides us the infrastructure with which to start recognizing taps on links, #71.
1 parent 91e202a commit f869088

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

lib/widgets/content.dart

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:flutter/gestures.dart';
12
import 'package:flutter/material.dart';
23
import 'package:html/dom.dart' as dom;
34

@@ -302,7 +303,7 @@ Widget _buildBlockInlineContainer({
302303
required BlockInlineContainerNode node,
303304
}) {
304305
if (node.links == null) {
305-
return InlineContent(style: style, nodes: node.nodes);
306+
return InlineContent(recognizer: null, style: style, nodes: node.nodes);
306307
}
307308
return _BlockInlineContainer(
308309
links: node.links!, style: style, nodes: node.nodes);
@@ -323,21 +324,24 @@ class _BlockInlineContainer extends StatefulWidget {
323324
class _BlockInlineContainerState extends State<_BlockInlineContainer> {
324325
@override
325326
Widget build(BuildContext context) {
326-
return InlineContent(style: widget.style, nodes: widget.nodes);
327+
return InlineContent(recognizer: null,
328+
style: widget.style, nodes: widget.nodes);
327329
}
328330
}
329331

330332
class InlineContent extends StatelessWidget {
331333
InlineContent({
332334
super.key,
333-
required this.nodes,
335+
required this.recognizer,
334336
required this.style,
337+
required this.nodes,
335338
}) {
336339
_builder = _InlineContentBuilder(this);
337340
}
338341

339-
final List<InlineContentNode> nodes;
342+
final GestureRecognizer? recognizer;
340343
final TextStyle? style;
344+
final List<InlineContentNode> nodes;
341345

342346
late final _InlineContentBuilder _builder;
343347

@@ -348,14 +352,21 @@ class InlineContent extends StatelessWidget {
348352
}
349353

350354
class _InlineContentBuilder {
351-
_InlineContentBuilder(this.widget);
355+
_InlineContentBuilder(this.widget) : _recognizer = widget.recognizer;
352356

353357
final InlineContent widget;
354358

355359
InlineSpan build() {
356360
return _buildNodes(widget.nodes, style: widget.style);
357361
}
358362

363+
// Why do we have to track `recognizer` here, rather than apply it
364+
// once at the top of the affected span? Because the events don't bubble
365+
// within a paragraph:
366+
// https://github.com/flutter/flutter/issues/10623
367+
// https://github.com/flutter/flutter/issues/10623#issuecomment-308030170
368+
final GestureRecognizer? _recognizer;
369+
359370
InlineSpan _buildNodes(List<InlineContentNode> nodes, {required TextStyle? style}) {
360371
return TextSpan(
361372
style: style,
@@ -364,7 +375,7 @@ class _InlineContentBuilder {
364375

365376
InlineSpan _buildNode(InlineContentNode node) {
366377
if (node is TextNode) {
367-
return TextSpan(text: node.text);
378+
return TextSpan(text: node.text, recognizer: _recognizer);
368379
} else if (node is LineBreakInlineNode) {
369380
// Each `<br/>` is followed by a newline, which browsers apparently ignore
370381
// and our parser doesn't. So don't do anything here.
@@ -401,7 +412,7 @@ class _InlineContentBuilder {
401412
style: const TextStyle(fontStyle: FontStyle.italic));
402413

403414
InlineSpan _buildLink(LinkNode node) {
404-
// TODO make link touchable
415+
// TODO make link touchable by setting _recognizer
405416
return _buildNodes(node.nodes,
406417
style: TextStyle(color: const HSLColor.fromAHSL(1, 200, 1, 0.4).toColor()));
407418
}
@@ -497,7 +508,11 @@ class UserMention extends StatelessWidget {
497508
return Container(
498509
decoration: _kDecoration,
499510
padding: const EdgeInsets.symmetric(horizontal: 0.2 * kBaseFontSize),
500-
child: InlineContent(nodes: node.nodes, style: null));
511+
child: InlineContent(
512+
// If an @-mention is inside a link, let the @-mention override it.
513+
recognizer: null, // TODO make @-mentions tappable, for info on user
514+
style: null,
515+
nodes: node.nodes));
501516
}
502517

503518
static get _kDecoration => BoxDecoration(

0 commit comments

Comments
 (0)