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

How add tap listener to img and url #50

Closed
ykrank opened this issue Jun 11, 2019 · 6 comments
Closed

How add tap listener to img and url #50

ykrank opened this issue Jun 11, 2019 · 6 comments
Assignees
Labels
question Further information is requested

Comments

@ykrank
Copy link

ykrank commented Jun 11, 2019

I know use onTapUrl to handle url tap. but now i need separate img and url tap listener. could i extend flutter_widget_from_html? or i must extend flutter_widget_from_html_core and copy this project code to my project?

@daohoangson
Copy link
Owner

What is your use case? How is your html look like? Do you have IMG inside A like this?

<a href="xxx"><img src="xxx" /></a>
<a href="xxx">XXX</a>

Or your IMG is standalone?

<img src="xxx" />
<a href="xxx">XXX</a>

In both cases, it should be possible to implement without copying the whole plugin code.

@daohoangson daohoangson self-assigned this Jun 11, 2019
@daohoangson daohoangson added the question Further information is requested label Jun 11, 2019
@ykrank
Copy link
Author

ykrank commented Jun 11, 2019

What is your use case? How is your html look like? Do you have IMG inside A like this?

<a href="xxx"><img src="xxx" /></a>
<a href="xxx">XXX</a>

Or your IMG is standalone?

<img src="xxx" />
<a href="xxx">XXX</a>

In both cases, it should be possible to implement without copying the whole plugin code.

IMG tag is standalone. how to implement img click?

@daohoangson
Copy link
Owner

Ah, standalone is easier. Something like this should work:

import 'package:flutter/material.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';

const html = """
<img src="https://media.giphy.com/media/6VoDJzfRjJNbG/giphy-downsized.gif" />
<a href="https://flutter.io">flutter.io</a>
""";

class ImgScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: Text('ImgScreen'),
        ),
        body: HtmlWidget(
          html,
          builderCallback: (meta, e) {
            if (e.localName == 'img' && e.attributes.containsKey('src')) {
              meta = lazySet(meta, buildOp: imgOnTap(e.attributes['src']));
            }

            return meta;
          },
          onTapUrl: (url) => debugPrint("onTapUrl: $url"),
        ),
      );

  BuildOp imgOnTap(String src) => BuildOp(
        // the default `BuildOp` for IMG has a priority of 10
        // and it will prepare `Image` widget for our `onWidgets` callback
        // this op needs a larger number to run after the default
        priority: 9999,
        // wrap each `Image` widget inside a `GestureDetector` to capture `onTap`
        onWidgets: (_, widgets) => widgets.map((widget) => GestureDetector(
              child: widget,
              onTap: () => debugPrint("IMG: $src"),
            )),
      );
}

@ykrank
Copy link
Author

ykrank commented Jun 12, 2019

lazySet

Thanks.

@ykrank ykrank closed this as completed Jun 12, 2019
@sunilbcit
Copy link

With flutter_widget_from_html: ^0.4.1, builderCallBack and lazyset removed.
How to achieve the same now.

@daohoangson
Copy link
Owner

@sunilbcit I have opened a new issue for your question, see it here #396

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants