42

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
rozerro
  • 5,787
  • 9
  • 46
  • 94

6 Answers6

48

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

kamran khan
  • 597
  • 3
  • 10
34

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,
      ),
    )
    
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
  • This one worked for me with the new ThemeData, but I had to add the parameter `brightness: Brightness.dark,`. Otherwise, Flutter gives an error without it. – PhönixGeist Sep 24 '22 at 22:21
4

Code before migration:

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

Code after migration:

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

benten
  • 696
  • 7
  • 18
3

As the deprecated message says:

///colorScheme.secondary
 ThemeData(colorScheme: ColorScheme(secondary:Colors.white ),);
Nicolás López
  • 430
  • 2
  • 8
2

Write this:

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

Then, use

colorScheme.secondary

in place of

accentColor

everywhere.

  • This looks the same as the top answer. – General Grievance Mar 31 '22 at 12:44
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 31 '22 at 14:28
0

you need to add a color scheme because accent color is deprecated.

body: const Center(child: const Text('BMI Calculator')),
  floatingActionButton: Theme(
    data: ThemeData(
      colorScheme:
          ColorScheme.fromSwatch().copyWith(secondary: Colors.white),
    ),
    child: FloatingActionButton(
      child: const Icon(
        Icons.add,
      ),
      onPressed: () {},
    ),
  ),
rabia
  • 1
  • 1