Skip to content

Instantly share code, notes, and snippets.

@ayoubzulfiqar
Created July 17, 2023 14:09
Show Gist options
  • Save ayoubzulfiqar/4be48fee4af7ee3166d003088973ee3f to your computer and use it in GitHub Desktop.
Save ayoubzulfiqar/4be48fee4af7ee3166d003088973ee3f to your computer and use it in GitHub Desktop.
// Extension
extension RandomColorExtension on String {
static const letters = "0123456789abcdefghijklmnopqrstuvwxyz";
static String _randomString() {
final Random random = Random();
String result = '';
for (int i = 0; i < 6; i++) {
int randomIndex = random.nextInt(letters.length);
result += letters[randomIndex];
}
return result;
}
static String randomColorHex() {
String hexColor;
do {
hexColor = _randomString();
} while (!RegExp(r'^[0-9a-fA-F]{6}$').hasMatch(hexColor));
return "0xFF$hexColor";
}
}
// Function
final List<String> colorList = <String>[];
String randomColorName = "";
Future<List<String>> futureColor() async {
await Future.delayed(
const Duration(seconds: 1),
() {
randomColorName = RandomColorExtension.randomColorHex();
colorList.add(randomColorName);
debugPrint("HexColor: $randomColorName");
setState(() {});
},
);
return colorList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment