# flutter_html **Repository Path**: xiaofan232/flutter_html ## Basic Information - **Project Name**: flutter_html - **Description**: A Flutter widget for rendering static html as Flutter widgets (Will render over 80 different html tags!) - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2021-03-04 - **Last Updated**: 2021-04-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # flutter_html [![pub package](https://img.shields.io/pub/v/flutter_html.svg)](https://pub.dev/packages/flutter_html) [![codecov](https://codecov.io/gh/Sub6Resources/flutter_html/branch/master/graph/badge.svg)](https://codecov.io/gh/Sub6Resources/flutter_html) [![CircleCI](https://circleci.com/gh/Sub6Resources/flutter_html.svg?style=svg)](https://circleci.com/gh/Sub6Resources/flutter_html) [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://github.com/Sub6Resources/flutter_html/blob/master/LICENSE) A Flutter widget for rendering HTML and CSS as Flutter widgets.
Screenshot 1 Screenshot 2 Screenshot 3
A Screenshot of flutter_html Another Screenshot of flutter_html Yet another Screenshot of flutter_html
## Table of Contents: - [Installing](#installing) - [Currently Supported HTML Tags](#currently-supported-html-tags) - [Currently Supported CSS Attributes](#currently-supported-css-attributes) - [Why flutter_html?](#why-this-package) - [API Reference](#api-reference) - [Parameters Table](#parameters) - [Data](#data) - [onLinkTap](#onlinktap) - [customRender](#customrender) - [onImageError](#onimageerror) - [onImageTap](#onimagetap) - [blacklistedElements](#blacklistedelements) - [style](#style) - [navigationDelegateForIframe](#navigationdelegateforiframe) - [customImageRender](#customimagerender) - [typedef ImageSourceMatcher (with examples)](#typedef-imagesourcematcher) - [typedef ImageRender (with examples)](#typedef-imagerender) - [Extended examples](#example-usages---customimagerender) - [Rendering Reference](#rendering-reference) - [Image](#image) - [Iframe](#iframe) - [Audio](#audio) - [Video](#video) - [SVG](#svg) - [Table](#table) - [Notes](#notes) - [Migration Guide](#migration-guides) - [Contribution Guide](#contribution-guide) ## Installing: Add the following to your `pubspec.yaml` file: dependencies: flutter_html: ^1.3.0 ## Currently Supported HTML Tags: | | | | | | | | | | | | |------------|-----------|-------|-------------|---------|---------|-------|------|--------|--------|--------| |`a` | `abbr` | `acronym`| `address` | `article`| `aside` | `audio`| `b` | `bdi` | `bdo` | `big` | |`blockquote`| `body` | `br` | `caption` | `cite` | `code` | `data`| `dd` | `del` | `details` | `dfn` | | `div` | `dl` | `dt` | `em` | `figcaption`| `figure`| `footer`| `h1` | `h2` | `h3` | `h4` | | `h5` |`h6` | `header` | `hr` | `i` | `iframe`| `img` | `ins` | `kbd`| `li` | `main` | | `mark` | `nav` | `noscript`|`ol` | `p` | `pre` | `q` | `rp` | `rt` | `ruby` | `s` | | `samp` | `section` | `small` | `span`| `strike` | `strong`| `sub` | `sup` | `summary` | `svg`| `table`| | `tbody` | `td` | `template` | `tfoot` | `th` | `thead` |`time` | `tr` | `tt` | `u` | `ul` | | `var` | `video` | | | | | | | | | | ## Currently Supported CSS Attributes: | | | | | | | | |------------------|--------|------------|----------|--------------|------------------------|------------| |`background-color`| `color`| `direction`| `display`| `font-family`| `font-feature-settings`| `font-size`| |`font-style` | `font-weight`| `height` | `letter-spacing`| `line-height`| `list-style-type` | `list-style-position`| |`padding` | `margin`| `text-align`| `text-decoration`| `text-decoration-color`| `text-decoration-style`| `text-decoration-thickness`| |`text-shadow` | `vertical-align`| `white-space`| `width` | `word-spacing`| | | Don't see a tag or attribute you need? File a feature request or contribute to the project! ## Why this package? This package is designed with simplicity in mind. Originally created to allow basic rendering of HTML content into the Flutter widget tree, this project has expanded to include support for basic styling as well! If you need something more robust and customizable, the package also provides a number of optional custom APIs for extremely granular control over widget rendering! ## API Reference: For the full API reference, see [here](https://pub.dev/documentation/flutter_html/latest/). For a full example, see [here](https://github.com/Sub6Resources/flutter_html/tree/master/example). Below, you will find brief descriptions of the parameters the`Html` widget accepts and some code snippets to help you use this package. ### Parameters: | Parameters | Description | |--------------|-----------------| | `data` | The HTML data passed to the `Html` widget. This is required and cannot be null. | | `onLinkTap` | A function that defines what the widget should do when a link is tapped. The function exposes the `src` of the link as a `String` to use in your implementation. | | `customRender` | A powerful API that allows you to customize everything when rendering a specific HTML tag. | | `onImageError` | A function that defines what the widget should do when an image fails to load. The function exposes the exception `Object` and `StackTrace` to use in your implementation. | | `shrinkWrap` | A `bool` used while rendering different widgets to specify whether they should be shrink-wrapped or not, like `ContainerSpan` | | `onImageTap` | A function that defines what the widget should do when an image is tapped. The function exposes the `src` of the image as a `String` to use in your implementation. | | `blacklistedElements` | A list of elements the `Html` widget should not render. The list should contain the tags of the HTML elements you wish to blacklist. | | `style` | A powerful API that allows you to customize the style that should be used when rendering a specific HTMl tag. | | `navigationDelegateForIframe` | Allows you to set the `NavigationDelegate` for the `WebView`s of all the iframes rendered by the `Html` widget. | | `customImageRender` | A powerful API that allows you to fully customize how images are loaded. | ### Data: The HTML data passed to the `Html` widget as a `String`. This is required and cannot be null. Any HTML tags in the `String` that are not supported by the package will not be rendered. #### Example Usage - Data: ```dart Widget html = Html( data: """

Demo Page

This is a fantastic product that you should buy!

Features

""", ); ``` ### onLinkTap: A function that defines what the widget should do when a link is tapped. #### Example Usage - onLinkTap: ```dart Widget html = Html( data: """

Linking to websites has never been easier.

""", onLinkTap: (String url) { //open URL in webview, or launch URL in browser, or any other logic here } ); ``` ### customRender: A powerful API that allows you to customize everything when rendering a specific HTML tag. This means you can add support for HTML elements that aren't supported natively. You can also make up your own custom tags in your HTML! `customRender` accepts a `Map`. The `CustomRender` type is a function that requires a `Widget` to be returned. It exposes `RenderContext`, the `Widget` that would have been rendered by `Html` without a `customRender` defined, the `attributes` of the HTML element as a `Map`, and the HTML element itself as `Element`. To use this API, set the key as the tag of the HTML element you wish to provide a custom implementation for, and create a function with the above parameters that returns a `Widget`. #### Example Usages - customRender: 1. Simple example - rendering custom HTML tags
View code ```dart Widget html = Html( data: """

Display bird element and flutter element

""", customRender: { "bird": (RenderContext context, Widget child, Map attributes, _) { return TextSpan(text: "🐦"); }, "flutter": (RenderContext context, Widget child, Map attributes, _) { return FlutterLogo( style: (attributes['horizontal'] != null) ? FlutterLogoStyle.horizontal : FlutterLogoStyle.markOnly, textColor: context.style.color, size: context.style.fontSize.size * 5, ); }, }, ); ```
2. Complex example - rendering an `iframe` differently based on whether it is an embedded youtube video or some other embedded content
View code ```dart Widget html = Html( data: """

Google iframe:

YouTube iframe:

""", customRender: { "iframe": (RenderContext context, Widget child, Map attributes, _) { if (attributes != null) { double width = double.tryParse(attributes['width'] ?? ""); double height = double.tryParse(attributes['height'] ?? ""); print(attributes['src']); return Container( width: width ?? (height ?? 150) * 2, height: height ?? (width ?? 300) / 2, child: WebView( initialUrl: attributes['src'], javascriptMode: JavascriptMode.unrestricted, //no need for scrolling gesture recognizers on embedded youtube, so set gestureRecognizers null //on other iframe content scrolling might be necessary, so use VerticalDragGestureRecognizer gestureRecognizers: attributes['src'].contains("youtube.com/embed") ? null : [ Factory(() => VerticalDragGestureRecognizer()) ].toSet(), navigationDelegate: (NavigationRequest request) async { //no need to load any url besides the embedded youtube url when displaying embedded youtube, so prevent url loading //on other iframe content allow all url loading if (attributes['src'].contains("youtube.com/embed")) { if (!request.url.contains("youtube.com/embed")) { return NavigationDecision.prevent; } else { return NavigationDecision.navigate; } } else { return NavigationDecision.navigate; } }, ), ); } else { return Container(height: 0); } } } ); ```
More example usages and in-depth details available [here](https://github.com/Sub6Resources/flutter_html/wiki/All-About-customRender). ### onImageError: A function that defines what the widget should do when an image fails to load. The function exposes the exception `Object` and `StackTrace` to use in your implementation. #### Example Usage - onImageError: ```dart Widget html = Html( data: """Alt Text of an intentionally broken image""", onImageError: (Exception exception, StackTrace stackTrace) { FirebaseCrashlytics.instance.recordError(exception, stackTrace); }, ); ``` ### onImageTap: A function that defines what the widget should do when an image is tapped. #### Example Usage - onImageTap: ```dart Widget html = Html( data: """Google""", onImageTap: (String url) { //open image in webview, or launch image in browser, or any other logic here } ); ``` ### blacklistedElements: A list of elements the `Html` widget should not render. The list should contain the tags of the HTML elements you wish to blacklist. #### Example Usage - blacklistedElements: You may have instances where you can choose between two different types of HTML tags to display the same content. In the example below, the `