css_text 0.9.9

CSS Text #

This library makes it easy for you to convert HTML and CSS content into RichText widgets. It supports most CSS properties that are relevant to text content, except those related to text alignment.

Here's some sample HTML content with inline CSS styles:

<body>
<p style="font-size:1.5em;">
Hello <b style="font-style:italic;color:#bb0000;background:#eeff00">World</b>!!
<br/>
How are you <span style="font-family:RobotoBlack;color:#ff0000;">today?</span>
<br/>
<b>But</b> why are you doing <a href="https://google.com">this?</a><br/>
<br/>
<span style="text-decoration: underline wavy; font-size:2em">
Can you tell <del>me</del>?
</span>
<br/>
Please visit <a style="font-weight:bold;" href="https://docs.flutter.io">Flutter docs</a>
<br/>
<br/>
<span style="color:#990000ff;background:#4400ff00">
This text is slightly transparent, and has a slightly transparent background too.
</span>
</p>
</body>

This library can automatically convert all the HTML content above into a TextSpan object or a RichText widget. Here's what the rendered HTML will look like:

Getting Started #

Using css_text is extremely easy. First import the library in your Dart code:

import 'package:css_text/css_text.dart';

You can then create RichText widget from any HTML content by calling the HTML.toTextSpan() method.

String htmlContent = """
<p style="font-size:1.5em;color:#99ff0011">Hello <b>World</b>
""";

var myRichText = RichText(text: HTML.toTextSpan(context, htmlContent));

As a shortcut, you can also use the HTML.toRichText() method.

myRichText = HTML.toRichText(context, htmlContent);

Your HTML content can have links. To handle them, you must use the linksCallback optional parameter.

String htmlContent = """<span style="font-size:2em">
Please click <a href="https://pub.dartlang.org">here</a> or 
<a href="https://old.reddit.com">here</a>.<br/>
<br/>
Go ahead! Try it.
</span>""";

var myRichText = HTML.toRichText(context, htmlContent, linksCallback: (link) {
    // You can now use the url_launcher library to open the link.
    // Or you can handle the link in your app itself. This gives you
    // complete control over your links.
    // For now, let's just print it
    print(link);
});

[0.9.9] - March 6th, 2019

Added better screenshot.

[0.9.1] - March 6th, 2019

Fixed README and description.

[0.5.0] - March 6th, 2019

  • Initial release.

example/lib/main.dart

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

void main() {
  runApp(Home());
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    String htmlContent = """
<body>
<p style="font-size:1.5em;">
Hello <b style="font-style:italic;color:#bb0000;background:#eeff00">World</b>!!
<br/>
How are you <span style="font-family:RobotoBlack;color:#ff0000;">today?</span>
<br/>
<b>But</b> why are you doing <a href="https://google.com">this?</a><br/>
<br/>
<span style="text-decoration: underline wavy; font-size:2em">Can you tell <del>me</del>?</span>
<br/>
Please visit <a style="font-weight:bold;" href="https://docs.flutter.io">Flutter docs</a>
<br/>
<br/>
<span style="color:#990000ff;background:#4400ff00">
This text is slightly transparent, and has a slightly transparent background too.
</span>
</p>
</body>
""";

    var myRichText = HTML.toRichText(context, htmlContent, linksCallback: (link) {
        print("You clicked on $link");
    });

    return MaterialApp(
      home: Scaffold(
      appBar: AppBar(title: Text("Demo")),
      body: Container(padding:EdgeInsets.all(16.0), child:myRichText)
    ));
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  css_text: ^0.9.9

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

We analyzed this package on Sep 20, 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.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
xml ^3.3.1 3.5.0
Transitive dependencies
charcode 1.1.2
collection 1.14.11 1.14.12
convert 2.1.1
meta 1.1.7
petitparser 2.4.0
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8