49

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
2
  • 1
    Use colorScheme Instead of accentColor colorScheme: ColorScheme.fromSwatch(accentColor: kBaseAccentColor) Commented Sep 22, 2021 at 18:15
  • Try my answer here hope it's helpful to you Commented Sep 22, 2021 at 18:31

6 Answers 6

56

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

Sign up to request clarification or add additional context in comments.

1 Comment

you can access the colour as Theme.of(context).colorScheme.secondary
39

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,
      ),
    )
    

1 Comment

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.
6

Write this:

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

Then, use

colorScheme.secondary

in place of

accentColor

everywhere.

2 Comments

This looks the same as the top answer.
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.
5

Code before migration:

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

Code after migration:

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

Comments

3

As the deprecated message says:

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

Comments

1

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: () {},
    ),
  ),

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.