You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在flutter_html中加载并使用外部CSS类?

在flutter_html里给HTML类绑定自定义样式

不用WebView完全没问题,flutter_html本身就支持把自定义样式映射到HTML的class上,直接按下面的方式实现:

  • 先定义样式集合,把text-right类对应到Flutter的Style属性:
final customClassStyles = {
  "text-right": Style(
    textAlign: TextAlign.right,
    // 还能添加其他样式属性,比如字体大小、颜色等
    // fontSize: FontSize(15),
    // color: Colors.blueGrey,
  ),
};
  • 接着在Html组件中,通过style.classes传入这个样式集合即可:
Html(
  data: '<p class="text-right">Supposed to be following text-right styles</p>',
  style: Style(
    classes: customClassStyles,
  ),
)

如果需要给特定标签的该类设置更精准的样式(比如仅对p标签的text-right生效),可以这么写:

final customClassStyles = {
  "p.text-right": Style(
    textAlign: TextAlign.right,
    fontWeight: FontWeight.w600,
  ),
};

这样HTML里的<p class="text-right">就会自动应用你定义的右对齐(以及加粗)样式了。

内容的提问来源于stack exchange,提问作者Naufal Rajabi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 00:52:08