Skip to content

Instantly share code, notes, and snippets.

@tranductam2802
Last active October 28, 2020 03:11
Show Gist options
  • Save tranductam2802/0a75b69d05af665ef1adf87781b6f072 to your computer and use it in GitHub Desktop.
Save tranductam2802/0a75b69d05af665ef1adf87781b6f072 to your computer and use it in GitHub Desktop.
Demo input parameter for enum extension
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