如何使用Flutter_html来引用外部CSS类?

huangapple go评论105阅读模式
英文:

How to use external CSS class using flutter_html?

问题

我正在使用 flutter_html 来加载一个来自 HTML 的小部件。我从一个返回带有外部 CSS 类的 HTML 字符串的 API 获取了构建器,大致如下:

  1. <p class="text-right">应该遵循 text-right 样式的文本</p>

假设我有 "text-right" 类的样式,如何使用 flutter_html 加载样式?在这种情况下,我不使用 flutter WebView,也不希望使用,因为 HTML 只是现有滚动页面中的一个小部件。

英文:

I'm using flutter_html to load a widget from HTML. I got the builder from an API that returns a HTML string with an external CSS class, roughly like this:

  1. <p class="text-right">Supposed to be following text-right styles</p>

Suppose I have the styling for the "text-right" class, how can we load the style using flutter_html? In this case, I'm not using flutter WebView and I don't want to since the HTML only a small widget inside an existing scrolling page.

答案1

得分: 1

I'm using 3.0.0 and was able to do the following:

"p.text-right" : Style(
textAlign: TextAlign.right
),

英文:

I'm using 3.0.0 and was able to do the following:

  1. "p.text-right" : Style(
  2. textAlign: TextAlign.right
  3. ),

答案2

得分: 0

Sure, here's the translated content:

首先,您需要在包含在HTML内容中的标记列表中添加一个标记,然后在样式参数中应用此标记。

  1. Container(
  2. child: Html(
  3. shrinkWrap: true,
  4. data: 'Your HTML content',
  5. style: {
  6. "body": Style(
  7. margin: Margins.zero,
  8. padding: EdgeInsets.zero,
  9. fontSize: FontSize(17.0),
  10. lineHeight: LineHeight(1.4),
  11. ),
  12. "p": Style(
  13. padding: EdgeInsets.all(6),
  14. alignment: Alignment.centerRight // for text right
  15. ),
  16. },
  17. ),
  18. ),

您可以在不提供自定义样式的情况下实现此效果。

  1. Html(data: """
  2. <p style="text-align: right">Text-Right</p>
  3. <p style="text-align: center">Text-center</p>
  4. <p style="text-align: left">Text-left</p>
  5. """)

输出:

如何使用Flutter_html来引用外部CSS类?

英文:

First, you need to add a tag list that is included in your HTML content and after adding this apply the style in the style parameter.

  1. Container(
  2. child: Html(
  3. shrinkWrap: true,
  4. data: &#39;Your HTML content&#39;,
  5. style: {
  6. &quot;body&quot;: Style(
  7. margin: Margins.zero,
  8. padding: EdgeInsets.zero,
  9. fontSize: FontSize(17.0),
  10. lineHeight: LineHeight(1.4),
  11. ),
  12. &quot;p&quot;: Style(
  13. padding: EdgeInsets.all(6),
  14. alignment: Alignment.centerRight // for text right
  15. ),
  16. },
  17. ),
  18. ),

You achieve this without giving a custom style

  1. Html(data: &quot;&quot;&quot;
  2. &lt;p style=&quot;text-align: right&quot;&gt;Text-Right&lt;/p&gt;
  3. &lt;p style=&quot;text-align: center&quot;&gt;Text-center&lt;/p&gt;
  4. &lt;p style=&quot;text-align: left&quot;&gt;Text-left&lt;/p&gt;
  5. &quot;&quot;&quot;, )

output:

如何使用Flutter_html来引用外部CSS类?

huangapple
  • 本文由 发表于 2023年5月13日 13:26:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241216.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定