-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Use .fromMap() constructors in example code
#152535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use .fromMap() constructors in example code
#152535
Conversation
|
Linking the original PR: #146043 |
|
LGTM! Thanks for the PR. |
Manual roll requested by [email protected] flutter/flutter@4ff9462...f10a497 2024-08-02 98614782+auto-submit[bot]@users.noreply.github.com Reverts "Shift Linux_build_test tests from MotoG4 to mokey (#152750)" (flutter/flutter#152755) 2024-08-02 [email protected] Shift Linux_build_test tests from MotoG4 to mokey (flutter/flutter#152750) 2024-08-02 [email protected] Roll Flutter Engine from b408111b976c to 077b6f057b69 (1 revision) (flutter/flutter#152745) 2024-08-02 [email protected] Add test for scaffold_state.show_bottom_sheet.0.dart (flutter/flutter#152731) 2024-08-02 [email protected] Roll Flutter Engine from fe39ed980e74 to b408111b976c (6 revisions) (flutter/flutter#152739) 2024-08-02 [email protected] Roll Flutter Engine from ab3f177fb61d to fe39ed980e74 (1 revision) (flutter/flutter#152721) 2024-08-02 [email protected] Roll Flutter Engine from 1cbe88e8115e to ab3f177fb61d (4 revisions) (flutter/flutter#152718) 2024-08-01 [email protected] Roll Flutter Engine from 17e3c7d520e7 to 1cbe88e8115e (5 revisions) (flutter/flutter#152707) 2024-08-01 [email protected] Use `.fromMap()` constructors in example code (flutter/flutter#152535) 2024-08-01 [email protected] Add more widgets to a11y assessment app (flutter/flutter#152662) 2024-08-01 [email protected] Roll Flutter Engine from 230879c7936e to 17e3c7d520e7 (1 revision) (flutter/flutter#152703) 2024-08-01 [email protected] Explain that predictive back doesn't work with WillPopScope (flutter/flutter#152116) 2024-08-01 [email protected] SearchBar.scrollPadding (flutter/flutter#152635) 2024-08-01 [email protected] Quick docs grammar fixes (flutter/flutter#152700) 2024-08-01 [email protected] Roll Flutter Engine from bbb2fcad4808 to 230879c7936e (1 revision) (flutter/flutter#152701) 2024-08-01 [email protected] Disable DDS and Dart profiling for Android driver tests. (flutter/flutter#152696) 2024-08-01 [email protected] Remove one set of parens that wrap a single String literal (flutter/flutter#152031) 2024-08-01 [email protected] Roll Flutter Engine from 7c4a44611abe to bbb2fcad4808 (3 revisions) (flutter/flutter#152694) 2024-08-01 [email protected] Fix bad pattern matching in DDS exception parsing (flutter/flutter#152685) 2024-08-01 [email protected] Roll Packages from 46a712f to 27896d1 (7 revisions) (flutter/flutter#152693) 2024-08-01 [email protected] [CupertinoActionSheet & AlertDialog] Improve documentation and type for `scrollController` parameters (flutter/flutter#152647) 2024-08-01 [email protected] Fix typo in CHANGELOG.md (flutter/flutter#152543) If this roll has caused a breakage, revert this CL and stop the roller using the controls here: https://autoroll.skia.org/r/flutter-packages Please CC [email protected],[email protected] on the revert to ensure that a human is aware of the problem. To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Currently, there are 21 `.resolveWith()` calls in example files.
This pull request changes 11 of them to use the new `.fromMap()` constructor. (Seven of them are now `const`!)
```dart
ListTile(
iconColor: WidgetStateColor.fromMap(<WidgetStatesConstraint, Color>{
WidgetState.disabled: Colors.red,
WidgetState.selected: Colors.green,
WidgetState.any: Colors.black,
}),
// The same can be achieved using the .resolveWith() constructor.
// The text color will be identical to the icon color above.
textColor: WidgetStateColor.resolveWith((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return Colors.red;
}
if (states.contains(WidgetState.selected)) {
return Colors.green;
}
return Colors.black;
}),
),
```
Currently, there are 21 `.resolveWith()` calls in example files.
This pull request changes 11 of them to use the new `.fromMap()` constructor. (Seven of them are now `const`!)
```dart
ListTile(
iconColor: WidgetStateColor.fromMap(<WidgetStatesConstraint, Color>{
WidgetState.disabled: Colors.red,
WidgetState.selected: Colors.green,
WidgetState.any: Colors.black,
}),
// The same can be achieved using the .resolveWith() constructor.
// The text color will be identical to the icon color above.
textColor: WidgetStateColor.resolveWith((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return Colors.red;
}
if (states.contains(WidgetState.selected)) {
return Colors.green;
}
return Colors.black;
}),
),
```
|
@nate-thegrate The new syntax is shorter and the main benefit turned out to be the equality problem it solved. Nonetheless, while working with it those days, I found it difficult to work with it when debugging is involved.
With the new syntax, I did not find a convenient way (other than adding breakpoint into WidgetStateMapper.resolve which is not very friendly when debugging issues with dozen of resolved colors or other properties). Any idea to mitigate this is welcomed. |
|
Fair point! I agree that there's a tradeoff between using a callback vs. a map in terms of verbosity/performance/debugging. I bet there are a lot of different ways we could go about improving the debugging experience. Currently there's a way to conveniently convert from stateless → stateful widget; perhaps the same could be done for map → callback. I'll keep this in mind and will try to come up with some alternatives too. |

Currently, there are 21
.resolveWith()calls in example files.This pull request changes 11 of them to use the new
.fromMap()constructor. (Seven of them are nowconst!)