flutter_html_renderer 0.1.2

flutter_html_renderer #

Simple HTML renderer for flutter. Converts HTML into Widgets.

Warning: This is alpha version! It is not recommended to use in production projects as API may still change!

Why #

Pub now offers variety of HTML parsing plugins. In time of creation of this plugin, there was no available solution that had all of the following points:

  • Simple and understandable codebase
  • Optimized rendering
  • Extensibility
  • Robust HTML implementation

Installation #

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

Example #

// IMPORT PACKAGE
import 'package:flutter_html_renderer/flutter_html_renderer.dart';

// INITIALIZE FROM HTML STRING.
Widget widget = HtmlRenderer(
    initialHtmlString: htmlString,
),

// INITIALIZE FROM HTML DOM NODES.
import 'package:html/parser.dart' as htmlParser;
HtmlRenderer(
    initialNodes: htmlParser.parse(htmlString).nodes, //you may have NodeList from other custom logic
),

// ENABLE WIDGET CACHE.
Widget widget = HtmlRenderer(
    initialHtmlString: htmlString,
    keepAlive: true
),

// ADD CUSTOM LINK CLICK HANDLER
HtmlRenderer(
    initialHtmlString: htmlString,
    linkHandler: (String url) async {
      print("Custom link handling");
      return true;
  },
),

// CUSTOM RENDERING FOR DIV
class DivDescriptor extends ElementDescriptor {
      const DivDescriptor()
      : super(
        name: 'div',
        supported: true,
        isBlock: true,
      );
    
      @override
      Future<Widget> render(
          dom.Element element,
          BuildContext context,
          WidgetsFactory widgetsFactory,
          LinkHandler linkHandler,
          RenderingContext renderingContext) async {
            if(element.attributes['id'] == 'someSpecialFeature'){
                return Text('To use this feature visit our website!');
            }
            return null; // use default rendering
        );
      }
    
}

// REPLACE DEFAULT DESCRIPTOR WITH CUSTOM
WidgetsFactory.allElements['div'] = DivDescriptor(); 
// INITIALIZE RENDERER NORMALY
HtmlRenderer(
    initialHtmlString: htmlString,
    keepAlive: true,
    linkHandler: (String url) async {
      print("Custom link handling");
      return true;
  },
),

List of currently supported elements #

  • Block level

    • body
    • html
    • address
    • article
    • aside
    • blockquote
    • details
    • dialog
    • div
    • dl
    • dt
    • fieldset
    • figcaption
    • figure
    • footer
    • h1, h2, h3, h4, h5, h6
    • header
    • hgroup
    • main
    • nav
    • p
    • section
    • iframe (partial support: only iframes embeding YouTube videos)
  • Inline

    • abbr
    • acronym
    • cite
    • img
    • label
    • span

Roadmap #

  • v 0.2.0 - support for formating elements (b, i, strong, ...)
  • v 0.3.0 - support for ul, ol, li
  • v 0.4.0 - support for tables
  • v 0.5.0 - polishing of default rendering of all element supported so far

Support for other tags will have to analyzed first. Roadmap may change depending on reported bugs and/or feature requests.

  • Elements that have planned support, but are not yet in roadmap
    • iframe
    • video
    • audio

Form elements are currently not planned before first stable release.

CSS and advanced styling support is not planned before first stable release

TODOs #

  • Add tests

Contribution and Support #

  • Contributions are welcome!
  • If you want to contribute code please create a PR
  • If you find a bug or want a feature, please fill an issue

[0.1.2]

  • Upgraded dependencies

[0.1.1]

  • Fixed wrapping issues
  • Implemented health suggestions on pub analysis

[0.1.0]

  • Support for iframes embeding YouTube videos

[0.0.3]

  • Changed default example readme

[0.0.2]

  • Added example
  • Minor improvement of p tag rendering

[0.0.1]

  • Initial alpha release of library.

example/README.md

example #

Example showcasing usage for flutter_html_renderer plugin

Getting Started #

Run project with flutter run on your device, to see plugin in action!

Use this package as a library

1. Depend on it

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


dependencies:
  flutter_html_renderer: ^0.1.2

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_html_renderer/flutter_html_renderer.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
47
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]
74
Learn more about scoring.

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

  • Dart: 2.5.0
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.2

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/flutter_html_renderer.dart.

Run flutter format to format lib/flutter_html_renderer.dart.

Format lib/widgets_factory.dart.

Run flutter format to format lib/widgets_factory.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
flutter_advanced_networkimage ^0.5.0 0.5.0
html ^0.14.0 0.14.0+2
url_launcher ^5.1.2 5.1.3
youtube_player_flutter ^4.1.0 4.1.0
Transitive dependencies
async 2.3.0
cached_network_image 1.1.1
charcode 1.1.2
collection 1.14.11 1.14.12
convert 2.1.1
crypto 2.1.3
csslib 0.16.1
flutter_cache_manager 1.1.1
flutter_svg 0.13.1 0.14.1
http 0.12.0+2
http_parser 3.1.3
meta 1.1.7
path 1.6.4
path_drawing 0.4.1
path_parsing 0.1.4
path_provider 1.3.0
pedantic 1.8.0+1
petitparser 2.4.0
platform 2.2.1
sky_engine 0.0.99
source_span 1.5.5
sqflite 1.1.6+4
string_scanner 1.0.5
synchronized 2.1.0+1
term_glyph 1.1.0
typed_data 1.1.6
uuid 2.0.2
vector_math 2.0.8
webview_flutter 0.3.14+1
xml 3.5.0
Dev dependencies
flutter_test