Orientação gerada a partir de informações públicas. Apenas para referência.
Um pacote Flutter que permite o uso de HTML simples e CSS embutido para estilizar texto, inspirado no pacote css_text. Permite aos desenvolvedores renderizar texto com estilo usando tags HTML familiares e propriedades CSS diretamente em widgets do Flutter. Ideal para interfaces de usuário controladas por servidor ou renderização de conteúdo dinâmico onde é necessário formatação de texto rica sem árvores de widget complexas. Suporta múltiplas plataformas, incluindo mobile, web e desktop.
Casos de uso
Renderização de texto rico a partir de respostas do servidor
Geração dinâmica de UI com HTML/CSS
Simplificando o estilo de texto em aplicativos Flutter
Criando componentes de texto interativos
Exibindo conteúdo formatado em formulários ou artigos
Principais recursos
Suporta tags HTML básicas
Estilo CSS embutido
Compatibilidade multiplataforma
Leve e fácil de integrar
Fork do css_text com melhorias
Indicado para
Desenvolvedores construindo interfaces do usuário controladas por servidor
Aplicativos que precisam de formatação de texto dinâmica
Equipes que preferem a sintaxe HTML/CSS
Projetos que exigem um mínimo de sobrecarga de estilização
Aplicações web e móveis com necessidades de texto rico
Considerações
Limitado a formatação de texto apenas
Sem suporte para elementos HTML avançados
Não adequado para layouts de página inteira
Recursos do CSS podem ser restritos
Melhor usado para cenários simples a moderadamente complexos de formatação de texto
README original
Snapshot do README original do repositório-fonte vinculado. Consulte o repositório para o conteúdo mais recente.
This package makes it easy for you to convert HTML with inline CSS content
into RichText widgets. It supports most CSS properties that are
relevant to text content, except for a few. Also gives the ability to
override the styles of the content passed in.
NOTE: This is not an alternative to webview packages. This package only deals with text.
Here's a sample HTML content with inline CSS styles:
<body><h1style='color: white; font-size:50px; font-style:italic;
background-color: rgb(0,122,255); font-weight:100;)'> Hello word! </h1><h1style=''>Convert your <spanstyle='color:lightseagreen;'>
HTML</span> and <spanstyle='color:dodgerblue'>CSS</span>
easily into RichText</h1><p>Lorem ipsum dolor sit, consectetur adipiscing elit. Pellentesque in leo
id dui bibendum fringilla in et arcu. In vehicula vel est sed mattis.</p><p><ahref="https://google.com">Need more? click this link</a></p><p>We all spell <spanstyle='color:crimson;
text-decoration: underline wavy;'>recieve</span> wrong.<br />Some times we
delete <del>stuff</del></p><divstyle='font-size:17px'>We write things that are
<spanstyle='font-size:1.5em;'>Big,</span><b>bold</b> or
<spanstyle='color:brown'>colorful</span></div><pstyle='font-family:times;'>Some different FONT with
<spanstyle='background-color:lightcyan;'>this part highlighted</span></p><divstyle='line-height:2; font-size:17px;'><bstyle='color: rgb(0,122,255);
font-weight:500;'>Finally some line heights.</b> Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Pellentesque in leo id dui bibendum fringilla
in et arcu. In vehicula vel est sed mattis. Duis varius, sem non mattis.</div></body>
This package can automatically convert all the HTML content above into
TextSpan objects or a RichText widget. Here's what the render will look like:
Getting Started
Using simple_html_css is extremely easy. First import the library in
your Dart code:
As a shortcut, you can also use the HTML.toRichText() method.
var myRichText = HTML.toRichText(context, htmlContent);
Container(
child: myRichText,
);
Handling Links
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 itprint(link);
});
Apply a default style
You can apply a global text sytle. This acts as the base text style of all content.
You can override the inline styles and apply global styles for each of the
HTML tags in your HTML content.
String htmlContent =
"""
<p>This has no font size css property, but global style will be applied</p>
<a style='color: orange;'>The inline color for this is orange, but it will
get overridden by global style defined below</a>
""";
HTML.toTextSpan(
context,
htmlContent,
overrideStyle: {
"p": TextStyle(fontSize: 17.3),
"a": TextStyle(color: Colors.red),
// specify any tag not just the supported ones,// and apply TextStyles to them and/override them
},
);
Supported HTML Tags
Supports all tags which prints text normally like p, div, span, body etc.
And the following special tags which change the text appearance
<h1> - <h6>
<b><strong>
<br><br />
<i><em>
<u>
<strike><del><s>
<a> - anchor tags with link click callback
Supported CSS properties (text related)
supported Css property
supported value(s)
font-weight
100 to 900, normal, medium, bold
color
css color name like: red, orangered etc. or rgb(0, 0, 255) or rgba(0, 0, 50, 0.5) or Hex #eee/#ff00ff/#ff005522
<ol><ul><li>
These tags will be rendered on new lines, but without the number or symbol
Workaround for text not rendering properly
If you experienced any rendering issue like this,
It is becuase you need to have the build context of a Material parent
Widget such as a Scaffold or Material.
To get the context directly a quick workaround is adding a Builder