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

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

How to use external CSS class using flutter_html?

问题

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

<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:

<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:

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

答案2

得分: 0

Sure, here's the translated content:

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

Container(
  child: Html(
    shrinkWrap: true,
    data: 'Your HTML content',
    style: {
      "body": Style(
        margin: Margins.zero,
        padding: EdgeInsets.zero,
        fontSize: FontSize(17.0),
        lineHeight: LineHeight(1.4),
      ),
      "p": Style(
        padding: EdgeInsets.all(6),
        alignment: Alignment.centerRight // for text right 
      ),
    },
  ),
),

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

Html(data: """
  <p style="text-align: right">Text-Right</p>
  <p style="text-align: center">Text-center</p>
  <p style="text-align: left">Text-left</p>
""")

输出:

如何使用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.

Container(
                child: Html(
                  shrinkWrap: true,
                  data: &#39;Your HTML content&#39;,
                  style: {
                    &quot;body&quot;: Style(
                      margin: Margins.zero,
                      padding: EdgeInsets.zero,
                      fontSize: FontSize(17.0),
                      lineHeight: LineHeight(1.4),
                    ),
                    &quot;p&quot;: Style(
                      padding: EdgeInsets.all(6),
                      alignment: Alignment.centerRight // for text right 
                    ),
                  },
                ),
              ),

You achieve this without giving a custom style

Html(data: &quot;&quot;&quot;
     &lt;p style=&quot;text-align: right&quot;&gt;Text-Right&lt;/p&gt;
     &lt;p style=&quot;text-align: center&quot;&gt;Text-center&lt;/p&gt;
     &lt;p style=&quot;text-align: left&quot;&gt;Text-left&lt;/p&gt;
    &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:

确定