0

I have a function that gets Color as an argument. I want to generate a list of random swatches for that color within that function. For example, if the color passed is Colors.amber I want a list like:

[ Colors.amber[100], Colors.amber[800], Colors.amber[500], Colors.amber[900], Colors.amber[300]... ]

Is it possible? Any advice or help would be appreciated. Thank you in advance.

Nagulan S
  • 617
  • 5
  • 11

2 Answers2

2

You can randomize only the list of "plus colors":

Flutter Colors

List<int> types = [50, 100, 200, 300, 400, 600, 700, 800, 900]

And randomize a number between 0..8

int get getRandomNumber => 0 + Random().nextInt(8 - 0);

Use to get the random color

Color? selectedColor = Colors.amber[types[getRandomNumber]];

Update:

Using this method extension, we can create a "workaround"

extension HexColor on Color {
  /// String is in the format "aabbcc" or "ffaabbcc" with an optional leading "#".
  Color fromHex(String hexString) {
    final buffer = StringBuffer();
    if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');
    buffer.write(hexString.replaceFirst('#', ''));
    return Color(int.parse(buffer.toString(), radix: 16));
  }

  /// Prefixes a hash sign if [leadingHashSign] is set to `true` (default is `true`).
  String toHex({bool leadingHashSign = true}) => '${leadingHashSign ? '#' : ''}'
      '${alpha.toRadixString(16).padLeft(2, '0')}'
      '${red.toRadixString(16).padLeft(2, '0')}'
      '${green.toRadixString(16).padLeft(2, '0')}'
      '${blue.toRadixString(16).padLeft(2, '0')}';
}

And using this function

Color getRandomColor(String selectedColor) {
  final List<int> types = [50, 100, 200, 300, 400, 600, 700, 800, 900];
  int getRandomNumber = 0 + Random().nextInt(8 - 0);
  return Color.fromHex(selectedColor)[types[getRandomNumber]];
}
//true will return "#ffffff", false will return "ffffff"
Color _color = getRandomColor(Color.red.toHex(true));
Lucas Josino
  • 820
  • 1
  • 4
  • 14
  • So I should do like brute-forcing for all colors separately inside the function, right? Thank you – Nagulan S Jul 15 '21 at 03:20
  • Is there any generalized way that would work with all colors? Cos the needed color is passed as a parameter to the function. So the color needed is present in a variable, and I need to assign swatch to that variable. – Nagulan S Jul 15 '21 at 03:25
  • I updated the answer, but, i don't tested the code – Lucas Josino Jul 15 '21 at 12:19
  • The method 'fromHex' isn't defined for the type 'Color'. Try correcting the name to the name of an existing method, or defining a method named 'fromHex'. The above error is thrown @ Color.fromHex(...). Any fix? – Nagulan S Jul 17 '21 at 09:50
-1

Found a simple solution
Let color be the passed parameter. Now find the index of that color in the Colors.primaries list.

   for(int i=0;i<Colors.primaries.length;i++){
      if(color==Colors.primaries[i]){
        colorIndex=i;
      }
    }

Use this index to generate the random swatch of the preferred color.

Colors.primaries[colorIndex][Random.nextInt(9) * 100]
Nagulan S
  • 617
  • 5
  • 11