-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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 stuartmorgan@google.com 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 zanderso@users.noreply.github.com Shift Linux_build_test tests from MotoG4 to mokey (flutter/flutter#152750) 2024-08-02 engine-flutter-autoroll@skia.org Roll Flutter Engine from b408111b976c to 077b6f057b69 (1 revision) (flutter/flutter#152745) 2024-08-02 32538273+ValentinVignal@users.noreply.github.com Add test for scaffold_state.show_bottom_sheet.0.dart (flutter/flutter#152731) 2024-08-02 engine-flutter-autoroll@skia.org Roll Flutter Engine from fe39ed980e74 to b408111b976c (6 revisions) (flutter/flutter#152739) 2024-08-02 engine-flutter-autoroll@skia.org Roll Flutter Engine from ab3f177fb61d to fe39ed980e74 (1 revision) (flutter/flutter#152721) 2024-08-02 engine-flutter-autoroll@skia.org Roll Flutter Engine from 1cbe88e8115e to ab3f177fb61d (4 revisions) (flutter/flutter#152718) 2024-08-01 engine-flutter-autoroll@skia.org Roll Flutter Engine from 17e3c7d520e7 to 1cbe88e8115e (5 revisions) (flutter/flutter#152707) 2024-08-01 nate.w5687@gmail.com Use `.fromMap()` constructors in example code (flutter/flutter#152535) 2024-08-01 jhy03261997@gmail.com Add more widgets to a11y assessment app (flutter/flutter#152662) 2024-08-01 engine-flutter-autoroll@skia.org Roll Flutter Engine from 230879c7936e to 17e3c7d520e7 (1 revision) (flutter/flutter#152703) 2024-08-01 jmccandless@google.com Explain that predictive back doesn't work with WillPopScope (flutter/flutter#152116) 2024-08-01 jmccandless@google.com SearchBar.scrollPadding (flutter/flutter#152635) 2024-08-01 jmccandless@google.com Quick docs grammar fixes (flutter/flutter#152700) 2024-08-01 engine-flutter-autoroll@skia.org Roll Flutter Engine from bbb2fcad4808 to 230879c7936e (1 revision) (flutter/flutter#152701) 2024-08-01 matanlurey@users.noreply.github.com Disable DDS and Dart profiling for Android driver tests. (flutter/flutter#152696) 2024-08-01 srawlins@google.com Remove one set of parens that wrap a single String literal (flutter/flutter#152031) 2024-08-01 engine-flutter-autoroll@skia.org Roll Flutter Engine from 7c4a44611abe to bbb2fcad4808 (3 revisions) (flutter/flutter#152694) 2024-08-01 bkonyi@google.com Fix bad pattern matching in DDS exception parsing (flutter/flutter#152685) 2024-08-01 engine-flutter-autoroll@skia.org Roll Packages from 46a712f to 27896d1 (7 revisions) (flutter/flutter#152693) 2024-08-01 dkwingsmt@users.noreply.github.com [CupertinoActionSheet & AlertDialog] Improve documentation and type for `scrollController` parameters (flutter/flutter#152647) 2024-08-01 38427679+xVemu@users.noreply.github.com 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 rmistry@google.com,stuartmorgan@google.com 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!)