Home > database >  flutter: how colorScheme property of ThemeData works?
flutter: how colorScheme property of ThemeData works?

Time:11-19

So for color scheme to Material app

              colorScheme: ThemeData().colorScheme.copyWith(

                          primary: Color(0xff075e54),
                          secondary: Color(0xffeacb2d),
                   ),

    

My question is ThemeData().colorScheme.copyWith() works and gives 2 colors primary and secondary.but if I go like ColorScheme().copyWith() it doesn't work ? why? what is difference between ThemeData().colorScheme.copyWith() and ColorScheme().copyWith() ?

CodePudding user response:

the ThemeData is a Flutter class, which come with default values for all its property when you get this:

 ThemeData().colorScheme.copyWith()

you're trying to get the colorSheme property on that built-in ThemeData in the flutter framework, then override its values with the copyWith() method. basically, it is like you get an already property that exists then update it.

now, when you use:

ColorScheme()

you're calling the constructor of the ColorSheme class which as you see:

const ColorScheme({
required this.brightness,
required this.primary,
required this.onPrimary,
Color? primaryContainer,
Color? onPrimaryContainer,
required this.secondary,
required this.onSecondary,
Color? secondaryContainer,
Color? onSecondaryContainer,
/*...*/

some properties are required, so in order to call a full new ColorSheme constructor, you need to provide values for every required property inside it, after that you can say.

ColorScheme(/* after all properties are set*/).copyWith()

so you can copy it and override its values.

the ColorScheme have factory constructors which let you get default values based on the Material Design scheme colors like ColorScheme.light(), ColorScheme.dark()... which you can use directly, or override its values like this:

ColorScheme.light().copyWith()
  • Related