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

6.7k views Asked by At

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?

1

There are 1 answers

0
MendelG On BEST ANSWER

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