Flutter_widget_from_html: Hope to add onImageTap function support

Created on 25 May 2020  路  4Comments  路  Source: daohoangson/flutter_widget_from_html

When the picture is small, hope to trigger onImageTap after clicking the picture.
Thanks.

question

Most helpful comment

Just FYI, the new version, 0.4.x and up, has a breaking change (no more HtmlWidgetConfig) so the sample code needs to be updated a bit:

class Issue190 extends StatelessWidget {
  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(title: Text('Issue 190')),
        body: SingleChildScrollView(
          child: HtmlWidget(
            kHtml,
            factoryBuilder: () => _MyWidgetFactory(),
          ),
        ),
      );
}

class _MyWidgetFactory extends WidgetFactory {
  @override
  Widget buildImage(String url, {double height, String text, double width}) {
    final imageWidget = super.buildImage(
      url,
      height: height,
      text: text,
      width: width,
    );
    if (imageWidget == null) return imageWidget;

    return GestureDetector(
      child: imageWidget,
      onTap: () => print(url),
    );
  }
}

All 4 comments

I don't think this will be added to the package anytime soon. But thanks to the easy extensibility, you can do this in your app fairly easy. Checkout this sample code:


class Issue190 extends StatelessWidget {
  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(title: Text('Issue 190')),
        body: SingleChildScrollView(
          child: HtmlWidget(
            kHtml,
            factoryBuilder: (c) => _MyWidgetFactory(c),
          ),
        ),
      );
}

class _MyWidgetFactory extends WidgetFactory {
  _MyWidgetFactory(HtmlWidgetConfig config) : super(config);

  @override
  Widget buildImage(String url, {double height, String text, double width}) {
    final imageWidget = super.buildImage(
      url,
      height: height,
      text: text,
      width: width,
    );
    if (imageWidget == null) return imageWidget;

    return GestureDetector(
      child: imageWidget,
      onTap: () => print(url),
    );
  }
}

Basically you will need to use a custom WidgetFactory. In your subclass, override the buildImage method and wrap a GestureDetector to capture the onTap event. You can do whatever you want when user taps the image, I only printed the image URL.

This is the effect i want , thank you very much.馃帀

Glad to be of help. I'm closing this issue now btw, feel free to reopen or create a new one if you run into other problems.

Just FYI, the new version, 0.4.x and up, has a breaking change (no more HtmlWidgetConfig) so the sample code needs to be updated a bit:

class Issue190 extends StatelessWidget {
  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(title: Text('Issue 190')),
        body: SingleChildScrollView(
          child: HtmlWidget(
            kHtml,
            factoryBuilder: () => _MyWidgetFactory(),
          ),
        ),
      );
}

class _MyWidgetFactory extends WidgetFactory {
  @override
  Widget buildImage(String url, {double height, String text, double width}) {
    final imageWidget = super.buildImage(
      url,
      height: height,
      text: text,
      width: width,
    );
    if (imageWidget == null) return imageWidget;

    return GestureDetector(
      child: imageWidget,
      onTap: () => print(url),
    );
  }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

leewonghao picture leewonghao  路  7Comments

yerzhant picture yerzhant  路  7Comments

ahmadkhedr picture ahmadkhedr  路  7Comments

shofi-ishraak picture shofi-ishraak  路  6Comments

ahmadkhedr picture ahmadkhedr  路  6Comments