Skip to content

Instantly share code, notes, and snippets.

@jonahwilliams
Created October 18, 2023 00:38
Show Gist options
  • Save jonahwilliams/8cc24d2ae07ef4fe277b3d7535f7d4bc to your computer and use it in GitHub Desktop.
Save jonahwilliams/8cc24d2ae07ef4fe277b3d7535f7d4bc to your computer and use it in GitHub Desktop.
class ThemeMapper<T> {
ThemeMapper(this.callback);
final T Function(T value) callback;
T invoke(T defaultValue) {
return callback(defaultValue);
}
}
class ThemeDataMapper {
final Map<Type, ThemeMapper<Object?>> map_ = {};
void addAdaptive<T>(T Function(T value) callback) {
map_[T] = ThemeMapper<T>(callback);
}
T makeAdaptive<T>(T defaultValue) {
var mapper = map_[T];
if (mapper == null) {
return defaultValue;
}
return (mapper as ThemeMapper<T>).invoke(defaultValue);
}
}
class SwitchThemeData {
const SwitchThemeData(this.value);
final int value;
}
void main() {
var themeMapper = ThemeDataMapper();
themeMapper.addAdaptive((SwitchThemeData data) {
return SwitchThemeData(data.value + 1);
});
var switchTheme = SwitchThemeData(0);
var adaptive = themeMapper.makeAdaptive(switchTheme);
print(adaptive.value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment