How do I set letterSpacing for whole app in Flutter?

2.8k views Asked by At

I am working with Flutter and I know that letterSpacing property is useful to give some spacing between letters.

I want to give it to the whole application, I mean wherever I wrote any text in application. I want to set 1.0 letter spacing to all the text.

Is there any way to do it?

2

There are 2 answers

2
glavigno On

You can define a TextTheme and set a custom TextStyle for each type of text (headline1, ...).

If no Theme is provided, Flutter defines a default one for you.

However you can override or extend the default and define the letterSpacing property. I show an example for headline1 and button below.

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        textTheme: TextTheme(
          headline1: Theme.of(context)
              .textTheme
              .bodyText1
              .copyWith(letterSpacing: 1.0),
          button: Theme.of(context)
              .textTheme
              .button
              .copyWith(letterSpacing: 1.0),
        ),
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}
0
L. Gangemi On

You can define a const DEF_TEXT_STYLE in a constants.dart file and then apply it whenever you want.

constants.dart

const DEF_TEXT_STYLE = const TextStyle(
    letterSpacing: 1.0);

You could apply in this way:

Text(
   'This is my text',
    style: DEF_TEXT_STYLE,
),

Remember to import your constants.dart file.

Otherwise you could overwrite all textTheme data similar to what @glavigno said:

Here you can see all the textTheme data available in flutter. DOCS

theme: ThemeData(
        textTheme: TextTheme(
          headline1: Theme.of(context)
              .textTheme
              .headline1
              .copyWith(letterSpacing: 1.0),
          headline2: Theme.of(context)
              .textTheme
              .headline1
              .copyWith(letterSpacing: 1.0),
          headline3: Theme.of(context)
              .textTheme
              .headline1
              .copyWith(letterSpacing: 1.0),
          headline4: Theme.of(context)
              .textTheme
              .headline1
              .copyWith(letterSpacing: 1.0),
          headline5: Theme.of(context)
              .textTheme
              .headline1
              .copyWith(letterSpacing: 1.0),
          headline6: Theme.of(context)
              .textTheme
              .headline1
              .copyWith(letterSpacing: 1.0),
          subtitle1: Theme.of(context)
              .textTheme
              .headline1
              .copyWith(letterSpacing: 1.0),
          subtitle2: Theme.of(context)
              .textTheme
              .headline1
              .copyWith(letterSpacing: 1.0),
          bodyText1: Theme.of(context)
              .textTheme
              .headline1
              .copyWith(letterSpacing: 1.0),
          bodyText2: Theme.of(context)
              .textTheme
              .headline1
              .copyWith(letterSpacing: 1.0),
          button: Theme.of(context)
              .textTheme
              .headline1
              .copyWith(letterSpacing: 1.0),
          caption: Theme.of(context)
              .textTheme
              .headline1
              .copyWith(letterSpacing: 1.0),
          overline: Theme.of(context)
              .textTheme
              .headline1
              .copyWith(letterSpacing: 1.0),
          
        ),
      ),