17

I have added post description from Json Rest API using HTML package in flutter. The default font size looks too small. How to increase the font size of below content - Html( data: user['news_description'],),

import 'package:flutter_html/flutter_html.dart';

    class NewsDetails extends StatelessWidget {
          var user;
          var image_url = 'http://example.com/news/upload/';
          NewsDetails(this.user);

          @override
          Widget build(BuildContext context) {
            return Scaffold(
              body: SingleChildScrollView(
                child: Column(
                  children: <Widget>[
                    Image.network(image_url + user['news_image'],  width: double.infinity, height: 300.0, fit: BoxFit.cover,),
                    SizedBox(height:5.0),
                    Padding(padding: EdgeInsets.all(15.0),
                      child: Column(
                        children: <Widget>[
                          Text(user['news_title'],
                            style: TextStyle(fontSize: 22.0, fontWeight: FontWeight.w400),),
                          SizedBox(height:10.0),
                        Html( data: user['news_description'],)
                        ],
                      )
                    )
                  ],
                )
              ),
            );
          }
        }

6 Answers 6

46

Using flutter_html for richtext html.

Try this code to change the Html font style:

Html(
  data: 'my text',
  style: {
    "body": Style(
      fontSize: FontSize(18.0),
      fontWeight: FontWeight.bold,
    ),
  },
)
Sign up to request clarification or add additional context in comments.

3 Comments

This is the correct answer for the latest version flutter html 2.2.1
but now, in 2022 its not working, what is the approach now ?
I set font size to 17 sp, but it not display properly. I almost cannot read because its very small. It happened in certain device. Do you know how to fix this ?
7

You could use customTextStyle to change the font size.

customTextStyle: (dom.Node node, TextStyle baseStyle) {
  return baseStyle.merge(TextStyle(height: 2, fontSize: 28));
}

Complete example,

Html(
  data: user['news_description'],
  customTextStyle: (dom.Node node, TextStyle baseStyle) {
    return baseStyle.merge(TextStyle(height: 2, fontSize: 28));
  },
)

3 Comments

how to change width of table in flutter which show full screen width?
Upvote for this answer. But I have a little issue. What if I want to use the newest version of this package? It does not seem to have kept this customTextStyle property...
what is dom.Node node
4

We can use defaultTextStyle Try this code

    Html(
     data: "<b>Hai</b>",
     defaultTextStyle: TextStyle(
       fontSize: 15,
       color: Colors.blue,
       fontWeight: FontWeight.bold),
    ),

2 Comments

is there a missing " on the data
@GoldenLion Thanks for giving feedback. I updated the changes.
4

In year 2022, with new version of flutter_html package. To set style for html we do the following:

data: '<p>HTML text</p>',
style: {
    // p tag with text_size
    "p": Style(
           fontSize: FontSize(16),
           padding: EdgeInsets.all(6),
           backgroundColor: Colors.grey,
         ),
},

Comments

2

To change Html text style (color, size, style...) you can try:

Html(
      data: '${controller.product.value.description}',
      style: {
        'p': Style(
          color: Colors.grey
        ),
        'h4': Style(
            color: Colors.redAccent
        )
      },
    ),

and to change all text you can use: '*': Style(...),

Comments

0

For text color just use ColorFiltered

ColorFiltered(
 colorFilter: ColorFilter.mode(Colors.red, BlendMode.srcIn),
 child: Html(data: data)
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.