Skip to content

[material_ui] Avoid InheritedCupertinoTheme lookup in Theme.of and preserve MaterialBasedCupertinoThemeData - #13515

Open
kevmoo wants to merge 1 commit into
flutter:mainfrom
kevmoo:theme-of-cupertino-treeshake
Open

kevmoo wants to merge 1 commit into
flutter:mainfrom
kevmoo:theme-of-cupertino-treeshake

Conversation

@kevmoo

@kevmoo kevmoo commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

Avoid looking up InheritedCupertinoTheme on every Theme.of(context) call when a Material _InheritedTheme ancestor is already present, and break the Theme.build -> MaterialBasedCupertinoThemeData -> Theme.of -> CupertinoBasedMaterialThemeData -> ColorScheme.fromSeed retention cycle so Dart TFA can tree-shake ColorScheme.fromSeed and package:material_color_utilities when unused.

  • Problem:
    1. Theme.of(context) unconditionally called context.dependOnInheritedWidgetOfExactType<InheritedCupertinoTheme>() before checking inheritedTheme != null, registering a redundant InheritedElement dependency on every widget calling Theme.of(context) even when a Material Theme ancestor was present.
    2. Theme.build wraps every Material Theme in CupertinoTheme(data: MaterialBasedCupertinoThemeData(...)), and MaterialBasedCupertinoThemeData evaluated (materialTheme.cupertinoOverrideTheme ?? const CupertinoThemeData()).noDefault(), instantiating const CupertinoThemeData() in every Material app.
    3. Because InheritedCupertinoTheme and CupertinoThemeData were always instantiated, Dart Type Flow Analysis (TFA) could not eliminate the CupertinoBasedMaterialThemeData fallback branch in Theme.of(context), which calls ColorScheme.fromSeed and retains all 146 CAM16/HCT solver functions in package:material_color_utilities even in apps that never use ColorScheme.fromSeed.
  • Change:
    • Only look up InheritedCupertinoTheme in Theme.of(context) when inheritedTheme == null.
    • Replace (materialTheme.cupertinoOverrideTheme ?? const CupertinoThemeData()).noDefault() with materialTheme.cupertinoOverrideTheme?.noDefault() ?? const NoDefaultCupertinoThemeData() in MaterialBasedCupertinoThemeData.
    • Return themeData._materialTheme from CupertinoBasedMaterialThemeData when themeData is a MaterialBasedCupertinoThemeData.
  • Measured Wasm Size Impact (slide_puzzle with flutter build web --wasm --no-strip-wasm --source-maps):
    • Raw .wasm size: 2,497,700 B -> 2,453,498 B (-44,202 B / -1.77%; -35,403 B / -1.42% without --source-maps)
    • Wasm code section: 1,003,395 B -> 984,868 B (-18,527 B / -1.85%, -166 functions)
    • Theme.of compiled function size: 1,633 B -> 361 B (-1,272 B / -77.89%)
    • package:material_color_utilities retained code: 17,096 B (146 functions) -> 24 B (2 equality stubs, -99.86%)

Part of flutter/flutter#188444

Pre-Review Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2

…eserve MaterialBasedCupertinoThemeData

Only look up InheritedCupertinoTheme in Theme.of(context) when no
Material _InheritedTheme ancestor is present, use const
NoDefaultCupertinoThemeData() when ThemeData.cupertinoOverrideTheme is
null, and return the underlying Material ThemeData from
CupertinoBasedMaterialThemeData when given a
MaterialBasedCupertinoThemeData. This avoids an unnecessary
InheritedElement dependency on every Theme.of(context) call and allows
Dart TFA to tree-shake ColorScheme.fromSeed and
package:material_color_utilities (-44.2 KB raw Wasm with --source-maps /
-35.4 KB without --source-maps, and -18.5 KB code section across 166
functions on slide_puzzle) when ColorScheme.fromSeed is unused.
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Oct 31, 2026
@github-actions github-actions Bot added p: material_ui triage-design Should be looked at in design triage labels Oct 31, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request optimizes theme lookups and enables better tree-shaking. Specifically, it avoids looking up InheritedCupertinoTheme in Theme.of when a Material Theme ancestor is already present. It also updates MaterialBasedCupertinoThemeData to use NoDefaultCupertinoThemeData as a fallback, and modifies CupertinoBasedMaterialThemeData to preserve the underlying ThemeData when the provided theme is a MaterialBasedCupertinoThemeData. Corresponding unit tests and a changelog entry have been added. There are no review comments, and I have no feedback to provide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD p: material_ui triage-design Should be looked at in design triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant