【问题标题】:Flutter: flutter_markdown fontSize颤振:flutter_markdown 字体大小
【发布时间】:2018-09-05 01:14:09
【问题描述】:

在使用flutter_markdown时有什么方法可以改变文本的字体大小吗?类似于将 TextStyle 提供给 Text 小部件。谢谢!

【问题讨论】:

    标签: flutter markdown


    【解决方案1】:
    Markdown(
        data: html2md.convert(article.content),
        styleSheet: MarkdownStyleSheet.fromTheme(Theme.of(context))
                        .copyWith(
                           p: Theme.of(context).textTheme.body1.copyWith(fontSize: 12.0)
                        ),
    )
    

    您可以在markdown中覆盖特定元素的文本样式,上面的代码示例从markdown覆盖p元素(html中的<p>元素)

    【讨论】:

      【解决方案2】:

      以下代码在所有元素中缩放文本大小:

      Markdown(
        styleSheet: MarkdownStyleSheet.fromTheme(Theme.of(context))
            .copyWith(textScaleFactor: 1.5),
        data: md,
      );
      

      【讨论】:

        【解决方案3】:

        在 2021 年,设置正文样式的方式是:

        Markdown(
            data: "This is the *way*",
            styleSheet: MarkdownStyleSheet.fromTheme(ThemeData(
              textTheme: TextTheme(
                  bodyText2: TextStyle(
                      fontSize: 20.0, color: Colors.green)))))),
        

        【讨论】:

          【解决方案4】:

          这对我有用:

          theme:new ThemeData(
            backgroundColor: Colors.black26,primarySwatch: Colors.grey,
            textTheme: TextTheme(body1: TextStyle(fontSize: 25.0),
            headline: TextStyle(fontSize: 25.0),title: TextStyle(fontSize: 30.0))
          ),
          

          您可以查看 markdown github repo 中提供的 StyleSheet 类。基本上,这段代码就是我的备忘单:

            p: theme.textTheme.body1,
            h1 theme.textTheme.headline,
            h2: theme.textTheme.title,
            h3: theme.textTheme.subhead,
            h4: theme.textTheme.body2,
            h5: theme.textTheme.body2,
            h6: theme.textTheme.body2,
          

          您可以通过修改 MaterialApp 的主题数据中的标题来自定义 header1。同样,你可以通过修改title等来自定义h2。

          【讨论】:

            猜你喜欢
            • 2021-02-02
            • 2020-05-24
            • 2021-08-03
            • 2018-12-29
            • 1970-01-01
            • 2020-09-02
            • 2019-09-21
            • 2022-01-23
            • 1970-01-01
            相关资源
            最近更新 更多