Skip to content

Instantly share code, notes, and snippets.

@fredgrott
Created November 12, 2025 19:12
Show Gist options
  • Select an option

  • Save fredgrott/f2cb19f6efd2f856cb4d75a0c4699ede to your computer and use it in GitHub Desktop.

Select an option

Save fredgrott/f2cb19f6efd2f856cb4d75a0c4699ede to your computer and use it in GitHub Desktop.
withM3ETheme
/// Inject (or replace) the M3ETheme extension on a ThemeData.
ThemeData withM3ETheme(ThemeData base, {M3ETheme? override}) {
// Use any existing M3ETheme, else the provided override, else defaults.
final current = base.extension<M3ETheme>();
final next = override ?? current ?? M3ETheme.defaults(base.colorScheme);
// Merge existing extensions (values) with our M3ETheme, replacing prior ones.
final Iterable<ThemeExtension<dynamic>> existing = base.extensions.values;
final List<ThemeExtension<dynamic>> merged = <ThemeExtension<dynamic>>[];
for (final e in existing) {
if (e is! M3ETheme) {
merged.add(e);
}
}
merged.add(next);
return base.copyWith(extensions: merged);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment