flutter_widget_from_html_core 0.1.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 80

Flutter Widget from HTML (core)

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 {
  @override
  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 tag will be parsed as text.

Inline stylings

  • color: hex code only
  • font-family
  • font-style: italic/normal
  • font-weight: bold/normal/100..900
  • 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 and make it work for you.

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 {
  @override
  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);

  @override
  NodeMetadata collectMetadata(dom.Element e) {
    var meta = super.collectMetadata(e);

    if (e.classes.contains('smilie')) {
      meta = lazySet(meta, display: DisplayType.Inline);

      final alt = e.attributes['alt'];
      if (_kSmilies.containsKey(alt)) {
        meta = lazyAddNode(meta, text: _kSmilies[alt]);
      } else {
        // render alt text if mapping not found
        // because inline image is not supported
        meta = lazyAddNode(meta, text: alt);
      }
    }

    return meta;
  }
}

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.1.0

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.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
Popularity:
Describes how popular the package is relative to other packages. [more]
60
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]
80
Learn more about scoring.

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

  • Dart: 2.0.0
  • pana: 0.12.6
  • Flutter: 0.11.3

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
html ^0.13.3 0.13.3+3
Transitive dependencies
args 1.5.1
charcode 1.1.2
collection 1.14.11
csslib 0.14.6
logging 0.11.3+2
meta 1.1.6
path 1.6.2
sky_engine 0.0.99
source_span 1.4.1
typed_data 1.1.6
utf 0.9.0+5
vector_math 2.0.8
Dev dependencies
flutter_test