Created
July 7, 2020 20:56
-
-
Save felipecarvalho/e4fc123a408bed00b5f6241f4ba386e7 to your computer and use it in GitHub Desktop.
Custom color swatch for MaterialColor on Flutter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MaterialColor createMaterialColor(Color color) { | |
| List strengths = <double>[.05]; | |
| Map swatch = <int, Color>{}; | |
| final int r = color.red, g = color.green, b = color.blue; | |
| for (int i = 1; i < 10; i++) { | |
| strengths.add(0.1 * i); | |
| } | |
| strengths.forEach((strength) { | |
| final double ds = 0.5 - strength; | |
| swatch[(strength * 1000).round()] = Color.fromRGBO( | |
| r + ((ds < 0 ? r : (255 - r)) * ds).round(), | |
| g + ((ds < 0 ? g : (255 - g)) * ds).round(), | |
| b + ((ds < 0 ? b : (255 - b)) * ds).round(), | |
| 1, | |
| ); | |
| }); | |
| return MaterialColor(color.value, swatch); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment