Reputation:
I am trying to generate a list of hexcode between two values so I can use then to generate a range of colors.
What I am trying to achieve is this.
List.generate(36, (i) => generateRandomCode(0xFF0587D8, 0xFF0345B5))
generateRandomCode(min, max) {
// implementation here
}
How do I generate this int in the function generateRandomCode?
Upvotes: 1
Views: 1876
Reputation: 15103
Generator:
int generateRandomCode(int minValue, int maxValue) {
return Random().nextInt((maxValue - minValue).abs() + 1) + min(minValue, maxValue);
}
Usage:
final list = List<int>.generate(36, (i) => generateRandomCode(0xFF0587D8, 0xFF0345B5));
print(list);
Result:
/flutter ( 6592): [4278422613, 4278508577, 4278489065, 4278486019, 4278499653, 4278480654, 4278464106, 4278474805, 4278462976, 4278549386, 4278537465, 4278418510, 4278496777, 4278405225, 4278411018, 4278412393, 4278461314, 4278538568, 4278549901, 4278510124, 4278492024, 4278530862, 4278517728, 4278425917, 4278442865, 4278497051, 4278430858, 4278497227, 4278462764, 4278412600, 4278448684, 4278422213, 4278464891, 4278473256, 4278543371, 4278476016]
You can still convert it to list of colors
final colorList = list.map((hex) => Color(hex)).toList();
print(colorList);
Result:
I/flutter ( 6592): [Color(0xff04c366), Color(0xff03d608), Color(0xff03a34a), Color(0xff048eac), Color(0xff03924a), Color(0xff03f0ba), Color(0xff052271), Color(0xff03ef8a), Color(0xff0582e0), Color(0xff0551ae), Color(0xff0402b3), Color(0xff0552be), Color(0xff050553), Color(0xff04c39f), Color(0xff053f88), Color(0xff04b6b8), Color(0xff05299f), Color(0xff03f1a7), Color(0xff03ca2f), Color(0xff04a864), Color(0xff04ee66), Color(0xff0358ce), Color(0xff03b741), Color(0xff046785), Color(0xff04ef11), Color(0xff04e618), Color(0xff03ff8a), Color(0xff03dc97), Color(0xff04353e), Color(0xff04cff6), Color(0xff03bfa4), Color(0xff049ca3), Color(0xff04bbac), Color(0xff03c5d3), Color(0xff05730b), Color(0xff036c8f)]
Upvotes: 2
Reputation: 3015
List<int> createList(int min, int max) {
return List.generate(max - min + 1, (i) => min + i);
}
Result:
print(">>>>>>>>>> ${createList(10, 22).join(", ")}");
// >>>>>>>>>> 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22
Upvotes: 1
Reputation: 3419
Use a List Generate Constructor
new List<int>.generate(3, (int index) => index * index);
Link to the source : Link generator
Upvotes: 1