flutter_widget_from_html_core 0.1.5

  • README.md
  • Example
  • Installing
  • Versions
  • 90

Flutter Widget from HTML (core) #

CircleCI codecov Pub

A Flutter plugin for building Flutter-widget tree from html.

This core package implements html parsing and widget building logic so it's easy to extend and fit your app's use case. It tries to render an optimal tree: use Text instead of RichText as much as possible, merge text spans together, show images in AspectRatio, etc.

If this is your first time here, consider using the flutter_widget_from_html package as a quick starting point.

Usage #

To use this plugin, add flutter_widget_from_html_core as a dependency in your pubspec.yaml file.

See the Example app for inspiration.

Example #

class HelloWorldCoreScreen extends StatelessWidget {
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: Text('HelloWorldCoreScreen'),
        body: HtmlWidget("""<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<h4>Heading 4</h4>
<h5>Heading 5</h5>
<h6>Heading 6</h6>
<p>A paragraph with <strong>strong</strong> <em>emphasized</em> text.</p>
<p>And of course, cat image: <img src="https://media.giphy.com/media/6VoDJzfRjJNbG/giphy-downsized.gif" /></p>
<div style="text-align: center">Source: <a href="https://gph.is/QFgPA0">https://gph.is/QFgPA0</a></div>

Features #

HTML tags #

Below tags are the ones that have special meaning / styling, all other tags will be parsed as text.

However, these tags and their contents will be ignored:

Inline stylings #

  • color: hex values only (#F00, #0F08, #00FF00 or #00FF0080)
  • font-family
  • font-size (value in px only)
  • font-style: italic/normal
  • font-weight: bold/normal/100..900
  • margin, margin-top, margin-right, margin-bottom, margin-left (values in px only)
  • text-align: center/justify/left/right
  • text-decoration: line-through/none/overline/underline

Extensibility #

As previously mentioned, this package focuses on the core parsing-building routine with lots of tests to make sure it works correctly. If the flutter_widget_from_html package does not suite your need or you don't like any of the dependencies included in that package, it's time to extend flutter_widget_from_html_core.

Here is how it works:

  1. HtmlWidget parses input html into dom nodes
  2. Builder loops through each node, looking for NodeMetadata (text size, styling, hyperlink or image source, etc.)
  3. Use the metadata to build widget via WidgetFactory

If you want to, you can change the way metadata is collected (in step 2) and build widget however you like (in step 3) by extending the WidgetFactory and give it to HtmlWidget. The example below replace smilie inline image with an emoji:

class SmilieScreen extends StatelessWidget {
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: Text('SmilieScreen'),
        body: HtmlWidget(
          '<p>Hello <img class="smilie smilie-1" alt=":)" src="http://domain.com/sprites.png" />!</p>',
          wfBuilder: (context) => SmilieWf(context),

const _kSmilies = {':)': '🙂'};

class SmilieWf extends WidgetFactory {
  SmilieWf(BuildContext context) : super(context);

  NodeMetadata parseElement(dom.Element e) {
    var meta = super.parseElement(e);

    if (e.classes.contains('smilie') && e.attributes.containsKey('alt')) {
      final alt = e.attributes['alt'];
      // render alt text if mapping not found
      // because inline image is not supported
      final text = _kSmilies.containsKey(alt) ? _kSmilies[alt] : alt;
      meta = lazySet(
        buildOp: BuildOp(
          onProcess: (_, __, write) => write(text),

    return meta;

0.1.5 #

  • Fix margin with partial invalid values (#21)

0.1.4 #

  • Update dependencies (#12)
  • Fix layout rebuild loop because of Column's UniqueKey (#19)

0.1.3 #

  • Fix bug stylings got lost during block rendering (#10)

0.1.2 #

  • Fix bug rendering overlapping elements with styling (#11)
  • Expand CSS color hex values support

0.1.1 #

  • Bug fixes
  • Add support for BuildOp, making it easier to render new html tags
  • Add support for margin inline styling

0.0.1 #

  • First release


example #

See the full Example app for inspiration.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  flutter_widget_from_html_core: ^0.1.5

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart';
Version Uploaded Documentation Archive
0.1.5 Apr 14, 2019 Go to the documentation of flutter_widget_from_html_core 0.1.5 Download flutter_widget_from_html_core 0.1.5 archive
0.1.4 Apr 1, 2019 Go to the documentation of flutter_widget_from_html_core 0.1.4 Download flutter_widget_from_html_core 0.1.4 archive
0.1.3 Mar 5, 2019 Go to the documentation of flutter_widget_from_html_core 0.1.3 Download flutter_widget_from_html_core 0.1.3 archive
0.1.2 Mar 5, 2019 Go to the documentation of flutter_widget_from_html_core 0.1.2 Download flutter_widget_from_html_core 0.1.2 archive
0.1.1 Feb 9, 2019 Go to the documentation of flutter_widget_from_html_core 0.1.1 Download flutter_widget_from_html_core 0.1.1 archive
0.1.0 Oct 19, 2018 Go to the documentation of flutter_widget_from_html_core 0.1.0 Download flutter_widget_from_html_core 0.1.0 archive
0.0.2 Oct 19, 2018 Go to the documentation of flutter_widget_from_html_core 0.0.2 Download flutter_widget_from_html_core 0.0.2 archive
0.0.1 Oct 18, 2018 Go to the documentation of flutter_widget_from_html_core 0.0.1 Download flutter_widget_from_html_core 0.0.1 archive
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Apr 16, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.2.0
  • pana: 0.12.14
  • Flutter: 1.4.7


Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health issues and suggestions

Document public APIs. (-0.73 points)

111 out of 112 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Format lib/src/core_wf.dart.

Run flutter format to format lib/src/core_wf.dart.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
html ^0.13.4 0.13.4+2 0.14.0+1
Transitive dependencies
charcode 1.1.2
collection 1.14.11
csslib 0.15.0
meta 1.1.6 1.1.7
path 1.6.2
sky_engine 0.0.99
source_span 1.5.5
term_glyph 1.1.0
typed_data 1.1.6
utf 0.9.0+5
vector_math 2.0.8
Dev dependencies