-
Notifications
You must be signed in to change notification settings - Fork 30.3k
Proposal: Add themeBuilder to MaterialApp to eliminate theme duplication #185189
Copy link
Copy link
Open
Labels
P3Issues that are less important to the Flutter projectIssues that are less important to the Flutter projectc: proposalA detailed proposal for a change to FlutterA detailed proposal for a change to Flutterf: material designflutter/packages/flutter/material repository.flutter/packages/flutter/material repository.frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.team-designOwned by Design Languages teamOwned by Design Languages teamtriaged-designTriaged by Design Languages teamTriaged by Design Languages team
Metadata
Metadata
Assignees
Labels
P3Issues that are less important to the Flutter projectIssues that are less important to the Flutter projectc: proposalA detailed proposal for a change to FlutterA detailed proposal for a change to Flutterf: material designflutter/packages/flutter/material repository.flutter/packages/flutter/material repository.frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.team-designOwned by Design Languages teamOwned by Design Languages teamtriaged-designTriaged by Design Languages teamTriaged by Design Languages team
Use case
Flutter recommends using
ThemeData.from(colorScheme: ...)together withColorScheme.fromSeed(..., brightness: brightness). With this pattern, the only thing that differs between light and dark themes isBrightness— almost of settings not related to color (typography, shape, component themes, some ofThemeExtensions, etc.) are identical.However,
MaterialApponly accepts fully-specifiedThemeDataobjects for each oftheme,darkTheme,highContrastTheme, andhighContrastDarkTheme. The only way to avoid duplication is to define a helper function on the user side:The framework offers no first-class support for this pattern, so developers unfamiliar with it naturally write separate
ThemeDataobjects for each property, leading to drift over time.Proposal
Add a
themeBuilderproperty toMaterialAppwith the following signature:Example usage:
When
themeBuilderis provided,MaterialAppcalls it internally with the resolvedBrightnessandhighContrast, replacing the need fortheme,darkTheme,highContrastTheme, andhighContrastDarkTheme. If dark theme is not needed, the brightness argument can simply be ignored.themeBuilderand those four properties would be mutually exclusive (assertion error if both are set).themeModecontinues to work as-is.Additionally, since there is no need to instantiate a separate
ThemeDatafor each theme variant upfront, there may be a marginal performance improvement.