accentColor is deprecated and shouldn't be used

Viewed 15102

The accentColor in ThemeData was deprecated.

What to use then in ThemeData?

theme: ThemeData(
    brightness: Brightness.light,
    primaryColor: kBaseColor,
    accentColor: kBaseAccentColor, // 'accentColor' is deprecated and shouldn't be used
5 Answers

Use the below code instead of accentColor: kBaseAccentColor,

colorScheme: ColorScheme.fromSwatch()
            .copyWith(secondary: kBaseAccentColor),

OR

Do this in a simple way: Click on Magic Bulb enter image description here

Click on Migrate to 'ColorScheme.secondary' it will automatically be converted.

enter image description here

accentColor is now replaced by ColorScheme.secondary.

  • Using new ThemeData:

    theme: ThemeData(
      colorScheme: ColorScheme.fromSwatch().copyWith(
        secondary: Colors.red, // Your accent color
      ),
    )
    
  • Using existing ThemeData:

    final theme = ThemeData.dark();
    

    You can use it as:

    theme: theme.copyWith(
      colorScheme: theme.colorScheme.copyWith(
        secondary: Colors.red,
      ),
    )
    

As the deprecated message says:

///colorScheme.secondary
 ThemeData(colorScheme: ColorScheme(secondary:Colors.white ),);

Code before migration:

Color myColor = Theme.of(context).accentColor;

Code after migration:

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

Write this:

colorScheme: ColorScheme.fromSwatch()
            .copyWith(secondary: kBaseAccentColor),

Then, use

colorScheme.secondary

in place of

accentColor

everywhere.

Related