rozerro
rozerro

Reputation: 7146

accentColor is deprecated and shouldn't be used

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

Upvotes: 48

Views: 42678

Answers (6)

rabia
rabia

Reputation: 11

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

Upvotes: 1

Mohammad Bilal Akbar
Mohammad Bilal Akbar

Reputation: 170

Write this:

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

Then, use

colorScheme.secondary

in place of

accentColor

everywhere.

Upvotes: 5

benten
benten

Reputation: 790

Code before migration:

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

Code after migration:

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

Upvotes: 5

CopsOnRoad
CopsOnRoad

Reputation: 267454

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

Upvotes: 39

3kdeveloper
3kdeveloper

Reputation: 822

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

Upvotes: 55

As the deprecated message says:

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

Upvotes: 3

Related Questions