Created
July 17, 2023 14:09
-
-
Save ayoubzulfiqar/4be48fee4af7ee3166d003088973ee3f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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