-
Notifications
You must be signed in to change notification settings - Fork 30.3k
Add a static function to Material UI widgets exposing internal default values #130135
Copy link
Copy link
Open
Labels
P3Issues that are less important to the Flutter projectIssues that are less important to the Flutter projectc: new featureNothing broken; request for a new capabilityNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to FlutterA detailed proposal for a change to Fluttercustomer: crowdAffects or could affect many people, though not necessarily a specific customer.Affects or could affect many people, though not necessarily a specific customer.f: material designflutter/packages/flutter/material repository.flutter/packages/flutter/material repository.f: themingStyling widgets with a themeStyling widgets with a themeframeworkflutter/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 teamwould be a good packageSeparate Flutter package should be made for thisSeparate Flutter package should be made for this
Metadata
Metadata
Assignees
Labels
P3Issues that are less important to the Flutter projectIssues that are less important to the Flutter projectc: new featureNothing broken; request for a new capabilityNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to FlutterA detailed proposal for a change to Fluttercustomer: crowdAffects or could affect many people, though not necessarily a specific customer.Affects or could affect many people, though not necessarily a specific customer.f: material designflutter/packages/flutter/material repository.flutter/packages/flutter/material repository.f: themingStyling widgets with a themeStyling widgets with a themeframeworkflutter/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 teamwould be a good packageSeparate Flutter package should be made for thisSeparate Flutter package should be made for this
Is there an existing issue for this?
Use case
When making custom widgets, it is often useful to use default values from standard Material UI widgets as default fall-back values for the custom widget from its equivalent standard version's theme properties.
Doing that currently involves looking up the correct fallback value for such properties from its corresponding standard Material UI Widget in the source code from its private classes that implement these default values, and then add them as hard coded own constants for its own M2 and/or M3 mode values.
It would be very useful if we could access the private default values contained in Material UI component class files. They are typically found at the end of every UI widget having the name structure
_ComponentDefaultsM2and_ComponentDefaultsM3, whereComponentis the name of the Material UI widget in question.Before Material3 was added, the UI widget default values where spread all over Widget build methods. Getting access to them was not feasible and even finding the default values was very difficult. However, the Material3 addition/migration cleaned up all this and extracted the default values into own private classes that the Material UI widgets access to get their defaults from. It would thus now easily be possible and very useful to offer these values also externally.
Proposal
One possible way of doing this would be to add a static default theme value getter to each UI widget. For example in the
AppBarclass we could have a staticthemeDefaultsgetter like:When we need to know what default values an
AppBarwidget uses, we could read that by getting the default values with:Current Material UI components typically do this in their build, e.g.
AppBarflutter/packages/flutter/lib/src/material/app_bar.dart
Line 822 in ce9a604
They could use same static internally going forward:
This was just one example, it should be added to all Material UI components that have default values provided via the private default value classes.
EDIT: Add precedent info below
Precedent: DatePicker
There is already a precedent for this pattern. The
DatePickeralready offers it. However, theDatePickeralso differs in implementation style from all other Material UI widgets. The DatePicker has the private default M2/M3 value classes in the component theme filedate_picker_theme.dartand not in the component filedate_picker.dartlike all other Material component widgets do.In the
date_picker_theme.dartit has a staticdefaultsin theDatePickerTheme, like so:flutter/packages/flutter/lib/src/material/date_picker_theme.dart
Line 627 in 3ec96a8
The
DatePickercomponent implementation then use this static from theDatePickerThemeto get the defaults:flutter/packages/flutter/lib/src/material/date_picker.dart
Line 504 in 3ec96a8
Why is
DatePickerimplementation different? Is the intent to migrate all Material UI components to this model?The
DatePickerThemeis one of the newest theme additions in the framework, so it would make sense if it is a new design, but it does not make sense if it is the only one using this pattern.The static
defaultscertainly fits even better in the component theme class, than in the component class. Moving all the private default value classes to the theme classes certainly works very well too, even better. No objections there, as long as we can have access to them.cc: @TahaTesser