Search code examples
flutterdartflutter-layoutflutter-appbar

Cannot apply textTheme inside AppBarTheme in flutter


I am trying to use textTheme inside AppBarTheme in flutter but when I run my application, nothing changes. Also my editor says that 'textTheme' is deprecated and shouldn't be used. This property is no longer used, please use toolbarTextStyle and titleTextStyle instead.

Here is my code snippet:

return MaterialApp(
  title: 'Flutter App',
  theme: ThemeData(
    colorScheme: ColorScheme.fromSwatch(
      primarySwatch: Colors.purple,
    ).copyWith(
      secondary: Colors.amber,
    ),
    fontFamily: 'QuickSand',
    appBarTheme: AppBarTheme(
      textTheme: ThemeData.light().textTheme.copyWith(
            headline6: const TextStyle(
              fontFamily: 'OpenSans',
              fontSize: 20,
            ),
          ),
    ),
  ),
  home: MyHomePage(),
);

I am a newbie and just started learning Flutter.


Solution

  • textThemeTextTheme? This property is deprecated, please use toolbarTextStyle and titleTextStyle instead.

    Use titleTextStyle

    appBarTheme: AppBarTheme(
    titleTextStyle:
        Theme.of(context).appBarTheme.titleTextStyle?.copyWith(
              fontFamily: 'OpenSans',
              fontSize: 20,
            ),
    ),
    

    If you like to use headline6 it is coming from textTheme.

    appBarTheme: AppBarTheme(
      titleTextStyle: Theme.of(context).textTheme.headline6?.copyWith(
            fontFamily: 'OpenSans',
            fontSize: 20,
          ),
    ),