Skip to content

Instantly share code, notes, and snippets.

@jonahwilliams
Created October 18, 2023 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonahwilliams/2b4f60357ce32dd6a2e4b9b861b57f26 to your computer and use it in GitHub Desktop.
Save jonahwilliams/2b4f60357ce32dd6a2e4b9b861b57f26 to your computer and use it in GitHub Desktop.
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