Sub6Resources / flutter_html

A Flutter widget for rendering static html as Flutter widgets (Will render over 80 different html tags!)
https://pub.dev/packages/flutter_html
MIT License
1.76k stars 814 forks source link

how to use customtextstyle to change the font size and line height? #116

Closed ADLsourceCode closed 4 years ago

ADLsourceCode commented 4 years ago

Hello, How do I use customTextstyle to change the line height and font size of the text? Below is my code:-

 Widget shortDesc(String data)
  {
   return Html(
      data: data,
      //Optional parameters:
      padding: EdgeInsets.all(2.0),
      backgroundColor: Colors.white70,
      defaultTextStyle: TextStyle(fontFamily: 'serif'),
      linkStyle: const TextStyle(
        color: Colors.redAccent,
      ),
      onLinkTap: (url) {
        // open url in a webview
      },
      onImageTap: (src) {
        // Display the image in large form.
      },
    );
  }
The-Redhat commented 4 years ago

You can use the customTextStyle parameter. The function gives you the dom node, which represents the html widget. (Like customTextAlign in Readme)

ADLsourceCode commented 4 years ago

@The-Redhat Thanks for your reply,

the code says

"Undefined class 'dom'"

Below is my code:-

Widget shortDesc(String data)
  {
   return Html(
      data: data,
      //Optional parameters:
      padding: EdgeInsets.all(2.0),
      backgroundColor: Colors.white70,
      defaultTextStyle: TextStyle(fontFamily: 'serif'),
      linkStyle: const TextStyle(
        color: Colors.redAccent,
      ),
      onLinkTap: (url) {
        // open url in a webview
      },
      onImageTap: (src) {
        // Display the image in large form.
      },
       useRichText: false,
       customTextStyle : (dom node,textstyle) {
         textstyle=TextStyle(height: 1);
         if (node is dom.Element) {
           switch (node.localName) {
             case "article":
               return textstyle;
           }
         }
       }
    );
  }
The-Redhat commented 4 years ago

You need to import dom:

import 'package:html/dom.dart' as dom;
ADLsourceCode commented 4 years ago

@The-Redhat Thanks you so much..

the Code works..but the custom styles do not get seen ... basically, there is no change on the text even after implementing custom styles

The-Redhat commented 4 years ago

The code you posted doesn't really change the TextStyle, or am I wrong ?

ADLsourceCode commented 4 years ago

@The-Redhat I tried changing the line height using textstyle=TextStyle(height: 1); in the code..

The-Redhat commented 4 years ago

But isn't 1 the default ?

ADLsourceCode commented 4 years ago

@The-Redhat yes... even if i change it to 2 or 2.5 ... No effect is shown...

The-Redhat commented 4 years ago

Ah okay does it work with other elements ?

ADLsourceCode commented 4 years ago

@The-Redhat nope... I tried using with p tag also... It does not...even the font size does not change

The-Redhat commented 4 years ago

Ok .. I haven't used the customTextStyle, the owner of this package for help should be able to help you

ADLsourceCode commented 4 years ago

@Sub6Resources pls help

The-Redhat commented 4 years ago

One question: to what is useRichText set ? If set to false try set it to true.

ADLsourceCode commented 4 years ago

@The-Redhat no use... nothing changes

ADLsourceCode commented 4 years ago

@Sub6Resources pls help

Sub6Resources commented 4 years ago

@ADLsourceCode please post your current code so we can take a look and see if there's anything not quite right.

Sub6Resources commented 4 years ago

Here is the correct way to use customTextStyle:

customTextStyle: (dom.Node node, TextStyle baseStyle) {
         if (node is dom.Element) {
           switch (node.localName) {
             case "article":
               return baseStyle.merge(TextStyle(height: 2, fontSize: 32));
           }
         }
       },

Note that useRichText must be set to true for customTextStyle to have any effect.

ADLsourceCode commented 4 years ago

@Sub6Resources
it does not work...

Below is my code :-


Widget shortDesc(String data) {
    return Html(
      data: data,
      padding: EdgeInsets.all(2.0),
      backgroundColor: Colors.white70,
      //defaultTextStyle: TextStyle(fontFamily: 'serif'),
      onLinkTap: (url) {
        // open url in a webview
      },
      onImageTap: (src) {
        // Display the image in large form.
      },
      useRichText: true,
      customTextStyle: (dom.Node node, TextStyle baseStyle) {
        if (node is dom.Element) {
          switch (node.localName) {
            case "p":
              return baseStyle.merge(TextStyle(height: 2, fontSize: 20));
          }
        }
      },
    );

 Container(
                      margin: EdgeInsets.only(top: 10),
                      width: size.width * 0.85,
                      child: shortDesc("""<h2>Short Desc of the book</h2>
                    <p>asdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadf
                     <ul>
                      <li>Coffee</li>
                       <li>Tea</li>
                      <li>Milk</li>
                      </ul> 
                    <p>slfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollView<b>asdadadadadadfslfsfsflsksd</b>sksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNeste</p><p>dScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScro</p>
                    <p>llViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrollViewasdadadadadadfslfsfsflsksdsksdfsNestedScrw</p>
                    """))
Sub6Resources commented 4 years ago

Hmm, this is odd. I'm able to replicate the issue, so I'll look into it.

Sub6Resources commented 4 years ago

I've identified the issue. A fix will be released asap.

Sub6Resources commented 4 years ago

A fix has been released in Version 0.10.4. The README now also contains example code for using customTextStyle. Thank you for your patience.

ADLsourceCode commented 4 years ago

@Sub6Resources It works... Thank you so much for your support.

@The-Redhat Thanks to you too.