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

如何修改flutter_markdown中超链接颜色并添加下划线?

解决flutter_markdown超链接样式修改问题

完全可行,直接通过MarkdownBody的styleSheet参数即可修改超链接的颜色和下划线——之前修改ThemeData无效是因为flutter_markdown组件自身的样式优先级高于全局主题。

具体实现代码

修改你的MarkdownBody配置,添加styleSheet属性自定义超链接样式:

_showMarkdownTestModal() {
    var testMarkdown = '[flutter_markdown](https://github.com/DavBfr/flutter_markdown)';
    showModalBottomSheet(
        context: context,
        backgroundColor: Colors.transparent,
        isScrollControlled: true,
        builder: (builder) {
          return FractionallySizedBox(
              heightFactor: 0.1,
              child: Center(
                  child: MarkdownBody(
                data: testMarkdown,
                // 新增超链接样式配置
                styleSheet: MarkdownStyleSheet(
                  a: TextStyle(
                    color: Colors.green,
                    decoration: TextDecoration.underline,
                  ),
                ),
                onTapLink: (text, url, title) {
                  if (url != null) {
                    _launchURL(Uri.parse(url));
                  }
                },
              )));
        });
  }

补充说明

  • MarkdownStyleSheet可配置Markdown中所有元素的样式(如标题、段落、列表等),此处仅针对超链接(a属性)做修改即可。
  • 确保代码已导入flutter/material.dart包,Colors和TextStyle均来自该包。

内容的提问来源于stack exchange,提问作者Orest Hredil

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 22:25:17