How to define custom text theme in flutter?

Here is an alternative using the extension method with the lineThrough:

extension CustomStyles on TextTheme {
 TextStyle get error => const TextStyle(decoration: TextDecoration.lineThrough, fontSize: 20.0, color: Colors.blue, fontWeight: FontWeight.bold);

And then you can use it like this:

Text("your text", style: Theme.of(context).textTheme.error)

Since the release of dart 2.7, you can also use an extension method:

extension CustomStyles on TextTheme {
  TextStyle get error {
    return TextStyle(
      fontSize: 18.0,
      color: Colors.red,
      fontWeight: FontWeight.bold,
    );
  }
}

Then you can use it like this:

Text(error, style: Theme.of(context).textTheme.error)

You can create a class to hold your style and then call it from anywhere in your app.

class CustomTextStyle {
  static TextStyle display5(BuildContext context) {
    return Theme.of(context).textTheme.display4.copyWith(fontSize: 192.0);
  }
}

And the use it as

Text(
   'Wow',
   style: CustomTextStyle.display5(context),
),

Look at question Flutter: Define custom TextStyles for use throughout the app that contains the complete answer referred here.

Tags:

Flutter