flutter_widget_from_html_core 0.4.1-rc.2020052701

  • Readme
  • Changelog
  • Example
  • Installing
  • 97

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, RP, RT, RUBY, S, SAMP, SECTION, STRIKE, STRONG, SUB, SUP, TT, U, VAR

However, these tags and their contents will be ignored:

Attributes #

  • dir: auto, ltr and rtl

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)
  • direction (similar to dir attribute)
  • 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 and margin-xxx (values in px, em)
  • padding and padding-xxx (values in px, em)
  • vertical-align: baseline/top/bottom/middle/sub/super
  • 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. HtmlBuilder 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?.text?.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.4.1-rc.2020052701 #

  • BREAKING: Remove HtmlConfig and change factoryBuilder method signature (#173)
  • Add support for multiple font families (#172)
  • Fix whitespace regex (#185)
  • Fix background-color priority conflict with padding (#171)
  • Switch to MIT license

0.4.0-rc.2020043001 #

  • Improve SUB, SUP to avoid overlapping (#164)
  • Add support for async build (#154)
  • Replace builderCallback with customStylesBuilder and customWidgetBuilder (#169)
  • Remove TextStyleBuilders.recognizer (#168)
  • Remove lazySet method (#169)
  • Fix conflict between TABLE and background-color (#171)

0.4.0-rc.2020041601 #

  • Simplify WidgetFactory.buildText (#162)
  • Fix bug vertical-align with block element inside (#159)

0.4.0-rc.2020040701 #

  • Use minimum main axis size
  • Improve whitespace handling (#137)
  • Improve support for right-to-left (#141)
  • Add support for async build
  • Add support for tag SUB, SUP and inline style vertical-align (#143)
  • Add support for tag RUBY (#144)
  • Add support for attribute align (#153)
  • Fix text bit loop initial state (#156)

0.4.0-rc.2020021401 #

  • Disable rendering of tag SVG

0.3.2+1 #

  • Fix IMG wrong size when device has scaled text (#127)

0.2.4+4 #

  • Fix bug rendering ZERO WIDTH SPACE character (#119)

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 text 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/main.dart

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Widget from HTML (core)',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Widget from HTML (core)'),
        ),
        body: Center(
          child: HtmlWidget('Hello World!'),
        ),
      ),
    );
  }
}

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.4.1-rc.2020052701

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]
97
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
95
Overall:
Weighted score of the above. [more]
97
Learn more about scoring.

We analyzed this package on Jun 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.9+1
  • Flutter: 1.17.3

Analysis suggestions

Package not compatible with SDK dart

because of import path [flutter_widget_from_html_core]

Maintenance suggestions

Package is pre-release. (-5 points)

Pre-release versions should be used with caution; their API can change in breaking ways.

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.3
collection 1.14.12
csslib 0.16.1
meta 1.1.8
path 1.7.0
sky_engine 0.0.99
source_span 1.7.0
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
test any