Skip to content
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

Add less hacky workaround for nested link and image tap detection #695

Merged
merged 1 commit into from
May 22, 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
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