Home > Back-end >  Style Property of TextSpan Widget in Flutter
Style Property of TextSpan Widget in Flutter

Time:01-01

I am working on a route that displays information about my flutter app. I've used RichText and TextSpan widgets in order to display inline hyperlinks to my email and github repo. This made it easy to not only underline the links, but also define TapGestureRecognizer behavior. When testing I discovered that in order for this text to always appear (regardless of DarkTheme or LightTheme) I needed to instruct the widget to get the context theme as shown below:

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

But now that I am passing a method to the style property, how can I also specify that I want text to be underlined as well? Before my style property read:

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

How can I accomplish both?

CodePudding user response:

Use copyWith:

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

TextSpan(
  text: "Your text...",
  style: existingStyle?.copyWith(
    decoration: TextDecoration.underline,
  ),
)
  • Related