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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 67 additions and 102 deletions

View File

@ -32,12 +32,12 @@ class MaterialStateExample extends StatelessWidget {
initialValue: 'abc', initialValue: 'abc',
decoration: InputDecoration( decoration: InputDecoration(
prefixIcon: const Icon(Icons.person), prefixIcon: const Icon(Icons.person),
prefixIconColor: WidgetStateColor.resolveWith((Set<WidgetState> states) { prefixIconColor: WidgetStateColor.fromMap(
if (states.contains(WidgetState.focused)) { <WidgetStatesConstraint, Color>{
return Colors.green; WidgetState.focused: Colors.green,
} WidgetState.any: Colors.grey,
return Colors.grey; },
}), ),
), ),
); );
} }

View File

@ -32,15 +32,11 @@ class MaterialStateExample extends StatelessWidget {
return Theme( return Theme(
data: themeData.copyWith( data: themeData.copyWith(
inputDecorationTheme: themeData.inputDecorationTheme.copyWith( inputDecorationTheme: themeData.inputDecorationTheme.copyWith(
prefixIconColor: WidgetStateColor.resolveWith( prefixIconColor: WidgetStateColor.fromMap(
(Set<WidgetState> states) { <WidgetStatesConstraint, Color>{
if (states.contains(WidgetState.error)) { WidgetState.error: Colors.red,
return Colors.red; WidgetState.focused: Colors.blue,
} WidgetState.any: Colors.grey,
if (states.contains(WidgetState.focused)) {
return Colors.blue;
}
return Colors.grey;
}, },
), ),
), ),

View File

@ -45,19 +45,13 @@ class _ListTileExampleState extends State<ListTileExample> {
_selected = !_selected; _selected = !_selected;
}); });
}, },
// This sets text color and icon color to red when list tile is disabled and iconColor: WidgetStateColor.fromMap(<WidgetStatesConstraint, Color>{
// green when list tile is selected, otherwise sets it to black. WidgetState.disabled: Colors.red,
iconColor: WidgetStateColor.resolveWith((Set<WidgetState> states) { WidgetState.selected: Colors.green,
if (states.contains(WidgetState.disabled)) { WidgetState.any: Colors.black,
return Colors.red;
}
if (states.contains(WidgetState.selected)) {
return Colors.green;
}
return Colors.black;
}), }),
// This sets text color and icon color to red when list tile is disabled and // The same can be achieved using the .resolveWith() constructor.
// green when list tile is selected, otherwise sets it to black. // The text color will be identical to the icon color above.
textColor: WidgetStateColor.resolveWith((Set<WidgetState> states) { textColor: WidgetStateColor.resolveWith((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) { if (states.contains(WidgetState.disabled)) {
return Colors.red; return Colors.red;

View File

@ -37,32 +37,19 @@ class _SwitchExampleState extends State<SwitchExample> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final WidgetStateProperty<Color?> trackColor = WidgetStateProperty.resolveWith<Color?>( // This object sets amber as the track color when the switch is selected.
(Set<WidgetState> states) { // Otherwise, it resolves to null and defers to values from the theme data.
// Track color when the switch is selected. const WidgetStateProperty<Color?> trackColor = WidgetStateProperty<Color?>.fromMap(
if (states.contains(WidgetState.selected)) { <WidgetStatesConstraint, Color>{
return Colors.amber; WidgetState.selected: 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;
}, },
); );
final WidgetStateProperty<Color?> overlayColor = WidgetStateProperty.resolveWith<Color?>( // This object sets the track color based on two WidgetState attributes.
(Set<WidgetState> states) { // If neither state applies, it resolves to null.
// Material color when switch is selected. final WidgetStateProperty<Color?> overlayColor = WidgetStateProperty<Color?>.fromMap(
if (states.contains(WidgetState.selected)) { <WidgetState, Color>{
return Colors.amber.withOpacity(0.54); WidgetState.selected: Colors.amber.withOpacity(0.54),
} WidgetState.disabled: Colors.grey.shade400,
// 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;
}, },
); );

View File

@ -36,12 +36,10 @@ class _SwitchExampleState extends State<SwitchExample> {
bool light0 = true; bool light0 = true;
bool light1 = true; bool light1 = true;
final WidgetStateProperty<Icon?> thumbIcon = WidgetStateProperty.resolveWith<Icon?>( static const WidgetStateProperty<Icon> thumbIcon = WidgetStateProperty<Icon>.fromMap(
(Set<WidgetState> states) { <WidgetStatesConstraint, Icon>{
if (states.contains(WidgetState.selected)) { WidgetState.selected: Icon(Icons.check),
return const Icon(Icons.check); WidgetState.any: Icon(Icons.close),
}
return const Icon(Icons.close);
}, },
); );

View File

@ -120,14 +120,12 @@ class _SwitchThemeAdaptation extends Adaptation<SwitchThemeData> {
return defaultValue; return defaultValue;
case TargetPlatform.iOS: case TargetPlatform.iOS:
case TargetPlatform.macOS: case TargetPlatform.macOS:
return SwitchThemeData( return const SwitchThemeData(
thumbColor: WidgetStateProperty.resolveWith<Color?>((Set<WidgetState> states) { thumbColor: WidgetStateProperty<Color?>.fromMap(<WidgetState, Color>{
if (states.contains(WidgetState.selected)) { WidgetState.selected: Colors.yellow,
return Colors.yellow; // Resolves to null if not selected, deferring to default values.
}
return null; // Use the default.
}), }),
trackColor: const WidgetStatePropertyAll<Color>(Colors.brown), trackColor: WidgetStatePropertyAll<Color>(Colors.brown),
); );
} }
} }

View File

@ -66,30 +66,24 @@ class Home extends StatefulWidget {
class _HomeState extends State<Home> { class _HomeState extends State<Home> {
bool selected = false; 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
body: Center( body: Center(
child: SelectableButton( child: SelectableButton(
selected: selected, selected: selected,
style: ButtonStyle( style: style,
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
},
),
),
onPressed: () { onPressed: () {
setState(() { setState(() {
selected = !selected; selected = !selected;

View File

@ -78,23 +78,6 @@ class _OrderedButtonState<T> extends State<OrderedButton<T>> {
order = LexicalFocusOrder(widget.order.toString()); order = LexicalFocusOrder(widget.order.toString());
} }
Color? overlayColor(Set<WidgetState> states) {
if (states.contains(WidgetState.focused)) {
return Colors.red;
}
if (states.contains(WidgetState.hovered)) {
return Colors.blue;
}
return null; // defer to the default overlayColor
}
Color? foregroundColor(Set<WidgetState> states) {
if (states.contains(WidgetState.focused) || states.contains(WidgetState.hovered)) {
return Colors.white;
}
return null; // defer to the default foregroundColor
}
return FocusTraversalOrder( return FocusTraversalOrder(
order: order, order: order,
child: Padding( child: Padding(
@ -102,9 +85,24 @@ class _OrderedButtonState<T> extends State<OrderedButton<T>> {
child: OutlinedButton( child: OutlinedButton(
focusNode: focusNode, focusNode: focusNode,
autofocus: widget.autofocus, autofocus: widget.autofocus,
style: ButtonStyle( style: const ButtonStyle(
overlayColor: WidgetStateProperty.resolveWith<Color?>(overlayColor), overlayColor: WidgetStateProperty<Color?>.fromMap(
foregroundColor: WidgetStateProperty.resolveWith<Color?>(foregroundColor), // If neither of these states is active, the property will
// resolve to null, deferring to the default overlay color.
<WidgetState, Color>{
WidgetState.focused: Colors.red,
WidgetState.hovered: Colors.blue,
},
),
foregroundColor: WidgetStateProperty<Color?>.fromMap(
// "WidgetState.focused | WidgetState.hovered" could be used
// instead of separate map keys, but this setup allows setting
// the button style to a constant value for improved efficiency.
<WidgetState, Color>{
WidgetState.focused: Colors.white,
WidgetState.hovered: Colors.white,
},
),
), ),
onPressed: () => _handleOnPressed(), onPressed: () => _handleOnPressed(),
child: Text(widget.name), child: Text(widget.name),