How to define custom text theme in flutter?


Asked by stuckedoverflow on December 14, 2018 (source).

How to make my own text theme style? I only find the default text theme like this but it's not enough.

textTheme: TextTheme(
  body1: TextStyle(),
  body2: TextStyle(),
  button: TextStyle(),
  caption: TextStyle(),
  display1: TextStyle(),
  display2: TextStyle(),
  display3: TextStyle(),
  display4: TextStyle(),
  headline: TextStyle(),
  overline: TextStyle(),
  subhead: TextStyle(),
  subtitle: TextStyle(),
  title: TextStyle(),

I want for example have a text with line through then some other have underline etc I was thinking to override the body2 for underline style then how to define another style for line through?

Kind Regards


Question answered by chemamolins (source).

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

   style: CustomTextStyle.display5(context),

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