Skip to content

Commit 1133dc9

Browse files
authored
Merge pull request #695 from tneotia/bugfix/link-and-image-taps
Add less hacky workaround for nested link and image tap detection
2 parents a226c49 + b3165c3 commit 1133dc9

File tree

3 files changed

+37
-29
lines changed

3 files changed

+37
-29
lines changed

lib/html_parser.dart

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -409,22 +409,17 @@ class HtmlParser extends StatelessWidget {
409409
);
410410
} else {
411411
return WidgetSpan(
412-
child: RawGestureDetector(
413-
key: AnchorKey.of(key, tree),
414-
gestures: {
415-
MultipleTapGestureRecognizer:
416-
GestureRecognizerFactoryWithHandlers<
417-
MultipleTapGestureRecognizer>(
418-
() => MultipleTapGestureRecognizer(),
419-
(instance) {
420-
instance
421-
..onTap = _onAnchorTap != null
422-
? () => _onAnchorTap!(tree.href, context, tree.attributes, tree.element)
423-
: null;
424-
},
425-
),
426-
},
427-
child: (childSpan as WidgetSpan).child,
412+
child: MultipleTapGestureDetector(
413+
onTap: _onAnchorTap != null
414+
? () => _onAnchorTap!(tree.href, context, tree.attributes, tree.element)
415+
: null,
416+
child: GestureDetector(
417+
key: AnchorKey.of(key, tree),
418+
onTap: _onAnchorTap != null
419+
? () => _onAnchorTap!(tree.href, context, tree.attributes, tree.element)
420+
: null,
421+
child: (childSpan as WidgetSpan).child,
422+
),
428423
),
429424
);
430425
}

lib/src/replaced_element.dart

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,19 @@ class ImageContentElement extends ReplacedElement {
8383
for (final entry in context.parser.imageRenders.entries) {
8484
if (entry.key.call(attributes, element)) {
8585
final widget = entry.value.call(context, attributes, element);
86-
return RawGestureDetector(
87-
key: AnchorKey.of(context.parser.key, this),
88-
child: widget,
89-
gestures: {
90-
MultipleTapGestureRecognizer: GestureRecognizerFactoryWithHandlers<MultipleTapGestureRecognizer>(
91-
() => MultipleTapGestureRecognizer(), (instance) {
92-
instance..onTap = () => context.parser.onImageTap?.call(src, context, attributes, element);
86+
return Builder(
87+
builder: (buildContext) {
88+
return GestureDetector(
89+
key: AnchorKey.of(context.parser.key, this),
90+
child: widget,
91+
onTap: () {
92+
if (MultipleTapGestureDetector.of(buildContext) != null) {
93+
MultipleTapGestureDetector.of(buildContext)!.onTap?.call();
94+
}
95+
context.parser.onImageTap?.call(src, context, attributes, element);
9396
},
94-
),
95-
},
97+
);
98+
}
9699
);
97100
}
98101
}

lib/src/utils.dart

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,21 @@ class Context<T> {
4848

4949
// This class is a workaround so that both an image
5050
// and a link can detect taps at the same time.
51-
class MultipleTapGestureRecognizer extends TapGestureRecognizer {
52-
@override
53-
void rejectGesture(int pointer) {
54-
acceptGesture(pointer);
51+
class MultipleTapGestureDetector extends InheritedWidget {
52+
final void Function()? onTap;
53+
54+
const MultipleTapGestureDetector({
55+
Key? key,
56+
required Widget child,
57+
required this.onTap,
58+
}) : super(key: key, child: child);
59+
60+
static MultipleTapGestureDetector? of(BuildContext context) {
61+
return context.dependOnInheritedWidgetOfExactType<MultipleTapGestureDetector>();
5562
}
63+
64+
@override
65+
bool updateShouldNotify(MultipleTapGestureDetector oldWidget) => false;
5666
}
5767

5868
class CustomBorderSide {

0 commit comments

Comments
 (0)