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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user