randomColorInRange static method
baseColor 基础颜色
variation 变化范围 0.0-1.0
Implementation
static Color randomColorInRange(Color baseColor, {double variation = 0.2}) {
assert(variation >= 0.0 && variation <= 1.0,
'variation must be between 0.0 and 1.0');
final random = Random.secure();
final range = (255 * variation).toInt();
// 使用 Color.withValues() 方法来创建新颜色
return baseColor.withValues(
red: (baseColor.r + (random.nextInt(range) - (range ~/ 2)) / 255)
.clamp(0.0, 1.0),
green: (baseColor.g + (random.nextInt(range) - (range ~/ 2)) / 255)
.clamp(0.0, 1.0),
blue: (baseColor.b + (random.nextInt(range) - (range ~/ 2)) / 255)
.clamp(0.0, 1.0),
);
}