Skip to content

Commit

Permalink
Merge pull request #695 from tneotia/bugfix/link-and-image-taps
Browse files Browse the repository at this point in the history
Add less hacky workaround for nested link and image tap detection
  • Loading branch information
erickok authored May 22, 2021
2 parents a226c49 + b3165c3 commit 1133dc9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
27 changes: 11 additions & 16 deletions lib/html_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -409,22 +409,17 @@ class HtmlParser extends StatelessWidget {
);
} else {
return WidgetSpan(
child: RawGestureDetector(
key: AnchorKey.of(key, tree),
gestures: {
MultipleTapGestureRecognizer:
GestureRecognizerFactoryWithHandlers<
MultipleTapGestureRecognizer>(
() => MultipleTapGestureRecognizer(),
(instance) {
instance
..onTap = _onAnchorTap != null
? () => _onAnchorTap!(tree.href, context, tree.attributes, tree.element)
: null;
},
),
},
child: (childSpan as WidgetSpan).child,
child: MultipleTapGestureDetector(
onTap: _onAnchorTap != null
? () => _onAnchorTap!(tree.href, context, tree.attributes, tree.element)
: null,
child: GestureDetector(
key: AnchorKey.of(key, tree),
onTap: _onAnchorTap != null
? () => _onAnchorTap!(tree.href, context, tree.attributes, tree.element)
: null,
child: (childSpan as WidgetSpan).child,
),
),
);
}
Expand Down
21 changes: 12 additions & 9 deletions lib/src/replaced_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,19 @@ class ImageContentElement extends ReplacedElement {
for (final entry in context.parser.imageRenders.entries) {
if (entry.key.call(attributes, element)) {
final widget = entry.value.call(context, attributes, element);
return RawGestureDetector(
key: AnchorKey.of(context.parser.key, this),
child: widget,
gestures: {
MultipleTapGestureRecognizer: GestureRecognizerFactoryWithHandlers<MultipleTapGestureRecognizer>(
() => MultipleTapGestureRecognizer(), (instance) {
instance..onTap = () => context.parser.onImageTap?.call(src, context, attributes, element);
return Builder(
builder: (buildContext) {
return GestureDetector(
key: AnchorKey.of(context.parser.key, this),
child: widget,
onTap: () {
if (MultipleTapGestureDetector.of(buildContext) != null) {
MultipleTapGestureDetector.of(buildContext)!.onTap?.call();
}
context.parser.onImageTap?.call(src, context, attributes, element);
},
),
},
);
}
);
}
}
Expand Down
18 changes: 14 additions & 4 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,21 @@ class Context<T> {

// This class is a workaround so that both an image
// and a link can detect taps at the same time.
class MultipleTapGestureRecognizer extends TapGestureRecognizer {
@override
void rejectGesture(int pointer) {
acceptGesture(pointer);
class MultipleTapGestureDetector extends InheritedWidget {
final void Function()? onTap;

const MultipleTapGestureDetector({
Key? key,
required Widget child,
required this.onTap,
}) : super(key: key, child: child);

static MultipleTapGestureDetector? of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<MultipleTapGestureDetector>();
}

@override
bool updateShouldNotify(MultipleTapGestureDetector oldWidget) => false;
}

class CustomBorderSide {
Expand Down

0 comments on commit 1133dc9

Please sign in to comment.