Home > database >  How can I upgrade "accentColor" in "ThemeData" in Flutter?
How can I upgrade "accentColor" in "ThemeData" in Flutter?

Time:12-28

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?

CodePudding user response:

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

  • enter image description here

  • Related