Use .fromMap() constructors in example code (#152535)

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;
  }),
),
```
This commit is contained in:
Nate Wilson
2024-08-01 16:28:22 -06:00
committed by GitHub
parent 8a4812a6da
commit 6ff806d74e
8 changed files with 67 additions and 102 deletions

View File

@@ -66,30 +66,24 @@ class Home extends StatefulWidget {
class _HomeState extends State<Home> {
bool selected = false;
/// Sets the button's foreground and background colors.
/// If not selected, resolves to null and defers to default values.
static const ButtonStyle style = ButtonStyle(
foregroundColor: WidgetStateProperty<Color?>.fromMap(<WidgetState, Color>{
WidgetState.selected: Colors.white,
}),
backgroundColor: WidgetStateProperty<Color?>.fromMap(<WidgetState, Color>{
WidgetState.selected: Colors.indigo,
}),
);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SelectableButton(
selected: selected,
style: ButtonStyle(
foregroundColor: WidgetStateProperty.resolveWith<Color?>(
(Set<WidgetState> states) {
if (states.contains(WidgetState.selected)) {
return Colors.white;
}
return null; // defer to the defaults
},
),
backgroundColor: WidgetStateProperty.resolveWith<Color?>(
(Set<WidgetState> states) {
if (states.contains(WidgetState.selected)) {
return Colors.indigo;
}
return null; // defer to the defaults
},
),
),
style: style,
onPressed: () {
setState(() {
selected = !selected;