Last active
October 28, 2020 03:11
-
-
Save tranductam2802/0a75b69d05af665ef1adf87781b6f072 to your computer and use it in GitHub Desktop.
Demo input parameter for enum extension
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
enum SelectedColor { | |
primaryColor, | |
secondaryColor, | |
} | |
extension SelectedColorExtension on SelectedColor { | |
String get name => describeEnum(this); | |
String displayTitle(index) { | |
print(index); | |
switch (this) { | |
case SelectedColor.primaryColor: | |
return 'This is the Primary Color'; | |
case SelectedColor.secondaryColor: | |
return 'This is the Secondary Color'; | |
default: | |
return 'SelectedScheme Title is null'; | |
} | |
} | |
String describeEnum(Object enumEntry) { | |
final String description = enumEntry.toString(); | |
final int indexOfDot = description.indexOf('.'); | |
assert(indexOfDot != -1 && indexOfDot < description.length - 1); | |
return description.substring(indexOfDot + 1); | |
} | |
} | |
void main() { | |
print('${SelectedColor.primaryColor.displayTitle('input')}'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment