Search code examples
fluttersyntaxupgradethemedata

How can I upgrade "accentColor" in "ThemeData" in Flutter?


I got the following code:

  theme: ThemeData(
    primarySwatch: Colors.purple,
    accentColor: Colors.deepOrange,
    fontFamily: 'Lato',
    pageTransitionsTheme: PageTransitionsTheme(
      builders: {
        TargetPlatform.android: CustomPageTransitionBuilder(),
        TargetPlatform.iOS: CustomPageTransitionBuilder(),
      },
    ),
  ),

And I know the accentColor is deprecated, so because of that I get the following errors for these lines of code:

 TextStyle( color: Theme.of(context).accentTextTheme.title.color,),

The getter 'title' isn't defined for the type 'TextTheme'. Try importing the library that defines 'title', correcting the name to the name of an existing getter, or defining a getter or field named 'title'.

 TextStyle( color: Theme.of(context).primaryTextTheme.title.color,),

The getter 'title' isn't defined for the type 'TextTheme'. Try importing the library that defines 'title', correcting the name to the name of an existing getter, or defining a getter or field named 'title'.

How can I fix the issue and upgrade the codes?


Solution

  • Instead of accentColor, use colorScheme:

    colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.purple)
                .copyWith(secondary: Colors.deepOrange),
    

    Usage:

    Color myColor = Theme.of(context).colorScheme.secondary;
    

    See also


    If you're using Android Studio, you can right-click on the deprecated warning for it to fix the error:

    enter image description here