un-break ThemeData equality (#154695)

This PR is _almost_ able to close issue #89127.

Sadly, no `InheritedModel` or custom `RenderObject`s today; instead the [WidgetState operators](https://main-api.flutter.dev/flutter/widgets/WidgetStateOperators.html) have been restructured to support equality checks.

`WidgetStateProperty.fromMap()` is now capable of accurate equality checks, and all of the `.styleFrom()` methods have been refactored to use that constructor.

(Equality checks are still broken for `WidgetStateProperty.resolveWith()`, and any other non-`const` objects that implement the interface.)

<br><br>

credit for this idea goes to @justinmc: https://github.com/flutter/flutter/issues/89127#issuecomment-2313187703
This commit is contained in:
Nate Wilson
2024-09-09 15:49:09 -06:00
committed by GitHub
parent 18c325af17
commit bfa04edca6
13 changed files with 360 additions and 564 deletions

View File

@@ -120,31 +120,24 @@ class _${blockName}DefaultsM3 extends SegmentedButtonThemeData {
@override
Widget? get selectedIcon => const Icon(Icons.check);
static MaterialStateProperty<Color?> resolveStateColor(Color? unselectedColor, Color? selectedColor, Color? overlayColor){
return MaterialStateProperty.resolveWith((Set<MaterialState> states) {
if (states.contains(MaterialState.selected)) {
if (states.contains(MaterialState.pressed)) {
return (overlayColor ?? selectedColor)?.withOpacity(0.1);
}
if (states.contains(MaterialState.hovered)) {
return (overlayColor ?? selectedColor)?.withOpacity(0.08);
}
if (states.contains(MaterialState.focused)) {
return (overlayColor ?? selectedColor)?.withOpacity(0.1);
}
} else {
if (states.contains(MaterialState.pressed)) {
return (overlayColor ?? unselectedColor)?.withOpacity(0.1);
}
if (states.contains(MaterialState.hovered)) {
return (overlayColor ?? unselectedColor)?.withOpacity(0.08);
}
if (states.contains(MaterialState.focused)) {
return (overlayColor ?? unselectedColor)?.withOpacity(0.1);
}
}
return Colors.transparent;
});
static WidgetStateProperty<Color?> resolveStateColor(
Color? unselectedColor,
Color? selectedColor,
Color? overlayColor,
) {
final Color? selected = overlayColor ?? selectedColor;
final Color? unselected = overlayColor ?? unselectedColor;
return WidgetStateProperty<Color?>.fromMap(
<WidgetStatesConstraint, Color?>{
WidgetState.selected & WidgetState.pressed: selected?.withOpacity(0.1),
WidgetState.selected & WidgetState.hovered: selected?.withOpacity(0.08),
WidgetState.selected & WidgetState.focused: selected?.withOpacity(0.1),
WidgetState.pressed: unselected?.withOpacity(0.1),
WidgetState.hovered: unselected?.withOpacity(0.08),
WidgetState.focused: unselected?.withOpacity(0.1),
WidgetState.any: Colors.transparent,
},
);
}
}
''';