'How to change font family of app bar universally in flutter?

I want to change app bar font in my application universally but I did not find any appropriate answer I was looking for.
Is there any way to apply theme globally using ThemeData and theme in MaterialApp?

I don't want to apply TextStyle property like below

appBar: AppBar(
            title: Text('Title ',style: TextStyle(fontFamily: 'YOUR_FONT_FAMILY'),
            ),
          ),

If not, what is the best way to change the app bar font?

P.S. This is my first post on StackOverflow.



Solution 1:[1]

You should use AppBarTheme.Please find the below code

In MaterialApp use like this,

MaterialApp(
      title: 'Test App',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        appBarTheme: AppBarTheme(
          textTheme: ThemeData.light()
              .textTheme
              .copyWith(
                headline6: TextStyle( // instead of title use can use display as well
                  fontFamily: 'Lobster',
                ),
              )
              .apply(
                fontFamily: 'Lobster',
              ),
        ),
      ),
    );
AppBar(
      title: Text('App bar', 
                 style: Theme.of(context).appBarTheme.textTheme. headline6,
             ),
    );

Solution 2:[2]

appBarTheme: AppBarTheme(
      textTheme: ThemeData.light().textTheme.copyWith(
            headline6: TextStyle(
              fontFamily: 'OpenSan',
              fontSize: 20,
              fontWeight: FontWeight.bold,
            ),
          ),
    ),

After calling Theme.of(), I got error "the receiver can be null...." and got fixed by putting the "!" in the below code

title: Text(
      'Title Text',
      style: Theme.of(context).appBarTheme.textTheme!.headline6,
    ),

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Imad Ud Din Khan