0

I made cards and manage to do randomly colorize them with material Colors,

class RandomColor<Color> {
  List<MaterialColor> color = [
    Colors.green,
    Colors.blue,
    Colors.indigo,
  ];

  var index = Random().nextInt(3);

  MaterialColor colorRandomizer() {
    print(color[index]);
    return color[index];
  }
}

Problem is when i try it with hex color, flutter gives an error "Color isn't a function"
Also the code below is working if its not in Class but it returns only 1 random color.

error messages can be seen here

class RandomHexColor<Color> {
  Color one = Color(0xff808000);
  Color two = Color(0xff608000);
  Color three = Color(0xff208080);

  List<Color> hexColor = [one, two, three];

  var indexColor = Random().nextInt(3);

  Color colorRandom() {
    print(hexColor[indexColor]);
    return hexColor[indexColor];
  }
}

full code can be found here https://gist.github.com/nevruzoglu/3db05f01706e5b2b4e75e24cded4a5b0

Nevruzoglu
  • 13
  • 4
  • First errors because RandomHexColor inherites from Color, why??? Check this https://stackoverflow.com/questions/50081213/how-do-i-use-hexadecimal-color-strings-in-flutter – rstrelba Feb 04 '20 at 22:42

1 Answers1

3

Why are making the class generic? why are you using RandomColor<Color>{...

Remove this <Color>.

Try this,

class RandomHexColor {
  static const Color one = Color(0xff808000);
  static const Color two = Color(0xff608000);
  static const Color three = Color(0xff208080);

  List<Color> hexColor = [one, two, three];

  static final _random = Random();

  Color colorRandom() {
    return hexColor[_random.nextInt(3)];
  }
}
Crazy Lazy Cat
  • 13,595
  • 4
  • 30
  • 54