Created
October 18, 2023 01:18
-
-
Save jonahwilliams/2b4f60357ce32dd6a2e4b9b861b57f26 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
typedef AdaptiveThemeCallback = Object? Function(ThemeData theme, Type type, Object? defaultValue); | |
Object? defaultCallback(ThemeData theme, Type type, Object? defaultValue) => defaultValue; | |
class ThemeData { | |
factory ThemeData({ AdaptiveThemeCallback? callback }) { | |
return ThemeData.raw(callback: callback ?? defaultCallback); | |
} | |
const ThemeData.raw({ this.callback = defaultCallback }); | |
final AdaptiveThemeCallback callback; | |
T adaptive<T>(T defaultValue) => callback(this, T, defaultValue) as T; | |
} | |
Object? myCallback(ThemeData theme, Type type, Object? defaultValue) { | |
return (type == String ? 'I am an adaptive theme' : defaultValue); | |
} | |
void main() { | |
// Unfortunately, at this point myCallback's type parameters become `dynamic` | |
final ThemeData myTheme = ThemeData(callback: myCallback); | |
// Foo.adaptive gets its version of the theme like this: | |
print(myTheme.adaptive<String>('I am a theme')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment