flutter_widget_from_html_core 0.2.4+3

  • Readme
  • Changelog
  • Example
  • Installing
  • 98

Flutter Widget from HTML (core) #

CirrusCI 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 #

const kHtml = """<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:</p>
<figure>
  <img src="https://media.giphy.com/media/6VoDJzfRjJNbG/giphy-downsized.gif" width="250" height="171" />
  <figcaption>Source: <a href="https://gph.is/QFgPA0">https://gph.is/QFgPA0</a></figcaption>
</figure>
""";

class HelloWorldCoreScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: Text('HelloWorldCoreScreen'),
        ),
        body: HtmlWidget(
          kHtml,
          onTapUrl: (url) => showDialog(
                context: context,
                builder: (_) => AlertDialog(
                      title: Text('onTapUrl'),
                      content: Text(url),
                    ),
              ),
        ),
      );
}

Features #

HTML tags #

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

  • A: underline, blue color, no default onTap action (use flutter_widget_from_html for that)
  • H1/H2/H3/H4/H5/H6
  • IMG with support for asset (asset://), data uri and network image without caching (use flutter_widget_from_html for that)
  • LI/OL/UL with support for:
    • Attributes: type, start, reversed
    • Inline style list-style-type values: lower-alpha, upper-alpha, lower-latin, upper-latin, circle, decimal, disc, lower-roman, upper-roman, square
  • TABLE/CAPTION/THEAD/TBODY/TFOOT/TR/TD/TH with support for:
    • Attribute: <table border="1">
    • Inline style: <table style="border: 1px solid #f00">
  • ABBR, ACRONYM, ADDRESS, ARTICLE, ASIDE, B, BIG, BLOCKQUOTE, BR, CENTER, CITE, CODE, DD, DEL, DFN, DIV, DL, DT, EM, FIGCAPTION, FIGURE, FONT, FOOTER, HEADER, HR, I, IMG, INS, KBD, MAIN, NAV, P, PRE, Q, S, SAMP, SECTION, STRIKE, STRONG, TT, U, VAR

However, these tags and their contents will be ignored:

Inline stylings #

  • border-top, border-bottom: overline/underline with support for dashed/dotted/double/solid style
  • color: hex values only (#F00, #0F08, #00FF00 or #00FF0080)
  • font-family
  • font-size: absolute (e.g. xx-large), relative (larger, smaller) and value in em/px
  • 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:

const kHtml = """
<p>Hello <img class="smilie smilie-1" alt=":)" src="http://domain.com/sprites.png" />!</p>
<p>How are you <img class="smilie smilie-2" alt=":P" src="http://domain.com/sprites.png" />?
""";

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

class SmilieScreen extends StatelessWidget {
  final smilieOp = BuildOp(
    onPieces: (meta, pieces) {
      final alt = meta.domElement.attributes['alt'];
      final text = kSmilies.containsKey(alt) ? kSmilies[alt] : alt;
      return pieces..first?.block?.addText(text);
    },
  );

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: Text('SmilieScreen'),
        ),
        body: HtmlWidget(
          kHtml,
          builderCallback: (meta, e) => e.classes.contains('smilie')
              ? lazySet(null, buildOp: smilieOp)
              : meta,
        ),
      );
}

0.2.4+3 #

  • Improve BR rendering logic
  • Add enableCaching prop to control cache logic

0.2.4+2 #

  • Add basic detection and support for Directionality widget during LI/OL/UL rendering (#115)
  • Fix bug LI has empty A (#112)

0.2.4+1 #

  • Improve caching logic (#112)
  • Fix extra space after BR tag (#111)
  • Fix cached image not being rendered on first build (#113)

0.2.4 #

  • Add support for type/start/reversed (LI/OL/UL) (#91)
  • Add support for tag FONT (#109)

0.2.3+4 #

  • Improve IMG error handling (#96)
  • Fix bug rendering nested list (OL/UL) with single child (#88)
  • Fix bug related to null widget (#94, #95)

0.2.3+3 #

  • Improve BR rendering to be consistent with browsers (#83, #84)
  • Improve TABLE rendering to support multiple tables (#85, #86)

0.2.3+2 #

  • Fix bug rendering empty TD tag (#81)
  • Improve white space rendering
  • Improve IMG rendering

0.2.3+1 #

  • Build RichText with proper textScaleFactor (#75, #78)

0.2.3 #

  • Re-implement text-align support to avoid conflicts (#66, #74)
  • Fix WebView bug triggering browser for http 301/302 urls
  • Improve performance when being put in list views (#62)

0.2.2+1 #

  • Update coding convention

0.2.2 #

  • Intercept all navigation requests within IFRAME (#48)
  • Add support for InlineSpan / inline image (PR #53, issue #7)
  • Add support for asset:// image (PR #52, issue #51)

0.2.1+1 #

  • Merge textStyle with default for easy usage (#45)
  • Fix bug in whitespace handling (#44)

0.2.1 #

  • Render IMG inline whenever possible
  • Other bug fixes and improvements

0.2.0 #

  • Add support for new tags: ABBR ACRONYM ADDRESS ARTICLE ASIDE BIG BLOCKQUOTE CITE CENTER DD/DL/DT DEL DFN FIGURE FIGCAPTION FOOTER HEADER HR INS KBD MAIN MARK NAV Q S SAMP STRIKE SECTION SMALL TT VAR
  • Add support for table tags: TABLE CAPTION THEAD TBODY TFOOT TR TD TH
  • Add support for background-color
  • Add support for em CSS unit
  • Improve support for existing tags: BR H1 H2 H3 H4 H5 H6 IMG P PRE
  • Simplify config for easy usage and customization
  • Fix bug using int.parse (#34)

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/README.md

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:


dependencies:
  flutter_widget_from_html_core: ^0.2.4+3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub 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';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
96
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
98
Learn more about scoring.

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

  • Dart: 2.6.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.6

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/src/ops/tag_img.dart.

Run flutter format to format lib/src/ops/tag_img.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
html ^0.14.0+3 0.14.0+3
Transitive dependencies
charcode 1.1.2
collection 1.14.11 1.14.12
csslib 0.16.1
meta 1.1.7 1.1.8
path 1.6.4
sky_engine 0.0.99
source_span 1.5.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test