mercredi 17 novembre 2021

Flutter : How to set Checkbox and Switch color in the Theme

In VSCode, Flutter is complaining to me that "'accentColor' is deprecated and shouldn't be used. Use colorScheme.secondary instead. For more information, consult the migration guide at https://flutter.dev/docs/release/breaking-changes/theme-data-accent-properties#migration-guide. This feature was deprecated after v2.3.0-0.1.pre.. Try replacing the use of the deprecated member with the replacement."

The migration guide suggests this :

Code before migration:

MaterialApp(
  theme: ThemeData(accentColor: myColor),
  // ...
);

Code after migration:

final ThemeData theme = ThemeData();
MaterialApp(
  theme: theme.copyWith(
    colorScheme: theme.colorScheme.copyWith(secondary: myColor),
  ),
  //...
)

So I made this change :

   Widget build(BuildContext context) {
+    final ThemeData theme = ThemeData();
     return MaterialApp(
       title: 'Title',
-      theme: ThemeData(
-        primaryColor: Color.fromRGBO(95, 53, 90, 1),
-        accentColor: Color.fromRGBO(76, 41, 71, 1)
+      theme: theme.copyWith(
+        colorScheme: theme.colorScheme.copyWith(
+          primary: Color.fromRGBO(95, 53, 90, 1),
+          secondary: Color.fromRGBO(76, 41, 71, 1),
         ),
+      ),
       home: MyApp()
     );
   }

But now my checkboxes and switches have changed from my primary colour to blue.

Can I still set the colour of my checkboxes in the theme ?




Aucun commentaire:

Enregistrer un commentaire