0

我正在研究一条显示有关我的颤振应用程序信息的路线。我使用 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,
),

我怎样才能做到这两点?

4

1 回答 1

1

使用copyWith

final existingStyle = Theme.of(context).textTheme.bodyText1;

TextSpan(
  text: "Your text...",
  style: existingStyle?.copyWith(
    decoration: TextDecoration.underline,
  ),
)
于 2021-12-31T19:02:49.063 回答