forked from firka/flutter
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:
@@ -37,32 +37,19 @@ class _SwitchExampleState extends State<SwitchExample> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final WidgetStateProperty<Color?> trackColor = WidgetStateProperty.resolveWith<Color?>(
|
||||
(Set<WidgetState> states) {
|
||||
// Track color when the switch is selected.
|
||||
if (states.contains(WidgetState.selected)) {
|
||||
return Colors.amber;
|
||||
}
|
||||
// Otherwise return null to set default track color
|
||||
// for remaining states such as when the switch is
|
||||
// hovered, focused, or disabled.
|
||||
return null;
|
||||
// This object sets amber as the track color when the switch is selected.
|
||||
// Otherwise, it resolves to null and defers to values from the theme data.
|
||||
const WidgetStateProperty<Color?> trackColor = WidgetStateProperty<Color?>.fromMap(
|
||||
<WidgetStatesConstraint, Color>{
|
||||
WidgetState.selected: Colors.amber,
|
||||
},
|
||||
);
|
||||
final WidgetStateProperty<Color?> overlayColor = WidgetStateProperty.resolveWith<Color?>(
|
||||
(Set<WidgetState> states) {
|
||||
// Material color when switch is selected.
|
||||
if (states.contains(WidgetState.selected)) {
|
||||
return Colors.amber.withOpacity(0.54);
|
||||
}
|
||||
// Material color when switch is disabled.
|
||||
if (states.contains(WidgetState.disabled)) {
|
||||
return Colors.grey.shade400;
|
||||
}
|
||||
// Otherwise return null to set default material color
|
||||
// for remaining states such as when the switch is
|
||||
// hovered, or focused.
|
||||
return null;
|
||||
// This object sets the track color based on two WidgetState attributes.
|
||||
// If neither state applies, it resolves to null.
|
||||
final WidgetStateProperty<Color?> overlayColor = WidgetStateProperty<Color?>.fromMap(
|
||||
<WidgetState, Color>{
|
||||
WidgetState.selected: Colors.amber.withOpacity(0.54),
|
||||
WidgetState.disabled: Colors.grey.shade400,
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@@ -36,12 +36,10 @@ class _SwitchExampleState extends State<SwitchExample> {
|
||||
bool light0 = true;
|
||||
bool light1 = true;
|
||||
|
||||
final WidgetStateProperty<Icon?> thumbIcon = WidgetStateProperty.resolveWith<Icon?>(
|
||||
(Set<WidgetState> states) {
|
||||
if (states.contains(WidgetState.selected)) {
|
||||
return const Icon(Icons.check);
|
||||
}
|
||||
return const Icon(Icons.close);
|
||||
static const WidgetStateProperty<Icon> thumbIcon = WidgetStateProperty<Icon>.fromMap(
|
||||
<WidgetStatesConstraint, Icon>{
|
||||
WidgetState.selected: Icon(Icons.check),
|
||||
WidgetState.any: Icon(Icons.close),
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@@ -120,14 +120,12 @@ class _SwitchThemeAdaptation extends Adaptation<SwitchThemeData> {
|
||||
return defaultValue;
|
||||
case TargetPlatform.iOS:
|
||||
case TargetPlatform.macOS:
|
||||
return SwitchThemeData(
|
||||
thumbColor: WidgetStateProperty.resolveWith<Color?>((Set<WidgetState> states) {
|
||||
if (states.contains(WidgetState.selected)) {
|
||||
return Colors.yellow;
|
||||
}
|
||||
return null; // Use the default.
|
||||
return const SwitchThemeData(
|
||||
thumbColor: WidgetStateProperty<Color?>.fromMap(<WidgetState, Color>{
|
||||
WidgetState.selected: Colors.yellow,
|
||||
// Resolves to null if not selected, deferring to default values.
|
||||
}),
|
||||
trackColor: const WidgetStatePropertyAll<Color>(Colors.brown),
|
||||
trackColor: WidgetStatePropertyAll<Color>(Colors.brown),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user