【问题标题】:Style Property of TextSpan Widget in FlutterFlutter 中 TextSpan Widget 的 Style 属性
【发布时间】:2021-12-31 18:56:32
【问题描述】:

我正在研究一条显示有关我的颤振应用程序信息的路线。我使用 RichText 和 TextSpan 小部件来显示指向我的电子邮件和 github 存储库的内联超链接。这不仅可以轻松地为链接添加下划线,还可以轻松定义 TapGestureRecognizer 行为。测试时我发现为了始终显示此文本(无论 DarkTheme 或 LightTheme),我需要指示小部件获取上下文主题,如下所示:

TextSpan(
  text: "\n You may also view this project's source code at ",
  style: Theme.of(context).textTheme.bodyText1,
),

但是现在我将一个方法传递给 style 属性,我怎么能指定我希望文本也加下划线呢?在我的样式属性阅读之前:

style: TextStyle(
  decoration: TextDecoration.underline,
),

我怎样才能做到这两点?

【问题讨论】:

    标签: flutter flutter-layout text-formatting flutter-text


    【解决方案1】:

    使用copyWith:

    final existingStyle = Theme.of(context).textTheme.bodyText1;
    
    TextSpan(
      text: "Your text...",
      style: existingStyle?.copyWith(
        decoration: TextDecoration.underline,
      ),
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      • 2019-12-05
      • 2020-10-10
      • 2021-11-25
      • 2021-06-12
      相关资源
      最近更新 更多