Created
May 20, 2021 15:25
-
-
Save bambinoua/9fc9b8624063e951a72d36b647e15ed6 to your computer and use it in GitHub Desktop.
Utility class for generation list of gradient colors
This file contains 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
import 'package:flutter/painting.dart'; | |
/// Utility class for generation list of gradient colors. | |
class ColorGradient { | |
ColorGradient._(); | |
/// Generates the list from `length` colors which starts from | |
/// `startColor` and ends with `endColor`. | |
/// | |
/// The length must be greater than 0. | |
static List<Color> generate(Color startColor, Color endColor, int length) { | |
assert(length > 0, 'The length of gradient pallette cannot be zero.'); | |
return List.generate(length, (index) { | |
final grade = index / length; | |
return Color.fromARGB( | |
255, | |
(startColor.red * (1 - grade) + grade * endColor.red).round(), | |
(startColor.green * (1 - grade) + grade * endColor.green).round(), | |
(startColor.blue * (1 - grade) + grade * endColor.blue).round()); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment