Created
October 18, 2023 03:29
-
-
Save HansMuller/a527c2aa45094256a6e08eed4f1cdd0e to your computer and use it in GitHub Desktop.
This file contains 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
class Adaptation<T> { | |
const Adaptation(); | |
Type get type => T; | |
T adapt(ThemeData theme, T defaultValue) => defaultValue; | |
} | |
class ThemeData { | |
factory ThemeData({ Iterable<Adaptation>? adaptations }) { | |
return ThemeData.raw( | |
adaptationMap: <Type, Adaptation>{ | |
for (Adaptation adaptation in adaptations ?? const <Adaptation>[]) | |
adaptation.type: adaptation | |
}, | |
); | |
} | |
const ThemeData.raw({ required this.adaptationMap }); | |
final Map<Type, Adaptation> adaptationMap; | |
T adaptive<T>(T defaultValue) { | |
final Adaptation? adaptation = adaptationMap[T]; | |
return adaptation == null ? defaultValue : adaptation.adapt(this, defaultValue); | |
} | |
} | |
class StringAdaptation extends Adaptation<String>{ | |
const StringAdaptation(); | |
@override | |
String adapt(ThemeData theme, String defaultValue) => 'I am an adaptive theme'; | |
} | |
void main() { | |
final ThemeData myTheme = ThemeData( | |
adaptations: [const StringAdaptation()], | |
); | |
print(myTheme.adaptive<String>('I am the default theme')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment